Advertisement
Guest User

Untitled

a guest
Sep 24th, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.41 KB | None | 0 0
  1. package server.util;
  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 {
  10. /**
  11. * Define MySQL connection info. XAMPP user/pass. Make sure to add a remote
  12. * user with gator1022.hostgator.com as localhost.
  13. */
  14. static final String host = "";
  15. static final String db = "";// database name
  16. static final String user = ""; // database user
  17. static final String pass = ""; // database passwd
  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 + ":"
  61. + port + "/" + db, user, pass);
  62. statement = con.createStatement();
  63. statement.setEscapeProcessing(true);
  64. return isConnected();
  65. } catch (SQLException e) {
  66. e.printStackTrace();
  67. return false;
  68. }// return true;//soz lol I disabled it so i could run XD
  69. }
  70.  
  71. public boolean isConnected() {
  72. try {
  73. statement.executeQuery("SELECT CURRENT_DATE");
  74. return true;
  75. } catch (SQLException e) {
  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 + "'",
  90. e.getSQLState(), e.getErrorCode());
  91. }
  92. }
  93.  
  94. /**
  95. * Runs a select query on the current database connection
  96. *
  97. * @param s
  98. * The query to be ran
  99. */
  100. public ResultSet getQuery(String q) throws SQLException {
  101. try {
  102. lastQuery = q;
  103. return statement.executeQuery(q);
  104. } catch (SQLException e) {
  105. if (!isConnected() && createConnection()) {
  106. return getQuery(q);
  107. }
  108. throw new SQLException(e.getMessage() + ": '" + lastQuery + "'",
  109. e.getSQLState(), e.getErrorCode());
  110. }
  111. }
  112.  
  113. /**
  114. * Runs a update/insert/replace query on the current database connection
  115. *
  116. * @param s
  117. * The query to be ran
  118. */
  119. public int updateQuery(String q) throws SQLException {
  120. try {
  121. lastQuery = q;
  122. return statement.executeUpdate(q);
  123. } catch (SQLException e) {
  124. if (!isConnected() && createConnection()) {
  125. return updateQuery(q);
  126. }
  127. throw new SQLException(e.getMessage() + ": '" + lastQuery + "'",
  128. e.getSQLState(), e.getErrorCode());
  129. }
  130. }
  131.  
  132. /**
  133. * Closes the database conection.
  134. *
  135. * @throws SQLException
  136. * if there was an error when closing the connection
  137. */
  138. public void close() throws SQLException {
  139. con.close();
  140. con = null;
  141. }
  142.  
  143. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement