Advertisement
Guest User

Untitled

a guest
Oct 16th, 2018
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.74 KB | None | 0 0
  1. /*
  2. * To change this license header, choose License Headers in Project Properties.
  3. * To change this template file, choose Tools | Templates
  4. * and open the template in the editor.
  5. */
  6. package dataaccess;
  7. import business.User;
  8. import java.io.*;
  9. import java.sql.Connection;
  10. import java.sql.PreparedStatement;
  11. import java.sql.SQLException;
  12. import java.sql.DriverManager;
  13. import java.sql.ResultSet;
  14. import java.util.ArrayList;
  15. import java.util.List;
  16. import java.util.Set;
  17. import java.util.logging.Level;
  18. import java.util.logging.Logger;
  19.  
  20. public class UserDB {
  21. public static int insert(User user) throws IOException, SQLException, ClassNotFoundException
  22. {
  23. // load the driver
  24. try{
  25. Class.forName("com.mysql.jdbc.Driver");
  26.  
  27. // get a connection
  28. String dbURL = "jdbc:mysql://localhost:3306/twitterdb";
  29. String username = "root";
  30. String password = "sesame";
  31.  
  32. Connection connection = DriverManager.getConnection(dbURL, username, password);
  33.  
  34. PreparedStatement ps = null;
  35.  
  36.  
  37.  
  38. String query
  39. = "INSERT INTO user (fullname, username, emailAddress, password, birthdate, questionNo, answer) "
  40. + "VALUES (?, ?, ?, ?, ?, ?, ?)";
  41.  
  42.  
  43. ps = connection.prepareStatement(query);
  44. ps.setString(1, user.getFullName());
  45. ps.setString(2, user.getUserName());
  46. ps.setString(3, user.getEmail());
  47. ps.setString(4, user.getPassword());
  48. ps.setString(5, user.getBirthdate());
  49. ps.setString(6, user.getQuestionNo());
  50. ps.setString(7, user.getAnswer());
  51. return ps.executeUpdate();
  52. } catch (SQLException e) {
  53. System.out.println(e);
  54. return 0;
  55. }
  56.  
  57. }
  58.  
  59. /**
  60. *
  61. * @param emailAddress
  62. * @return
  63. */
  64. public static User search(String emailAddress)
  65. {
  66.  
  67. try {
  68. Class.forName("com.mysql.jdbc.Driver");
  69.  
  70. // get a connection
  71. String dbURL = "jdbc:mysql://localhost:3306/twitterdb";
  72. String username = "root";
  73. String password = "sesame";
  74.  
  75. Connection connection = DriverManager.getConnection(dbURL, username, password);
  76.  
  77. PreparedStatement ps = null;
  78. ResultSet rs = null;
  79.  
  80. String query = "SELECT * FROM User "
  81. + "WHERE Email = ?";
  82.  
  83. ps = connection.prepareStatement(query);
  84. ps.setString(1, emailAddress);
  85. rs = ps.executeQuery();
  86.  
  87. User user = null;
  88. if(rs.next())
  89. {
  90. user = new User();
  91. user.setFullName(rs.getString("fullName"));
  92. user.setUserName(rs.getString("userName"));
  93. user.setEmail(rs.getString("email"));
  94. user.setPassword(rs.getString("password"));
  95. user.setBirthdate(rs.getString("bitrhdate"));
  96. user.setQuestionNo(rs.getString("questionNo"));
  97. user.setAnswer(rs.getString("answer"));
  98. }
  99. return user;
  100. } catch (SQLException e) {
  101. System.out.println(e);
  102. return null;
  103. } catch (ClassNotFoundException ex) {
  104. Logger.getLogger(UserDB.class.getName()).log(Level.SEVERE, null, ex);
  105. }
  106. return null;
  107. }
  108.  
  109. public static boolean emailExists(String email) {
  110. throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
  111. }
  112.  
  113. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement