Advertisement
Guest User

Add to userdb

a guest
Oct 16th, 2018
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.58 KB | None | 0 0
  1. public static User LoginU(String emailAddress)
  2. {
  3.  
  4. try {
  5. Class.forName("com.mysql.jdbc.Driver");
  6.  
  7. // get a connection
  8. String dbURL = "jdbc:mysql://localhost:3306/twitterdb";
  9. String username = "root";
  10. String password = "sesame";
  11.  
  12. Connection connection = DriverManager.getConnection(dbURL, username, password);
  13.  
  14. PreparedStatement ps = null;
  15. ResultSet rs = null;
  16.  
  17. String query = "SELECT * FROM User "
  18. + "WHERE username = ?";
  19.  
  20. ps = connection.prepareStatement(query);
  21. ps.setString(1, emailAddress);
  22. rs = ps.executeQuery();
  23.  
  24. User user = null;
  25. if(rs.next())
  26. {
  27. user = new User();
  28. user.setFullName(rs.getString("fullname"));
  29. user.setUserName(rs.getString("username"));
  30. user.setEmail(rs.getString("emailAddress"));
  31. user.setPassword(rs.getString("password"));
  32. user.setBirthdate(rs.getString("bitrhdate"));
  33. user.setQuestionNo(rs.getString("questionNo"));
  34. user.setAnswer(rs.getString("answer"));
  35. }
  36. return user;
  37. } catch (SQLException e) {
  38. System.out.println(e);
  39. return null;
  40. } catch (ClassNotFoundException ex) {
  41. Logger.getLogger(UserDB.class.getName()).log(Level.SEVERE, null, ex);
  42. }
  43. return null;
  44. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement