Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- package dataaccess;
- import business.User;
- import java.io.*;
- import java.sql.Connection;
- import java.sql.PreparedStatement;
- import java.sql.SQLException;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.util.ArrayList;
- import java.util.List;
- import java.util.Set;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class UserDB {
- public static int insert(User user) throws IOException, SQLException, ClassNotFoundException
- {
- // load the driver
- try{
- Class.forName("com.mysql.jdbc.Driver");
- // get a connection
- String dbURL = "jdbc:mysql://localhost:3306/twitterdb";
- String username = "root";
- String password = "sesame";
- Connection connection = DriverManager.getConnection(dbURL, username, password);
- PreparedStatement ps = null;
- String query
- = "INSERT INTO user (fullname, username, emailAddress, password, birthdate, questionNo, answer) "
- + "VALUES (?, ?, ?, ?, ?, ?, ?)";
- ps = connection.prepareStatement(query);
- ps.setString(1, user.getFullName());
- ps.setString(2, user.getUserName());
- ps.setString(3, user.getEmail());
- ps.setString(4, user.getPassword());
- ps.setString(5, user.getBirthdate());
- ps.setString(6, user.getQuestionNo());
- ps.setString(7, user.getAnswer());
- return ps.executeUpdate();
- } catch (SQLException e) {
- System.out.println(e);
- return 0;
- }
- }
- /**
- *
- * @param emailAddress
- * @return
- */
- public static User search(String emailAddress)
- {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- // get a connection
- String dbURL = "jdbc:mysql://localhost:3306/twitterdb";
- String username = "root";
- String password = "sesame";
- Connection connection = DriverManager.getConnection(dbURL, username, password);
- PreparedStatement ps = null;
- ResultSet rs = null;
- String query = "SELECT * FROM User "
- + "WHERE Email = ?";
- ps = connection.prepareStatement(query);
- ps.setString(1, emailAddress);
- rs = ps.executeQuery();
- User user = null;
- if(rs.next())
- {
- user = new User();
- user.setFullName(rs.getString("fullName"));
- user.setUserName(rs.getString("userName"));
- user.setEmail(rs.getString("email"));
- user.setPassword(rs.getString("password"));
- user.setBirthdate(rs.getString("bitrhdate"));
- user.setQuestionNo(rs.getString("questionNo"));
- user.setAnswer(rs.getString("answer"));
- }
- return user;
- } catch (SQLException e) {
- System.out.println(e);
- return null;
- } catch (ClassNotFoundException ex) {
- Logger.getLogger(UserDB.class.getName()).log(Level.SEVERE, null, ex);
- }
- return null;
- }
- public static boolean emailExists(String email) {
- throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement