Advertisement
SalehKasem

MySQLConnection

Feb 1st, 2019
388
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.28 KB | None | 0 0
  1. package server;
  2.  
  3. import java.io.InputStream;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.PreparedStatement;
  7. import java.sql.ResultSet;
  8. import java.sql.ResultSetMetaData;
  9. import java.sql.SQLException;
  10. import java.sql.Statement;
  11. import java.util.ArrayList;
  12.  
  13. import entities.Account;
  14. import entities.UserAccount;
  15.  
  16. public class MySQLConnection {
  17. final String DATABASE_URL = "jdbc:mysql://localhost/";
  18. private Connection conn;
  19. private String schema;
  20.  
  21. public MySQLConnection(String schema, String userName, String password) {
  22. try {
  23. Class.forName("com.mysql.jdbc.Driver").newInstance();
  24. } catch (Exception ex) {
  25. /* handle the error */}
  26.  
  27. try {
  28. this.schema = schema;
  29. conn = DriverManager.getConnection(DATABASE_URL + schema, userName, password);
  30. System.out.println("SQL connection succeed");
  31. } catch (SQLException ex) {/* handle any errors */
  32. System.out.println("SQLException: " + ex.getMessage());
  33. System.out.println("SQLState: " + ex.getSQLState());
  34. System.out.println("VendorError: " + ex.getErrorCode());
  35. System.exit(0);
  36. }
  37. }
  38.  
  39. public Object executeQuery(Object msg) {
  40. String query;
  41. try {
  42. if (msg instanceof ArrayList) {
  43. ArrayList<String> arr = (ArrayList<String>) msg;
  44. query = String.valueOf(arr.get(arr.size() - 1));
  45. if (query.startsWith("INSERT") || query.startsWith("UPDATE") || query.startsWith("DELETE")) {
  46. int i;
  47. PreparedStatement ps = conn.prepareStatement(query);
  48. for (i = 0; i < arr.size() - 1; i++) {
  49. ps.setString(i + 1, arr.get(i));
  50. }
  51. ps.executeUpdate();
  52. System.out.println("DB: Query => Executed Successfully");
  53. return null;
  54. } else
  55. return executeSelectQuery(msg);
  56.  
  57. } else if (msg instanceof String) {
  58. query = msg.toString();
  59. if (query.startsWith("INSERT") || query.startsWith("UPDATE") || query.startsWith("DELETE")) {
  60. Statement stmt = conn.createStatement();
  61. stmt.executeUpdate(query);
  62. System.out.println("DB: " + query + " => Executed Successfully");
  63. return null;
  64. } else
  65. return executeSelectQuery(msg);
  66. }
  67.  
  68. } catch (SQLException sqlException) {
  69. System.out.println("Couldn't execute query");
  70. sqlException.printStackTrace();
  71. return null;
  72. }
  73. return null;
  74. }
  75.  
  76. public Object executeSelectQuery(Object msg) {
  77. String query = msg.toString();
  78. try {
  79. Statement stmt = conn.createStatement();
  80. ResultSet rs = stmt.executeQuery(query);
  81. System.out.println("DB: " + query + " => Executed Successfully");
  82. return parseResultSet(rs);
  83.  
  84. } catch (SQLException sqlException) {
  85. System.out.println("Couldn't execute query");
  86. sqlException.printStackTrace();
  87. return null;
  88. }
  89. }
  90.  
  91. /**
  92. * save the file in bookContentsFile table
  93. * @param inputStream
  94. * @param id
  95. */
  96. public void updateFile(InputStream inputStream, int id) {
  97. //String sql = "INSERT INTO BookContentsFile (upload_file) values (?)";
  98. String sql = "UPDATE Book SET tableOfContents =? WHERE bookID = '" + id + "';";
  99. try {
  100. PreparedStatement statement = conn.prepareStatement(sql);
  101. statement.setBinaryStream(1, inputStream);
  102. statement.executeUpdate();
  103. } catch (SQLException e) {
  104. e.printStackTrace();
  105. }
  106. //updateBookTableOfContents(id);
  107. }
  108.  
  109. /**
  110. * Parse database result set into an ArrayList with rows separated by commas
  111. *
  112. * @param rs
  113. * @return ArrayList<String>
  114. */
  115. public ArrayList<String> parseResultSet(ResultSet rs) {
  116. ArrayList<String> arr = new ArrayList<>();
  117. int i;
  118.  
  119. try {
  120. ResultSetMetaData rsmd = rs.getMetaData();
  121. while (rs.next()) {
  122. i = 1;
  123. while (i <= rsmd.getColumnCount()) {
  124. arr.add(rs.getString(i++));
  125. }
  126. }
  127. } catch (SQLException Exception) {
  128. System.out.println("ERROR while parsing array!");
  129. }
  130. return arr;
  131. }
  132.  
  133. public ResultSet executeFileQuery(int bookID) {
  134. String query = "SELECT tableOfContents FROM Book WHERE bookID = '" + bookID + "';";
  135. try {
  136. Statement stmt = conn.createStatement();
  137. ResultSet rs = stmt.executeQuery(query);
  138. System.out.println("DB: " + query + " => Executed Successfully");
  139. return rs;
  140. } catch (SQLException sqlException) {
  141. System.out.println("Couldn't execute query");
  142. sqlException.printStackTrace();
  143. return null;
  144. }
  145. }
  146.  
  147. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement