Advertisement
Guest User

Untitled

a guest
Sep 21st, 2017
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.19 KB | None | 0 0
  1. package org.dementhium.mysql;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.ResultSet;
  6. import java.sql.SQLException;
  7. import java.sql.Statement;
  8.  
  9. public class DatabaseConnection extends DatabaseFunctions {
  10. /*
  11. * Define MySQL connection info. XAMPP user/pass.
  12. */
  13. static final String host = "";
  14. static final String db = "";
  15. static final String user = "";
  16. static final String pass = "";
  17. static final String port = "3306";
  18.  
  19. /**
  20. * The database connection in use
  21. */
  22. private Connection con;
  23. /**
  24. * A statement for running queries on
  25. */
  26. private Statement statement;
  27. /**
  28. * The last query being executed
  29. */
  30. private String lastQuery;
  31.  
  32. static {
  33. testForDriver();
  34. }
  35.  
  36. /**
  37. * Tests we have a mysql Driver
  38. */
  39. private static void testForDriver() {
  40. try {
  41. Class.forName("com.mysql.jdbc.Driver");
  42. } catch (ClassNotFoundException cnfe) {
  43. System.out.println("Class not found exception");
  44. }
  45. }
  46.  
  47. /**
  48. * Instantiates a new database connection
  49. */
  50. public DatabaseConnection() {
  51. if (!createConnection()) {
  52. System.out.println("Unable to connect to MySQL");
  53. System.exit(1);
  54. }
  55. }
  56.  
  57. public boolean createConnection() {
  58. try {
  59. con = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + db, user, pass);
  60. statement = con.createStatement();
  61. statement.setEscapeProcessing(true);
  62. return isConnected();
  63. } catch (SQLException e) {
  64. e.printStackTrace();
  65. return false;
  66. }//return true;//soz lol I disabled it so i could run XD
  67. }
  68.  
  69. public boolean isConnected() {
  70. try {
  71. statement.executeQuery("SELECT CURRENT_DATE");
  72. return true;
  73. } catch (SQLException e) {
  74. e.printStackTrace();
  75. return false;
  76. }
  77. }
  78.  
  79. public int newQuery(String q) throws SQLException {
  80. try {
  81. Statement tempStatement = con.createStatement();
  82. lastQuery = q;
  83. return tempStatement.executeUpdate(q);
  84. } catch (SQLException e) {
  85. if (!isConnected() && createConnection()) {
  86. return updateQuery(q);
  87. }
  88. throw new SQLException(e.getMessage() + ": '" + lastQuery + "'", e
  89. .getSQLState(), e.getErrorCode());
  90. }
  91. }
  92. /**
  93. * Runs a select query on the current database connection
  94. *
  95. * @param s
  96. * The query to be ran
  97. */
  98. public ResultSet getQuery(String q) throws SQLException {
  99. try {
  100. lastQuery = q;
  101. return statement.executeQuery(q);
  102. } catch (SQLException e) {
  103. if (!isConnected() && createConnection()) {
  104. return getQuery(q);
  105. }
  106. throw new SQLException(e.getMessage() + ": '" + lastQuery + "'", e.getSQLState(), e.getErrorCode());
  107. }
  108. }
  109.  
  110. /**
  111. * Runs a update/insert/replace query on the current database connection
  112. *
  113. * @param s
  114. * The query to be ran
  115. */
  116. public int updateQuery(String q) throws SQLException {
  117. try {
  118. lastQuery = q;
  119. return statement.executeUpdate(q);
  120. } catch (SQLException e) {
  121. if (!isConnected() && createConnection()) {
  122. return updateQuery(q);
  123. }
  124. throw new SQLException(e.getMessage() + ": '" + lastQuery + "'", e
  125. .getSQLState(), e.getErrorCode());
  126. }
  127. }
  128.  
  129. /**
  130. * Closes the database conection.
  131. *
  132. * @throws SQLException
  133. * if there was an error when closing the connection
  134. */
  135. public void close() throws SQLException {
  136. con.close();
  137. con = null;
  138. }
  139.  
  140. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement