Guest User

Untitled

a guest
Apr 24th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.99 KB | None | 0 0
  1. package classes;
  2.  
  3. import javafx.collections.FXCollections;
  4. import javafx.collections.ObservableList;
  5.  
  6. import java.sql.*;
  7.  
  8. public class DB {
  9.  
  10. public static ObservableList<Employee> data = FXCollections.observableArrayList();
  11.  
  12. private final String ip;
  13. private final String username;
  14. private final String password;
  15. private final String port;
  16. private final String database_name;
  17. private Connection connection;
  18.  
  19. public DB() {
  20. this.ip = "192.168.99.100";
  21. this.username = "sa";
  22. this.password = "nastenjka26";
  23. this.port = "1433";
  24. this.database_name = "DBEmployee";
  25. }
  26.  
  27. public void connect() throws Exception {
  28. Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver").newInstance();
  29. connection = DriverManager.getConnection("jdbc:sqlserver://" + this.ip + ":" + this.port + ";databaseName=" + this.database_name, this.username, this.password);
  30. }
  31.  
  32. public ObservableList<Employee> readAllRows() throws Exception {
  33. connect();
  34. PreparedStatement ps = connection.prepareStatement("SELECT * FROM tblEmployee");
  35. ResultSet rs = ps.executeQuery();
  36.  
  37. while (rs.next()) {
  38.  
  39. data.add(new Employee(
  40. rs.getInt("fldId"),
  41. rs.getString("fldName"),
  42. rs.getString("fldDepartment"),
  43. rs.getInt("fldWeeklyHours"),
  44. rs.getDate("fldDateOfEmployment")
  45. ));
  46.  
  47. }
  48. rs.close();
  49. ps.close();
  50.  
  51. return data;
  52. }
  53.  
  54. public void update(Employee newEmployee) throws Exception {
  55. connect();
  56. String sql = "UPDATE tblEmployee SET fldName = ? ,"
  57. + "fldDepartment = ? ,"
  58. + "fldWeeklyHours = ? ,"
  59. + "fldDateOfEmployment = ? "
  60. + "WHERE fldId = ?";
  61.  
  62. try (PreparedStatement pstmt = connection.prepareStatement(sql)) {
  63.  
  64. pstmt.setString(1, newEmployee.getName());
  65. pstmt.setString(2, newEmployee.getDepartment());
  66. pstmt.setDouble(3, newEmployee.getWeeklyHours());
  67. pstmt.setDate(4, newEmployee.getDateOfEmployment());
  68. pstmt.setInt(5, newEmployee.getId());
  69.  
  70. pstmt.executeUpdate();
  71.  
  72. } catch (SQLException e) {
  73. System.err.println(e.getMessage());
  74. }
  75.  
  76. }
  77.  
  78. public void updateRows() throws Exception {
  79. connect();
  80. PreparedStatement ps = connection.prepareStatement("SELECT * FROM tblEmployee WHERE fldId = ?");
  81. Employee oldEmployee = null;
  82.  
  83. for (Employee newEmployee : data) {
  84. ps.setInt(1, newEmployee.getId());
  85.  
  86. ResultSet rs = ps.executeQuery();
  87.  
  88. while (rs.next()) {
  89.  
  90. oldEmployee = new Employee(
  91. rs.getInt("fldId"),
  92. rs.getString("fldName"),
  93. rs.getString("fldDepartment"),
  94. rs.getInt("fldWeeklyHours"),
  95. rs.getDate("fldDateOfEmployment")
  96. );
  97.  
  98. }
  99. rs.close();
  100.  
  101.  
  102. if (oldEmployee.getName() != newEmployee.getName() ||
  103. oldEmployee.getDepartment() != newEmployee.getDepartment() ||
  104. oldEmployee.getWeeklyHours() != newEmployee.getWeeklyHours() ||
  105. oldEmployee.getDateOfEmployment() != newEmployee.getDateOfEmployment()
  106. ) {
  107. update(newEmployee);
  108. }
  109.  
  110.  
  111. }
  112.  
  113. }
  114.  
  115.  
  116. public int insertNewRow(Employee employee) throws Exception {
  117. connect();
  118. PreparedStatement ps = connection.prepareStatement("INSERT INTO tblEmployee(fldName, fldDepartment, fldWeeklyHours,fldDateOfEmployment) VALUES (?,?,?,?);");
  119.  
  120. ps.setString(1, employee.getName());
  121. ps.setString(2, employee.getDepartment());
  122. ps.setDouble(3, employee.getWeeklyHours());
  123. ps.setDate(4, employee.getDateOfEmployment());
  124.  
  125. int row = ps.executeUpdate();
  126. ps.close();
  127.  
  128.  
  129. return row;
  130. }
  131.  
  132.  
  133. }
Add Comment
Please, Sign In to add comment