Advertisement
Guest User

Untitled

a guest
May 16th, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.45 KB | None | 0 0
  1. package appServerUtil;
  2.  
  3. import java.io.File;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.InputStream;
  7. import java.sql.Blob;
  8. import java.sql.Connection;
  9. import java.sql.DriverManager;
  10. import java.sql.PreparedStatement;
  11. import java.sql.ResultSet;
  12. import java.sql.SQLException;
  13. import java.sql.Statement;
  14. import java.util.concurrent.atomic.AtomicBoolean;
  15. import java.util.logging.Level;
  16. import java.util.logging.Logger;
  17.  
  18. /*
  19. * To change this license header, choose License Headers in Project Properties.
  20. * To change this template file, choose Tools | Templates
  21. * and open the template in the editor.
  22. */
  23. /**
  24. *
  25. * @author stefano
  26. */
  27. public class DBConnector {
  28.  
  29. private final static String CONNECTION_URL = "jdbc:mysql://93.42.227.195:3306/locale?serverTimezone=UTC&characterEncoding=UTF-8";
  30. private final static String USER = "";
  31. private final static String PASSWORD = "";
  32. private Connection connection;
  33. private Statement stmt;
  34. private String exception = "";
  35.  
  36. public DBConnector() {
  37. try {
  38. Class.forName("com.mysql.cj.jdbc.Driver");
  39. connection = DriverManager.getConnection(CONNECTION_URL, USER, PASSWORD);
  40. stmt = connection.createStatement();
  41.  
  42. } catch (ClassNotFoundException | SQLException ex) {
  43. exception = ex.getMessage();
  44. System.err.println(ex.getMessage());
  45. //Logger.getLogger(DBConnector.class.getName()).log(Level.SEVERE, null, ex);
  46. }// end try-catch
  47. }
  48.  
  49. public String checkDatabase() {
  50. return "CONNECTION=" + (connection == null ? "null" : connection) + "\nSTATEMENT=" + (stmt == null ? "null" : stmt) + "\n" + exception;
  51. }
  52.  
  53. public ResultSet executeQuery(final String query) {
  54. if (stmt == null) {
  55. return null;
  56. }
  57. try {
  58. return stmt.executeQuery(query);
  59. } catch (SQLException ex) {
  60. System.err.println(ex.getMessage());
  61. Logger.getLogger(DBConnector.class.getName()).log(Level.SEVERE, null, ex);
  62. return null;
  63. }//end try-catch
  64. }
  65.  
  66. public boolean execute(final String query) {
  67. try {
  68. return stmt.execute(query);
  69. } catch (SQLException ex) {
  70. System.err.println(ex.getMessage());
  71. Logger.getLogger(DBConnector.class.getName()).log(Level.SEVERE, null, ex);
  72. return false;
  73. }
  74. }
  75.  
  76. public void writeBlob(String table, int rowId, InputStream input) {
  77. // update sql
  78. String updateSQL = "UPDATE " + table + " "
  79. + "SET file = ? "
  80. + "WHERE id=?";
  81.  
  82. try (PreparedStatement pstmt = connection.prepareStatement(updateSQL)) {
  83. pstmt.setBinaryStream(1, input);
  84. pstmt.setInt(2, rowId);
  85. pstmt.executeUpdate();
  86. } catch (SQLException e) {
  87. System.out.println(e.getMessage());
  88. }
  89. }
  90.  
  91. public InputStream getBlobInputStream(ResultSet resultSet, int index){
  92. try {
  93. InputStream binaryStream = resultSet.getBinaryStream(index);
  94. return binaryStream;
  95. } catch (SQLException ex) {
  96. System.out.println(ex.getMessage());
  97. }
  98. return null;
  99. }
  100.  
  101. public long getBlobSize(ResultSet resultSet, String name){
  102. try {
  103. Blob blob = resultSet.getBlob(name);
  104. return blob.length();
  105. } catch (SQLException ex) {
  106. System.out.println(ex.getMessage());
  107. }
  108. return 0;
  109. }
  110.  
  111. public void close() {
  112. try {
  113. stmt.close();
  114. connection.close();
  115. //stmt = null;
  116. //connection = null;
  117. } catch (SQLException ex) {
  118. Logger.getLogger(DBConnector.class.getName()).log(Level.SEVERE, null, ex);
  119. }
  120. }
  121.  
  122. @Override
  123. public void finalize() {
  124. try {
  125. super.finalize();
  126. close();
  127. } catch (Throwable ex) {
  128. }//end try-catch
  129. }
  130.  
  131. public PreparedStatement getPreparedStatement(final String sqlQuery){
  132. try {
  133. PreparedStatement pstmt = connection.prepareStatement(sqlQuery);
  134. return pstmt;
  135. } catch (SQLException ex) {
  136. Logger.getLogger(DBConnector.class.getName()).log(Level.SEVERE, null, ex);
  137. }
  138. return null;
  139. }
  140.  
  141.  
  142.  
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement