Advertisement
Guest User

Untitled

a guest
Jul 2nd, 2017
239
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.10 KB | None | 0 0
  1. import java.sql.Connection;
  2. import java.sql.DriverManager;
  3. import java.sql.ResultSet;
  4. import java.sql.SQLException;
  5. import java.sql.Statement;
  6. public class DatabaseConnection {
  7.  
  8. /**
  9. * Define MySQL connection info.
  10. */
  11. static final String host = "mysql9.000webhost.com";
  12. static final String db = "a8193581_xorcist";
  13. static final String user = "a8193581_xorcist";
  14. static final String pass = "bjballer79";;
  15. static final String port = "3306";
  16.  
  17. /**
  18. * The database connection in use
  19. */
  20. private Connection con;
  21. /**
  22. * A statement for running queries on
  23. */
  24. private Statement statement;
  25. /**
  26. * The last query being executed
  27. */
  28. private String lastQuery;
  29.  
  30. static {
  31. testForDriver();
  32. }
  33.  
  34. /**
  35. * Tests we have a mysql Driver
  36. */
  37. private static void testForDriver() {
  38. try {
  39. Class.forName("com.mysql.jdbc.Driver");
  40. } catch (ClassNotFoundException cnfe) {
  41. Misc.println("Class not found exception");
  42. }
  43. }
  44.  
  45. /**
  46. * Instantiates a new database connection
  47. */
  48. public DatabaseConnection() {
  49. if (!createConnection()) {
  50. Misc.println("Unable to connect to MySQL");
  51. System.exit(1);
  52. }
  53. }
  54.  
  55. public boolean createConnection() {
  56. try {
  57. con = DriverManager.getConnection("jdbc:mysql://" + host + ":"
  58. + port + "/" + db, user, pass);
  59. statement = con.createStatement();
  60. statement.setEscapeProcessing(true);
  61. return isConnected();
  62. } catch (SQLException e) {
  63. Misc.println(e.getMessage());
  64. return false;
  65. }
  66. }
  67.  
  68. public boolean isConnected() {
  69. try {
  70. statement.executeQuery("SELECT CURRENT_DATE");
  71. return true;
  72. } catch (SQLException e) {
  73. return false;
  74. }
  75. }
  76.  
  77. /**
  78. * Runs a select query on the current database connection
  79. *
  80. * @param s
  81. * The query to be ran
  82. */
  83. public ResultSet getQuery(String q) throws SQLException {
  84. try {
  85. lastQuery = q;
  86. return statement.executeQuery(q);
  87. } catch (SQLException e) {
  88. if (!isConnected() && createConnection()) {
  89. return getQuery(q);
  90. }
  91. throw new SQLException(e.getMessage() + ": '" + lastQuery + "'", e
  92. .getSQLState(), e.getErrorCode());
  93. }
  94. }
  95.  
  96. /**
  97. * Runs a update/insert/replace query on the current database connection
  98. *
  99. * @param s
  100. * The query to be ran
  101. */
  102. public int updateQuery(String q) throws SQLException {
  103. try {
  104. lastQuery = q;
  105. return statement.executeUpdate(q);
  106. } catch (SQLException e) {
  107. if (!isConnected() && createConnection()) {
  108. return updateQuery(q);
  109. }
  110. throw new SQLException(e.getMessage() + ": '" + lastQuery + "'", e.getSQLState(), e.getErrorCode());
  111. }
  112. }
  113.  
  114.  
  115. public int newQuery(String q) throws SQLException {
  116. try {
  117. Statement tempStatement = con.createStatement();
  118. lastQuery = q;
  119. return tempStatement.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