Advertisement
Guest User

Untitled

a guest
Jun 13th, 2016
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. package servlet1.dao;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.ResultSet;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.sql.PreparedStatement;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. import servlet1.webshop.User;
  12. import util.ConnectionManager;
  13.  
  14. public class UserDAO {
  15. public User getUserByID(int id) {
  16. User user = null;
  17. try {
  18. Connection conn = ConnectionManager.getConnection();
  19. String selectSQL = "SELECT id, username, password FROM User WHERE id = ?";
  20. PreparedStatement preparedStatement = conn
  21. .prepareStatement(selectSQL);
  22. preparedStatement.setInt(1, id);
  23. ResultSet rset = preparedStatement.executeQuery();
  24.  
  25. if (rset.next()) {
  26. String username = rset.getString(1);
  27. String password = rset.getString(2);
  28.  
  29. user = new User(username, password);
  30. }
  31. rset.close();
  32. preparedStatement.close();
  33. } catch (Exception ex) {
  34. ex.printStackTrace();
  35. }
  36. return user;
  37. }
  38.  
  39. public User getUserByUsernameAndPassword(String username, String password) {
  40. User user = null;
  41. try {
  42. Connection conn = ConnectionManager.getConnection();
  43. String selectSQL = "SELECT id FROM User WHERE username = ? AND password = ?";
  44. PreparedStatement preparedStatement = conn
  45. .prepareStatement(selectSQL);
  46. preparedStatement.setString(1, username);
  47. preparedStatement.setString(2, password);
  48. ResultSet rset = preparedStatement.executeQuery();
  49.  
  50. if (rset.next()) {
  51. int id = rset.getInt(1);
  52. user = new User(id, username, password);
  53. }
  54. rset.close();
  55. preparedStatement.close();
  56. } catch (Exception ex) {
  57. ex.printStackTrace();
  58. }
  59. return user;
  60. }
  61.  
  62. public boolean deleteUser(int id) {
  63. boolean retVal = false;
  64. try {
  65. Connection conn = ConnectionManager.getConnection();
  66.  
  67. String selectSQL = "DELETE FROM User WHERE id = ?";
  68. PreparedStatement preparedStatement = conn
  69. .prepareStatement(selectSQL);
  70. preparedStatement.setInt(1, id);
  71. if (preparedStatement.executeUpdate() == 1)
  72. retVal = true;
  73. preparedStatement.close();
  74.  
  75. } catch (Exception ex) {
  76. ex.printStackTrace();
  77. }
  78. return retVal;
  79. }
  80.  
  81. public boolean insertUser(User user) {
  82. boolean retVal = false;
  83. try {
  84. String update = "INSERT INTO User (username, password) values (?, ?)";
  85. Connection conn = ConnectionManager.getConnection();
  86. PreparedStatement pstmt = conn.prepareStatement(update);
  87. pstmt.setString(1, user.getUsername());
  88. pstmt.setString(2, user.getPassword());
  89. if (pstmt.executeUpdate() == 1) {
  90. retVal = true;
  91. }
  92. pstmt.close();
  93. } catch (SQLException e) {
  94. e.printStackTrace();
  95. }
  96. return retVal;
  97.  
  98. }
  99.  
  100. public List<User> getAllUsers() {
  101. String query = "select id, username, password from User";
  102. Statement stmt;
  103. List<User> retVal = null;
  104. try {
  105. stmt = ConnectionManager.getConnection().createStatement();
  106. ResultSet rset = stmt.executeQuery(query);
  107. retVal = new ArrayList<User>();
  108. while (rset.next()) {
  109. int id = rset.getInt(1);
  110. String username = rset.getString(2);
  111. String password = rset.getString(3);
  112. User u = new User(id, username, password);
  113. retVal.add(u);
  114. }
  115. rset.close();
  116. stmt.close();
  117. } catch (SQLException e) {
  118. // TODO Auto-generated catch block
  119. e.printStackTrace();
  120. }
  121. return retVal;
  122. }
  123.  
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement