Advertisement
Guest User

Untitled

a guest
Dec 19th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.05 KB | None | 0 0
  1. /**
  2. *
  3. */
  4. package com.mortalpvp.core.global.database;
  5.  
  6. import java.sql.Connection;
  7. import java.sql.DriverManager;
  8. import java.sql.PreparedStatement;
  9. import java.sql.ResultSet;
  10. import java.sql.SQLException;
  11. import java.sql.Statement;
  12.  
  13. /**
  14. * @author Stijn van Nieulande
  15. * @date 16 okt. 2017
  16. * @project MPCore
  17. */
  18. public class MySQLConnection_BACKUP {
  19.  
  20. private MySQLConnectionDetails conn_details = null;
  21. private Connection c = null;
  22.  
  23. /**
  24. * Create a new instance of the MySQLConnection
  25. *
  26. * @param conn_details
  27. * The MySQLConnectionDetails
  28. */
  29. public MySQLConnection_BACKUP(MySQLConnectionDetails conn_details) {
  30. this.conn_details = conn_details;
  31. }
  32.  
  33. /**
  34. * Create a new instance of the MySQLConnection
  35. */
  36. public MySQLConnection_BACKUP() {
  37. String className = new Exception().getStackTrace()[1].getClassName();
  38. if (className.startsWith("me.Stijn.MPCore")) {
  39. this.conn_details = new MySQLConnectionDetails();
  40. } else {
  41. System.err.println("Connection is not made by the Core :/ Please use your own connection details which you can get from the Lead Developer!");
  42. }
  43. }
  44.  
  45. /**
  46. * Open the connection
  47. */
  48. public Connection open() {
  49. if (c != null) {
  50. return c;
  51. } else {
  52. try {
  53. Class.forName("com.mysql.jdbc.Driver");
  54. this.c = DriverManager.getConnection(String.format("jdbc:mysql://%s:%s/%s?autoReconnect=true&useUnicode=true&characterEncoding=utf8&characterSetResults=utf8&connectionCollation=utf8mb4_unicode_ci",
  55. conn_details.getHost(), conn_details.getPort(), conn_details.getDatabase()), conn_details.getUser(), conn_details.getPassword());
  56. return this.c;
  57. } catch (SQLException e) {
  58. System.out.println(String.format("jdbc:mysql://%s:%s/%s?autoReconnect=true&useUnicode=true&characterEncoding=utf8", conn_details.getHost(), conn_details.getPort(), conn_details.getDatabase()));
  59. System.out.println("Could not connect to MySQL server! because: " + e.getMessage());
  60. } catch (ClassNotFoundException e) {
  61. System.out.println("JDBC Driver not found!");
  62. }
  63. }
  64. return null;
  65. }
  66.  
  67. /**
  68. * Get the MySQLConnectionDetails
  69. *
  70. * @return The MySQLConnectionDetails
  71. */
  72. public MySQLConnectionDetails getMySQLConnectionDetails() {
  73. return this.conn_details;
  74. }
  75.  
  76. /**
  77. * Get the connection
  78. *
  79. * @return The connection or null if not opened
  80. */
  81. public Connection getConn() {
  82. return this.c;
  83. }
  84.  
  85. /**
  86. * Close the connection
  87. */
  88. public void close() {
  89. if (this.c != null) {
  90. try {
  91. c.close();
  92. } catch (SQLException e) {
  93. e.printStackTrace();
  94. }
  95. }
  96. c = null;
  97. }
  98.  
  99. /*
  100. * =========================================================================
  101. * Do Query / Update SYNC
  102. * =========================================================================
  103. */
  104.  
  105. /**
  106. * Execute a query
  107. *
  108. * @param sql
  109. * The query string
  110. * @param callback
  111. * The callback
  112. */
  113. public void doQuery(String sql, MySQLConnectionBeforeCloseCallback callback) {
  114. this.open();
  115. ResultSet res = null;
  116.  
  117. try {
  118. Statement s = this.c.createStatement();
  119. res = s.executeQuery(sql);
  120.  
  121. callback.executeBeforeClose(res);
  122.  
  123. res.close();
  124. s.close();
  125. } catch (SQLException ex) {
  126. ex.printStackTrace();
  127. }
  128. try {
  129. c.close();
  130. } catch (SQLException ex) {
  131. ex.printStackTrace();
  132. }
  133. }
  134.  
  135. /**
  136. * Execute a query
  137. *
  138. * @param prepared_statement
  139. * The prepared statement
  140. * @param callback
  141. * The callback
  142. */
  143. public void doQuery(PreparedStatement prepared_statement, MySQLConnectionBeforeCloseCallback callback) {
  144. this.open();
  145. ResultSet res = null;
  146.  
  147. try {
  148. res = prepared_statement.executeQuery();
  149.  
  150. callback.executeBeforeClose(res);
  151.  
  152. res.close();
  153. prepared_statement.close();
  154. } catch (SQLException ex) {
  155. ex.printStackTrace();
  156. }
  157. try {
  158. c.close();
  159. } catch (SQLException ex) {
  160. ex.printStackTrace();
  161. }
  162. }
  163.  
  164. public interface MySQLConnectionBeforeCloseCallback {
  165. void executeBeforeClose(ResultSet result);
  166. }
  167.  
  168. /**
  169. * Execute a update
  170. *
  171. * @param sql
  172. * The sql
  173. * @return Count or -1 if failed
  174. */
  175. public int doUpdate(String sql) {
  176. this.open();
  177. int res = -1;
  178.  
  179. try {
  180. Statement s = c.createStatement();
  181. res = s.executeUpdate(sql);
  182.  
  183. s.close();
  184. } catch (SQLException e1) {
  185. e1.printStackTrace();
  186. }
  187. try {
  188. c.close();
  189. } catch (SQLException ex) {
  190. ex.printStackTrace();
  191. }
  192. return res;
  193. }
  194.  
  195. /**
  196. * Execute a query
  197. *
  198. * @param prepared_statement
  199. * The prepared statement
  200. * @return Count or -1 if failed
  201. */
  202. public int doUpdate(PreparedStatement prepared_statement) {
  203. this.open();
  204. int res = -1;
  205.  
  206. try {
  207. res = prepared_statement.executeUpdate();
  208.  
  209. prepared_statement.close();
  210. } catch (SQLException ex) {
  211. ex.printStackTrace();
  212. }
  213. try {
  214. c.close();
  215. } catch (SQLException ex) {
  216. ex.printStackTrace();
  217. }
  218. return res;
  219. }
  220.  
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement