Advertisement
Guest User

Untitled

a guest
Mar 9th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. import model.UserDtoModel;
  2. import model.UserLocalModel;
  3.  
  4. import java.sql.*;
  5. import java.util.ArrayList;
  6. import java.util.List;
  7.  
  8. public class DatabaseHandler implements DatabseQueries {
  9.  
  10. private Connection connection;
  11.  
  12. private Boolean checkIfJdbcDriverIsActive() {
  13. String driver = "com.mysql.jdbc.Driver";
  14. try {
  15. Class.forName(driver);
  16. return true;
  17. } catch (Exception ex) {
  18. System.out.println("Database driver not found");
  19. }
  20. return false;
  21. }
  22.  
  23. public void openConnection(String dbUrl, String userName, String password) {
  24. checkIfJdbcDriverIsActive();
  25. try {
  26. connection = DriverManager.getConnection(dbUrl, userName, password);
  27. } catch (SQLException e) {
  28. e.printStackTrace();
  29. }
  30. }
  31.  
  32. @Override
  33. public List<UserLocalModel> selectAllUsers(String executeQuery2) throws SQLException {
  34. String db_url = "jdbc:mysql://mysql.agh.edu.pl:3306/bachniak";
  35. String userName = "bachniak";
  36. String password = "kHRSkY8bZjKjzhan";
  37. // openConnection(db_url, userName, password);
  38.  
  39. Connection connection = null;
  40. try {
  41. connection = DriverManager.getConnection(db_url, userName, password);
  42. System.out.println("Connected :D");
  43. } catch (SQLException e) {
  44. e.printStackTrace();
  45. System.out.println("Kurwa error");
  46. }
  47.  
  48. String executeQuery = "SELECT * FROM Krzysztof";
  49. Statement statement = connection.createStatement();
  50. ResultSet resultSet = statement.executeQuery(executeQuery);
  51. List<UserLocalModel> usersList = new ArrayList();
  52. UserDtoModel dtoModel = new UserDtoModel(0, "", "");
  53.  
  54. while (resultSet.next()) {
  55. dtoModel.setId(resultSet.getInt("id"));
  56. dtoModel.setFirstName(resultSet.getString("first_name"));
  57. dtoModel.setLastName(resultSet.getString("last_name"));
  58. usersList.add(new UserLocalModel(dtoModel.getId(), dtoModel.getFirstName(), dtoModel.getLastName()));
  59. System.out.println(dtoModel.getId().toString() + " " + dtoModel.getFirstName() + "" + dtoModel.getLastName());
  60. }
  61. return usersList;
  62. }
  63.  
  64. @Override
  65. public void insert(UserDtoModel model) {
  66.  
  67. }
  68.  
  69. @Override
  70. public void remove(UserDtoModel model) {
  71.  
  72. }
  73. }
  74.  
  75.  
  76. .......................
  77.  
  78.  
  79. package model;
  80.  
  81. public class UserLocalModel {
  82.  
  83. private Integer id;
  84. private String firstName;
  85. private String lastName;
  86.  
  87. public UserLocalModel(Integer id, String firstName, String lastName) {
  88. this.id = id;
  89. this.firstName = firstName;
  90. this.lastName = lastName;
  91. }
  92.  
  93. public Integer getId() {
  94. return id;
  95. }
  96.  
  97. public void setId(Integer id) {
  98. this.id = id;
  99. }
  100.  
  101. public String getFirstName() {
  102. return firstName;
  103. }
  104.  
  105. public void setFirstName(String firstName) {
  106. this.firstName = firstName;
  107. }
  108.  
  109. public String getLastName() {
  110. return lastName;
  111. }
  112.  
  113. public void setLastName(String lastName) {
  114. this.lastName = lastName;
  115. }
  116. }
  117.  
  118.  
  119.  
  120. ..................
  121.  
  122.  
  123. import model.UserDtoModel;
  124. import model.UserLocalModel;
  125.  
  126. import java.sql.SQLException;
  127. import java.util.List;
  128.  
  129. public interface DatabseQueries {
  130. List<UserLocalModel> selectAllUsers(String executeQuery) throws SQLException;
  131. void insert(UserDtoModel model);
  132. void remove(UserDtoModel model);
  133. }
  134.  
  135. ....
  136.  
  137. import java.sql.SQLException;
  138.  
  139. public class Main {
  140.  
  141.  
  142. public static void main(String[] args) {
  143. String db_url = "jdbc:mysql://mysql.agh.edu.pl:3306/bachniak";
  144. String userName = "Krzysztof";
  145. String password = "kHRSkY8bZjKjzhan";
  146. DatabaseHandler databaseHandler = new DatabaseHandler();
  147. // databaseHandler.openConnection(db_url, userName, password);
  148. try {
  149. databaseHandler.selectAllUsers("");
  150. } catch (SQLException e) {
  151. e.printStackTrace();
  152. }
  153. }
  154.  
  155. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement