Advertisement
Guest User

Untitled

a guest
Oct 10th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.63 KB | None | 0 0
  1. package com.alta189.sqllibrary.mysql;
  2.  
  3. import java.net.MalformedURLException;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.ResultSet;
  7. import java.sql.SQLException;
  8. import java.sql.Statement;
  9.  
  10. public class DatabaseHandler {
  11. private mysqlCore core;
  12. private Connection connection;
  13. private String dblocation;
  14. private String username;
  15. private String password;
  16. private String database;
  17.  
  18.  
  19. public DatabaseHandler(mysqlCore core, String dbLocation, String database, String username, String password) {
  20. this.core = core;
  21. this.dblocation = dbLocation;
  22. this.database = database;
  23. this.username = username;
  24. this.password = password;
  25. }
  26.  
  27. private void openConnection() throws MalformedURLException, InstantiationException, IllegalAccessException {
  28. try {
  29. Class.forName("com.mysql.jdbc.Driver");
  30. connection = DriverManager.getConnection("jdbc:mysql://" + dblocation + "/" + database, username, password);
  31. } catch (ClassNotFoundException e) {
  32. core.writeError("ClassNotFoundException! " + e.getMessage(), true);
  33. } catch (SQLException e) {
  34. core.writeError("SQLException! " + e.getMessage(), true);
  35. }
  36. }
  37.  
  38. public Boolean checkConnection() {
  39. if (connection == null) {
  40. try {
  41. openConnection();
  42. return true;
  43. } catch (MalformedURLException ex) {
  44. core.writeError("MalformedURLException! " + ex.getMessage(), true);
  45. } catch (InstantiationException ex) {
  46. core.writeError("InstantiationExceptioon! " + ex.getMessage(), true);
  47. } catch (IllegalAccessException ex) {
  48. core.writeError("IllegalAccessException! " + ex.getMessage(), true);
  49. }
  50. return false;
  51. }
  52. return true;
  53. }
  54.  
  55. public void closeConnection() {
  56. try {
  57. if (connection != null)
  58. connection.close();
  59. } catch (Exception e) {
  60. core.writeError("Failed to close database connection! " + e.getMessage(), true);
  61. }
  62. }
  63.  
  64. public Connection getConnection() throws MalformedURLException, InstantiationException, IllegalAccessException {
  65. if (connection == null) {
  66. openConnection();
  67. }
  68. return connection;
  69. }
  70.  
  71. public ResultSet sqlQuery(String query) throws MalformedURLException, InstantiationException, IllegalAccessException {
  72. try {
  73. Connection connection = getConnection();
  74. Statement statement = connection.createStatement();
  75.  
  76. ResultSet result = statement.executeQuery(query);
  77.  
  78. return result;
  79. } catch (SQLException ex) {
  80. core.writeError("Error at SQL Query: " + ex.getMessage(), false);
  81. }
  82. return null;
  83. }
  84.  
  85. public void insertQuery(String query) throws MalformedURLException, InstantiationException, IllegalAccessException {
  86. try {
  87. Connection connection = getConnection();
  88. Statement statement = connection.createStatement();
  89.  
  90. statement.executeUpdate(query);
  91.  
  92.  
  93. } catch (SQLException ex) {
  94.  
  95. if (!ex.toString().contains("not return ResultSet")) core.writeError("Error at SQL INSERT Query: " + ex, false);
  96.  
  97.  
  98. }
  99. }
  100.  
  101. public void updateQuery(String query) throws MalformedURLException, InstantiationException, IllegalAccessException {
  102. try {
  103. Connection connection = getConnection();
  104. Statement statement = connection.createStatement();
  105.  
  106. statement.executeUpdate(query);
  107.  
  108.  
  109. } catch (SQLException ex) {
  110.  
  111. if (!ex.toString().contains("not return ResultSet")) core.writeError("Error at SQL UPDATE Query: " + ex, false);
  112.  
  113. }
  114. }
  115.  
  116. public void deleteQuery(String query) throws MalformedURLException, InstantiationException, IllegalAccessException {
  117. try {
  118. Connection connection = getConnection();
  119. Statement statement = connection.createStatement();
  120.  
  121. statement.executeUpdate(query);
  122.  
  123.  
  124. } catch (SQLException ex) {
  125.  
  126. if (!ex.toString().contains("not return ResultSet")) core.writeError("Error at SQL DELETE Query: " + ex, false);
  127.  
  128. }
  129. }
  130.  
  131. public Boolean checkTable(String table) throws MalformedURLException, InstantiationException, IllegalAccessException {
  132. try {
  133. Connection connection = getConnection();
  134. Statement statement = connection.createStatement();
  135.  
  136. ResultSet result = statement.executeQuery("SELECT * FROM " + table);
  137.  
  138. if (result == null) return false;
  139. if (result != null) return true;
  140. } catch (SQLException ex) {
  141. if (ex.getMessage().contains("exist")) {
  142. return false;
  143. } else {
  144. core.writeError("Error at SQL Query: " + ex.getMessage(), false);
  145. }
  146. }
  147.  
  148.  
  149. if (sqlQuery("SELECT * FROM " + table) == null) return true;
  150. return false;
  151. }
  152.  
  153. public Boolean wipeTable(String table) throws MalformedURLException, InstantiationException, IllegalAccessException {
  154. try {
  155. if (!core.checkTable(table)) {
  156. core.writeError("Error at Wipe Table: table, " + table + ", does not exist", true);
  157. return false;
  158. }
  159. Connection connection = getConnection();
  160. Statement statement = connection.createStatement();
  161. String query = "DELETE FROM " + table + ";";
  162. statement.executeUpdate(query);
  163.  
  164. return true;
  165. } catch (SQLException ex) {
  166. if (!ex.toString().contains("not return ResultSet")) core.writeError("Error at SQL WIPE TABLE Query: " + ex, false);
  167. return false;
  168. }
  169. }
  170.  
  171. public Boolean createTable(String query) {
  172. try {
  173. if (query == null) { core.writeError("SQL Create Table query empty.", true); return false; }
  174.  
  175. Statement statement = connection.createStatement();
  176. statement.execute(query);
  177. return true;
  178. } catch (SQLException ex){
  179. core.writeError(ex.getMessage(), true);
  180. return false;
  181. }
  182. }
  183. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement