Advertisement
Guest User

Untitled

a guest
May 23rd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.24 KB | None | 0 0
  1. package bloggen.io;
  2.  
  3. import java.sql.*;
  4. import java.util.ArrayList;
  5. import bloggen.domain.User;
  6. import dk.au.hum.imv.persistence.db.DatabasePersistent;
  7. import dk.au.hum.imv.persistence.db.JDBCConnectionFactory;
  8.  
  9. public class UserDAO extends DatabasePersistent {
  10.  
  11.  
  12. public static User getUserById(long ID) throws Exception {
  13.  
  14. User returnUser = null;
  15. Connection myConn = null;
  16.  
  17. try { // myConn objekt
  18.  
  19. myConn = JDBCConnectionFactory.getNewConnection();
  20.  
  21. String sql = "SELECT * FROM User WHERE userID = ?";
  22. PreparedStatement prep = myConn.prepareStatement(sql);
  23. prep.setLong(1, ID);
  24.  
  25. ResultSet res = prep.executeQuery();
  26.  
  27. while (res.next()) {
  28. String fullName = res.getString("User.fullName");
  29. String userName = res.getString("User.userName");
  30. String password = res.getString("User.password");
  31. String email = res.getString("User.email");
  32.  
  33. returnUser = new User(ID, fullName, userName, password, email);
  34.  
  35. }
  36. } catch (SQLException e) {
  37. e.printStackTrace();
  38. } finally {
  39. JDBCConnectionFactory.closeConnection(myConn);
  40. }
  41.  
  42. return returnUser;
  43. }
  44.  
  45. // ny arraylist,
  46. // her vil vi opbevarer vore brugere, så vi kan udskrive dem og løbe igennem dem.
  47.  
  48.  
  49. public static ArrayList<User> getAllUsers() throws SQLException {
  50. // Arraylist --> indholder metoden getAllUsers
  51.  
  52. ArrayList<User> users = new ArrayList<User>();
  53.  
  54. Connection myConn = null;
  55.  
  56. try {
  57. myConn = JDBCConnectionFactory.getNewConnection();
  58. String sql = "SELECT * FROM User";
  59. Statement statement = myConn.createStatement();
  60. ResultSet res = statement.executeQuery(sql);
  61.  
  62. while (res.next()) {
  63. long userID = res.getLong("userID");
  64. String fullName = res.getString("fullName");
  65. String userName = res.getString("userName");
  66. String password = res.getString("Password");
  67. String email = res.getString("email");
  68. users.add(new User(userID, fullName, userName, password, email));
  69. }
  70. } catch (SQLException e) {
  71. e.printStackTrace();
  72. } finally {
  73. JDBCConnectionFactory.closeConnection(myConn);
  74. }
  75. return users;
  76. }
  77.  
  78. public static void createUser(String userName, String password, String email, String fullName, long userID)
  79. throws SQLException {
  80.  
  81. Connection myConn = null;
  82.  
  83. try {
  84. myConn = JDBCConnectionFactory.getNewConnection();
  85.  
  86.  
  87. PreparedStatement statement = myConn.prepareStatement(
  88. "INSERT INTO User (userName, password, email, fullName, userID) VALUES(?, ?, ?, ?, ?)");
  89. statement.setString(1, userName);
  90. statement.setString(2, password);
  91. statement.setString(3, email);
  92. statement.setString(4, fullName);
  93. statement.setLong(5, userID);
  94. statement.executeUpdate();
  95.  
  96. } catch (SQLException e) {
  97. e.printStackTrace();
  98. } finally {
  99. JDBCConnectionFactory.closeConnection(myConn);
  100. }
  101.  
  102. }
  103.  
  104.  
  105. public static User createUserFromResultSet(ResultSet res) throws SQLException { //opretter et user objekt med det der kommer som forespørgelsel fra database, så der bliver lavet et nyt userobjekt hver gang
  106.  
  107. String fullName = res.getString("user.fullName");
  108. String userName = res.getString("user.userName");
  109. String password = res.getString("password");
  110. long databaseId = res.getLong("user.userID");
  111. String email = res.getString("user.email");
  112. User result = new User(databaseId, fullName, userName, password, email);
  113. return result;
  114. }
  115.  
  116.  
  117.  
  118. public static User getUserFromCredentials(String loginname, String password) throws SQLException {
  119. //metode til at hente bruger ud fra brugernavn og password. --> Den fungerer med login.jsp
  120.  
  121. User result = null;
  122. Connection con = null;
  123.  
  124. try{
  125.  
  126. con = JDBCConnectionFactory.getNewConnection();
  127.  
  128. String sql = "SELECT * FROM User WHERE User.userName = ? AND User.password = ?";
  129. PreparedStatement prep = con.prepareStatement(sql);
  130. prep.setString(1, loginname);
  131. prep.setString(2, password);
  132. ResultSet res = prep.executeQuery();
  133. while (res.next()) {
  134. result = createUserFromResultSet(res);
  135. }
  136.  
  137. } catch(SQLException e) {
  138.  
  139. e.printStackTrace();
  140.  
  141. } finally {
  142. JDBCConnectionFactory.closeConnection(con);
  143.  
  144. }
  145. return result;
  146. }
  147.  
  148.  
  149. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement