Guest User

docsdao

a guest
May 3rd, 2022
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.66 KB | None | 0 0
  1. package pt.agilefactor.trulydocs;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.PreparedStatement;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9. import java.util.ArrayList;
  10. import java.util.List;
  11.  
  12. public class DocsDAO {
  13. private String jdbcURL;
  14. private String jdbcUsername;
  15. private String jdbcPassword;
  16. private Connection jdbcConnection;
  17.  
  18. public DocsDAO(String jdbcURL, String jdbcUsername, String jdbcPassword) {
  19. this.jdbcURL = jdbcURL;
  20. this.jdbcUsername = jdbcUsername;
  21. this.jdbcPassword = jdbcPassword;
  22. }
  23.  
  24. protected void connect() throws SQLException {
  25. if (this.jdbcConnection == null || this.jdbcConnection.isClosed()) {
  26. try {
  27. Class.forName("com.mysql.cj.jdbc.Driver"); //$NON-NLS-1$
  28. } catch (ClassNotFoundException e) {
  29. throw new SQLException(e);
  30. }
  31. this.jdbcConnection = DriverManager.getConnection(this.jdbcURL, this.jdbcUsername, this.jdbcPassword);
  32. }
  33. }
  34.  
  35. protected void disconnect() throws SQLException {
  36. if (this.jdbcConnection != null && !this.jdbcConnection.isClosed()) {
  37. this.jdbcConnection.close();
  38. }
  39. }
  40.  
  41. public boolean insertDocs(Docs docs) throws SQLException {
  42. String sql = "INSERT INTO document (title_ds, document_tp, created_dt, update_dt, short_ds, created_user_id, update_user_id, long_ds) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"; //$NON-NLS-1$
  43. boolean rowInserted = false;
  44. connect();
  45. try (PreparedStatement statement = this.jdbcConnection.prepareStatement(sql);) {
  46. // PreparedStatement statement = this.jdbcConnection.prepareStatement(sql);
  47. statement.setString(1, docs.getTitle_ds());
  48. statement.setString(2, docs.getDocument_tp());
  49. statement.setString(3, docs.getCreated_dt());
  50. statement.setString(4, docs.getUpdate_dt());
  51. statement.setString(5, docs.getShort_ds());
  52. statement.setString(6, docs.getCreated_user_id());
  53. statement.setString(7, docs.getUpdate_user_id());
  54. statement.setString(8, docs.getLong_ds());
  55.  
  56. rowInserted = statement.executeUpdate() > 0;
  57. statement.close();
  58. }
  59. disconnect();
  60. return rowInserted;
  61. }
  62.  
  63. public List<Docs> listAllDocs() throws SQLException {
  64. List<Docs> listDocs = new ArrayList<>();
  65. String sql = "SELECT * FROM document"; //$NON-NLS-1$
  66. connect();
  67. try (Statement statement = this.jdbcConnection.createStatement();) {
  68. // Statement statement = this.jdbcConnection.createStatement();
  69. try (ResultSet resultSet = statement.executeQuery(sql)) {
  70. // ResultSet resultSet = statement.executeQuery(sql);
  71.  
  72. while (resultSet.next()) {
  73. String document_id = resultSet.getString("document_id"); //$NON-NLS-1$
  74. String title_ds = resultSet.getString("title_ds"); //$NON-NLS-1$
  75. String document_tp = resultSet.getString("document_tp"); //$NON-NLS-1$
  76. String created_dt = resultSet.getString("created_dt"); //$NON-NLS-1$
  77. String update_dt = resultSet.getString("update_dt"); //$NON-NLS-1$
  78. String short_ds = resultSet.getString("short_ds"); //$NON-NLS-1$
  79. String created_user_id = resultSet.getString("created_user_id"); //$NON-NLS-1$
  80. String update_user_id = resultSet.getString("update_user_id"); //$NON-NLS-1$
  81. String long_ds = resultSet.getString("long_ds"); //$NON-NLS-1$
  82.  
  83. Docs docs = new Docs(document_id, title_ds, document_tp, created_dt, update_dt, short_ds, created_user_id,
  84. update_user_id, long_ds);
  85. listDocs.add(docs);
  86. }
  87. resultSet.close();
  88. }
  89. statement.close();
  90. }
  91. disconnect();
  92. return listDocs;
  93. }
  94.  
  95. public boolean delete(int document_id) throws SQLException {
  96. String sql = "DELETE FROM document WHERE document_id = ?"; //$NON-NLS-1$
  97. boolean rowDeleted = false;
  98. connect();
  99. try (PreparedStatement statement = this.jdbcConnection.prepareStatement(sql);) {
  100. // PreparedStatement statement = this.jdbcConnection.prepareStatement(sql);
  101. statement.setInt(1, document_id);
  102. rowDeleted = statement.executeUpdate() > 0;
  103. statement.close();
  104. }
  105. disconnect();
  106. return rowDeleted;
  107. }
  108.  
  109. public boolean download(int document_id) throws SQLException {
  110. String sql = "SELECT FROM document WHERE document_id = ?"; //$NON-NLS-1$
  111. boolean rowDownload = false;
  112. connect();
  113. try (PreparedStatement statement = this.jdbcConnection.prepareStatement(sql);) {
  114. statement.setInt(1, document_id);
  115. rowDownload = statement.executeUpdate() > 0;
  116. statement.close();
  117. }
  118. disconnect();
  119. return rowDownload;
  120. }
  121. /*
  122. * public boolean updateDocs(Docs docs) throws SQLException { String sql =
  123. * "UPDATE book SET title = ?, author = ?, price = ?"; sql +=
  124. * " WHERE book_id = ?"; connect();
  125. *
  126. * PreparedStatement statement = jdbcConnection.prepareStatement(sql);
  127. * statement.setString(1, docs.getTitle()); statement.setString(2,
  128. * docs.getAuthor()); statement.setFloat(3, docs.getPrice());
  129. * statement.setInt(4, docs.getId());
  130. *
  131. * boolean rowUpdated = statement.executeUpdate() > 0; statement.close();
  132. * disconnect(); return rowUpdated; }
  133. */
  134.  
  135. /*
  136. * public Files getFiles(int document_id) throws SQLException { Files files =
  137. * null; String sql = "SELECT * FROM file WHERE document_id = ?"; connect();
  138. * PreparedStatement statement = jdbcConnection.prepareStatement(sql);
  139. * statement.setInt(1, document_id); ResultSet resultSet =
  140. * statement.executeQuery(); if (resultSet.next()) { String file_ds =
  141. * resultSet.getString("file_ds"); String first_upload_dt =
  142. * resultSet.getString("first_upload_dt"); String last_upload_dt =
  143. * resultSet.getString("last_upload_dt");
  144. *
  145. * files = new Files(document_id, file_ds, first_upload_dt, last_upload_dt); }
  146. *
  147. * resultSet.close(); statement.close();
  148. *
  149. * return files; }
  150. */
  151. public List<Files> listAllFiles(String document_id) throws SQLException {
  152. List<Files> listFiles = new ArrayList<>();
  153. String sql = "SELECT * FROM file WHERE document_id = ?"; //$NON-NLS-1$
  154. connect();
  155. try (PreparedStatement statement = this.jdbcConnection.prepareStatement(sql);) {
  156. // PreparedStatement statement = this.jdbcConnection.prepareStatement(sql);
  157. statement.setString(1, document_id);
  158. try (ResultSet resultSet = statement.executeQuery();) {
  159. // ResultSet resultSet = statement.executeQuery();
  160.  
  161. while (resultSet.next()) {
  162. String file_id = resultSet.getString("file_id"); //$NON-NLS-1$
  163. String first_upload_user_id = resultSet.getString("first_upload_user_id"); //$NON-NLS-1$
  164. String last_upload_user_id = resultSet.getString("last_upload_user_id"); //$NON-NLS-1$
  165. String file_ds = resultSet.getString("file_ds"); //$NON-NLS-1$
  166. String first_upload_dt = resultSet.getString("first_upload_dt"); //$NON-NLS-1$
  167. String last_upload_dt = resultSet.getString("last_upload_dt"); //$NON-NLS-1$
  168. String content_tp = resultSet.getString("content_tp"); //$NON-NLS-1$
  169. String uuid_fn = resultSet.getString("uuid_fn"); //$NON-NLS-1$
  170. String file_nm = resultSet.getString("file_nm"); //$NON-NLS-1$
  171.  
  172. Files files = new Files(file_id, document_id, first_upload_user_id, last_upload_user_id, file_ds,
  173. first_upload_dt, last_upload_dt, content_tp, uuid_fn, file_nm);
  174. listFiles.add(files);
  175. }
  176. resultSet.close();
  177. }
  178. statement.close();
  179. }
  180. disconnect();
  181. return listFiles;
  182. }
  183.  
  184. public List<Docs> listDoc(String document_id) throws SQLException {
  185. List<Docs> listDoc = new ArrayList<>();
  186. String sql = "SELECT * FROM document WHERE document_id = ?"; //$NON-NLS-1$
  187. connect();
  188. try (PreparedStatement statement = this.jdbcConnection.prepareStatement(sql);) {
  189. // PreparedStatement statement = this.jdbcConnection.prepareStatement(sql);
  190. statement.setString(1, document_id); // MYSQL BIGINT works as a String.
  191. try (ResultSet resultSet = statement.executeQuery();) {
  192. // ResultSet resultSet = statement.executeQuery();
  193.  
  194. while (resultSet.next()) {
  195. String title_ds = resultSet.getString("title_ds"); //$NON-NLS-1$
  196. String document_tp = resultSet.getString("document_tp"); //$NON-NLS-1$
  197. String created_dt = resultSet.getString("created_dt"); //$NON-NLS-1$
  198. String update_dt = resultSet.getString("update_dt"); //$NON-NLS-1$
  199. String short_ds = resultSet.getString("short_ds"); //$NON-NLS-1$
  200. String created_user_id = resultSet.getString("created_user_id"); //$NON-NLS-1$
  201. String update_user_id = resultSet.getString("update_user_id"); //$NON-NLS-1$
  202. String long_ds = resultSet.getString("long_ds"); //$NON-NLS-1$
  203.  
  204. Docs docs = new Docs(document_id, title_ds, document_tp, created_dt, update_dt, short_ds, created_user_id,
  205. update_user_id, long_ds);
  206. listDoc.add(docs);
  207. }
  208. resultSet.close();
  209. }
  210. statement.close();
  211. }
  212. disconnect();
  213. return listDoc;
  214. }
  215.  
  216. public boolean insertFilesInDoc(Files files) throws SQLException {
  217. String sql = "INSERT INTO file (document_id, file_nm, file_ds, uuid_fn, first_upload_dt, last_upload_dt, first_upload_user_id, last_upload_user_id) VALUES (?, ?, ?, ?, ?, ?, ?, ?)"; //$NON-NLS-1$
  218. boolean rowInserted = false;
  219. connect();
  220. try (PreparedStatement statement = this.jdbcConnection.prepareStatement(sql);) {
  221. // PreparedStatement statement = this.jdbcConnection.prepareStatement(sql);
  222. statement.setString(1, files.getDocument_id());
  223. statement.setString(2, files.getFile_nm());
  224. statement.setString(3, files.getFile_ds());
  225. statement.setString(4, files.getUuid_fn());
  226. statement.setString(5, files.getFirst_upload_dt());
  227. statement.setString(6, files.getLast_upload_dt());
  228. statement.setString(7, files.getFirst_upload_user_id());
  229. statement.setString(8, files.getLast_upload_user_id());
  230.  
  231. rowInserted = statement.executeUpdate() > 0;
  232. statement.close();
  233. }
  234. disconnect();
  235. return rowInserted;
  236. }
  237. }
Advertisement
Add Comment
Please, Sign In to add comment