Advertisement
Guest User

Untitled

a guest
Apr 30th, 2018
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.58 KB | None | 0 0
  1. package data;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8. import java.util.ArrayList;
  9. import java.util.List;
  10.  
  11. public class UserDAO implements IUserDAO {
  12.  
  13. private Connection connection = null;
  14.  
  15. private final String driverName = "com.mysql.jdbc.Driver";
  16. private String serverName = "91.100.3.26"; // Use this server.
  17. private String portNumber = "9865";
  18. private String projectName = "CDIO3";
  19. private String url = "jdbc:mysql://" + serverName + ":" + portNumber + "/" + projectName;
  20.  
  21. private String userName = "Eclipse-bruger";
  22. private String password = "ySmTL37uDjYZmzyn";
  23.  
  24. /**
  25. * Opret testforbindelse, smider exception hvis connection ikke er muligt.
  26. * @throws DALException
  27. */
  28. public void doConnection() throws DALException {
  29. try {
  30. // Load the JDBC driver
  31. Class.forName(driverName);
  32.  
  33. // Create a connection to the database
  34. connection = DriverManager.getConnection(url, userName, password);
  35.  
  36. } catch (ClassNotFoundException e) {
  37. throw new DALException("ClassNotFoundException: in doConnection(): " + e.getMessage());
  38. } catch (SQLException e) {
  39. throw new DALException("SQLException in doConnection() : " + e.getMessage());
  40. }
  41. }
  42.  
  43. /**
  44. * getUser_idFromDatabase giver et userId, til brug af SQL sætnings-sendinger, bruges kun online.
  45. * @param username brugernavn
  46. * @return user_id på brugeren
  47. * @throws DALException såfremt en SQL fejl sker.
  48. */
  49. public int getUser_idFromDatabase(String username){
  50. String query = "SELECT * FROM users WHERE username='" + username + "'";
  51. int userId = 0;
  52.  
  53. try {
  54. Statement stmt = connection.createStatement();
  55. ResultSet rs = stmt.executeQuery(query);
  56. while (rs.next()) {
  57. userId = rs.getInt("user_id");
  58. }
  59. return userId;
  60.  
  61. } catch (SQLException e) {
  62. System.out.println("SQLException in getUser_id(): " + e.getMessage());
  63. return userId;
  64. }
  65. }
  66.  
  67. /*
  68. * (non-Javadoc)
  69. * @see data.IUserDAO#getUser(java.lang.String)
  70. */
  71. @Override
  72. public UserDTO getUser(String username) throws DALException {
  73. String query = "SELECT * FROM totalView WHERE username='" + username + "'";
  74. int userId = 0;
  75. String userName = null;
  76. String ini = null;
  77. String password = null;
  78. String cpr = null;
  79.  
  80. try {
  81. Statement stmt = connection.createStatement();
  82. ResultSet rs = stmt.executeQuery(query);
  83.  
  84. List<String> roleList = new ArrayList<String>();
  85. if (rs.wasNull()) {
  86. throw new DALException("No users with this name exists");
  87. }
  88.  
  89. while (rs.next()) {
  90. userId = rs.getInt("user_id");
  91. userName = rs.getString("username");
  92. password = rs.getString("password");
  93. cpr = rs.getString("cpr");
  94. ini = rs.getString("ini");
  95. roleList.add(rs.getString("role_id"));
  96. }
  97. UserDTO user = new UserDTO(userId, userName, password, ini, cpr, roleList);
  98. return user;
  99.  
  100. } catch (SQLException e) {
  101. throw new DALException("SQLException in getUser(): " + e.getMessage());
  102. }
  103. }
  104.  
  105. /*
  106. * (non-Javadoc)
  107. * @see data.IUserDAO#getUserList()
  108. */
  109. @Override
  110. public List<UserDTO> getUserList() throws DALException {
  111. List<UserDTO> userList = new ArrayList<UserDTO>();
  112. String userQuery = "SELECT * FROM users WHERE NOT username='admin'";
  113. String roleQuery = "SELECT * from users_roles WHERE NOT users_id='1'";
  114. int user_id = 0;
  115. String userName = null;
  116. String password = null;
  117. String ini = null;
  118. String cpr = null;
  119. try {
  120. Statement stmt = connection.createStatement();
  121. Statement stmt2 = connection.createStatement();
  122. ResultSet rsUsers = stmt.executeQuery(userQuery);
  123. ResultSet rsRoles = stmt2.executeQuery(roleQuery);
  124. while (rsUsers.next()) {
  125. user_id = rsUsers.getInt("user_id");
  126. userName = rsUsers.getString("username");
  127. password = rsUsers.getString("password");
  128. ini = rsUsers.getString("ini");
  129. cpr = rsUsers.getString("cpr");
  130. UserDTO user = new UserDTO(user_id, userName, password, ini, cpr, null);
  131. userList.add(user);
  132. }
  133.  
  134. while (rsRoles.next()) {
  135. for (UserDTO usr : userList) {
  136. if (usr.getUser_id() == rsRoles.getInt("users_id")) {
  137. if (usr.getRoles() == null) {
  138. List<String> exRoleList = new ArrayList<String>();
  139. String lel = rsRoles.getString("roles_id");
  140. exRoleList.add(lel);
  141. usr.setRoles(exRoleList);
  142. } else {
  143. List<String> exRoleList = usr.getRoles();
  144. exRoleList.add(rsRoles.getString("roles_id"));
  145. usr.setRoles(exRoleList);
  146. }
  147. }
  148. }
  149. }
  150. return userList;
  151.  
  152. } catch (SQLException e) {
  153. throw new DALException("SQLException in getUserList(): " + e.getMessage());
  154. }
  155. }
  156.  
  157. /*
  158. * (non-Javadoc)
  159. * @see data.IUserDAO#createUser(data.UserDTO)
  160. */
  161. @Override
  162. public void createUser(UserDTO user) throws DALException {
  163. List<String> roleList = user.getRoles();
  164.  
  165. String userQuery = "INSERT INTO users (username, password, ini, cpr) " + "VALUES(" + "'" + user.getUserName()
  166. + "'," + "'" + user.getPassword() + "'," + "'" + user.getIni() + "'," + "'" + user.getCpr() + "')";
  167.  
  168. try {
  169. Statement stmt = connection.createStatement();
  170. stmt.executeUpdate(userQuery);
  171. for (String role : roleList) {
  172. Statement stmt2 = connection.createStatement();
  173. stmt2.executeUpdate(
  174. "INSERT INTO users_roles VALUES (" + getUser_idFromDatabase(user.getUserName()) + ", " + role + ")");
  175. }
  176.  
  177. } catch (SQLException e) {
  178. throw new DALException("SQLException in createUser(): " + e.getMessage());
  179. }
  180. }
  181.  
  182. /*
  183. * (non-Javadoc)
  184. * @see data.IUserDAO#updateUser(data.UserDTO)
  185. */
  186. @Override
  187. public void updateUser(UserDTO user) throws DALException {
  188. String query = "UPDATE users SET username ='" + user.getUserName() + "', password ='" + user.getPassword()
  189. + "', ini ='" + user.getIni() + "',cpr ='" + user.getCpr() + "' WHERE user_id='" + user.getUser_id()
  190. + "'";
  191. try {
  192. Statement stmt = connection.createStatement();
  193. stmt.executeUpdate(query);
  194. } catch (SQLException e) {
  195. throw new DALException("SQLException in updateUser(): " + e.getMessage());
  196. }
  197. }
  198.  
  199. /*
  200. * (non-Javadoc)
  201. * @see data.IUserDAO#deleteUser(java.lang.String)
  202. */
  203. @Override
  204. public void deleteUser(String userName) throws DALException {
  205. getUser_idFromDatabase(userName);
  206. String usersQuery = "DELETE FROM users WHERE username ='" + userName + "'";
  207. try {
  208. Statement stmt = connection.createStatement();
  209. stmt.executeUpdate(usersQuery);
  210. } catch (SQLException e) {
  211. throw new DALException("SQLException in deleteUser(): " + e.getMessage());
  212. }
  213. }
  214.  
  215. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement