Advertisement
Guest User

Untitled

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