Advertisement
Guest User

Untitled

a guest
Sep 19th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.01 KB | None | 0 0
  1. //Здесь создаются каналы подключения к БД
  2. package ConnectionPooling;
  3.  
  4. import javax.sql.DataSource;
  5. import org.apache.commons.dbcp.ConnectionFactory;
  6. import org.apache.commons.dbcp.DriverManagerConnectionFactory;
  7. import org.apache.commons.dbcp.PoolableConnectionFactory;
  8. import org.apache.commons.dbcp.PoolingDataSource;
  9. import org.apache.commons.pool.impl.GenericObjectPool;
  10.  
  11. import java.sql.Connection;
  12.  
  13. public class ConnectionPool {
  14. // JDBC Driver Name & Database URL
  15. static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
  16. static final String JDBC_DB_URL = "jdbc:mysql://localhost:3306/office_datas?" +
  17. "useUnicode=true&characterEncoding=utf-8";
  18. // JDBC Database Credentials
  19. static final String JDBC_USER = "root";
  20. static final String JDBC_PASS = "";
  21. private static GenericObjectPool gPool = null;
  22. public static Connection sCon = null; //переменная с которой возможно проблема
  23.  
  24. @SuppressWarnings("unused")
  25. public DataSource setUpPool() throws Exception {
  26. Class.forName(JDBC_DRIVER);
  27. // Creates an Instance of GenericObjectPool That Holds Our Pool of Connections Object!
  28. gPool = new GenericObjectPool();
  29. gPool.setMaxActive(2);
  30. // Creates a ConnectionFactory Object Which Will Be Use by the Pool to Create the Connection Object!
  31. ConnectionFactory cf = new DriverManagerConnectionFactory(JDBC_DB_URL, JDBC_USER, JDBC_PASS);
  32. // Creates a PoolableConnectionFactory
  33. // That Will Wraps the Connection Object Created by the ConnectionFactory to Add Object Pooling Functionality!
  34. PoolableConnectionFactory pcf = new PoolableConnectionFactory(cf, gPool,
  35. null, null, false, true);
  36.  
  37. return new PoolingDataSource(gPool);
  38. }
  39.  
  40. public GenericObjectPool getConnectionPool() {
  41. return gPool;
  42. }
  43.  
  44. public void closePool() throws Exception {
  45. gPool.close();
  46. }
  47.  
  48. // This Method Is Used To Print The Connection Pool Status
  49. public void printDbStatus() {
  50. System.out.println("Max.: " + getConnectionPool().getMaxActive() + "; Active: "
  51. + getConnectionPool().getNumActive() + "; Idle: " + getConnectionPool().getNumIdle());
  52. }
  53.  
  54. public void createConnection() throws Exception {
  55. DataSource dataSource = this.setUpPool();
  56. this.printDbStatus();
  57. sCon = dataSource.getConnection();
  58. this.printDbStatus();
  59. }
  60. }
  61.  
  62. //этот метод уже в другом классе, он обновляет БД
  63. public void update(Staff staff) {
  64. try {
  65. PreparedStatement preparedStatement =
  66. ConnectionPool.sCon.prepareStatement("UPDATE staff "+
  67. "SET " +
  68. "surname = ?, " +
  69. "name = ?, " +
  70. "fatherName = ?, " +
  71. "typeWork = ?, " +
  72. "position = ?, " +
  73. "addInfo = ? " +
  74. "WHERE idStaff = ?");
  75. preparedStatement.setString(1, staff.getmSurname());
  76. preparedStatement.setString(2, staff.getmName());
  77. preparedStatement.setString(3, staff.getmFathName());
  78. preparedStatement.setInt(4, staff.getmTypeWork());
  79. preparedStatement.setString(5, staff.getmPosition());
  80. preparedStatement.setString(6, staff.getmAddInfo());
  81. preparedStatement.setInt(7, staff.getmIdstaff());
  82.  
  83.  
  84. preparedStatement.executeUpdate();
  85. preparedStatement.close();
  86. } catch (Exception e) {
  87. e.printStackTrace();
  88. }
  89. }
  90.  
  91. //этот метод создает окно для редактирования данных
  92. private void createGui() {
  93. if (primaryStage == null) {
  94. primaryStage = new Stage();
  95. primaryStage.setTitle("Персональная информация");
  96. primaryStage.setScene(new Scene(fxmlPersonalInfo));
  97. primaryStage.initModality(Modality.WINDOW_MODAL);
  98. primaryStage.initOwner(mainStage);
  99. primaryStage.setMaxWidth(1020);
  100. primaryStage.setMaxHeight(660);
  101. primaryStage.setMinHeight(primaryStage.getHeight());
  102. primaryStage.setMinWidth(primaryStage.getWidth());
  103. }
  104. primaryStage.show();
  105. }
  106. //Этот метод обновляет объект, и сюда же я пытаюсь перенести обновление данных в БД, но появляется ошибка
  107. private void UpdateStaff(){
  108. Integer typeWork = 0;
  109. if (checkPed.isSelected()){
  110. typeWork = 1;
  111. }
  112. if (checkTech.isSelected()){
  113. typeWork =2;
  114. }
  115. staff.setmName(textName.getText());
  116. staff.setmSurname(textSurname.getText());
  117. staff.setmFathName(textFathNam.getText());
  118. staff.setmPosition(textPosition.getText());
  119. staff.setmAddInfo(textAddInfo.getText());
  120. staff.setmIdDoc(textNumPassp.getText());
  121. staff.setmDocPrivetNum(textPrivateNum.getText());
  122. staff.setmTel1(textTel1.getText());
  123. staff.setmTel2(textTel2.getText());
  124. staff.setmDateOfBirth(textDateOfBirth.getValue().toString());
  125. staff.setmTypeWork(typeWork);
  126. staff.setmAddress(textAddress.getText());
  127. staff.setmAnyAddress(textAnyAddress.getText());
  128. //вот этот метод, если делать так - ошибка с потоками
  129. mCollectionListStaff.update(staff);
  130.  
  131.  
  132. //пробовал это делать через потоки, если делать так - nullPointexeption
  133. /*Thread t = new Thread(() -> {
  134. try {
  135. mCollectionListStaff.update(staff);
  136.  
  137. } catch (Exception e) {
  138. e.printStackTrace();
  139. }
  140.  
  141. });
  142. t.start();*/
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement