Advertisement
Guest User

Untitled

a guest
Aug 15th, 2017
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.83 KB | None | 0 0
  1. I originally wrote, and released this for Hyperion, on google. Seeing I decided to just do a major rewrite of RSCD instead of using Hyperion, I figured I'd release it here...
  2.  
  3. Note: this is not everything you need, you'll need to add some variables to your Config class, and initialize it somewhere.
  4.  
  5. Code: [Select]
  6. package org.rscdaemon.server.net;
  7.  
  8. import java.util.concurrent.ConcurrentLinkedQueue;
  9.  
  10. import org.rscdaemon.server.util.Config;
  11.  
  12. /**
  13. *
  14. * @author ICodeForFame
  15. *
  16. */
  17. public class DatabaseManager {
  18.  
  19. private static ConcurrentLinkedQueue<DatabaseConnection> conns = new ConcurrentLinkedQueue<DatabaseConnection>();
  20.  
  21. static {
  22. driverTest();
  23. }
  24.  
  25. public DatabaseManager() {
  26. for (int i = 0; i < Config.dbConns; i++)
  27. addNewDatabaseConnection();
  28. }
  29.  
  30. private static void driverTest() {
  31. try {
  32. Class.forName("com.mysql.jdbc.Driver");
  33. } catch (ClassNotFoundException e) {
  34. e.printStackTrace();
  35. }
  36. }
  37.  
  38. public DatabaseConnection addNewDatabaseConnection() {
  39. String pass = Config.dbPass;
  40. DatabaseConnection conn = new DatabaseConnection();
  41. conns.add(conn);
  42. return conn;
  43. }
  44.  
  45. public DatabaseConnection getDatabaseConnection() {
  46. return conns.poll();
  47. }
  48.  
  49. public void returnDatabaseConnection(DatabaseConnection conn) {
  50. conns.offer(conn);
  51. }
  52. }
  53.  
  54. Code: [Select]
  55. package org.rscdaemon.server.net;
  56.  
  57. import java.sql.DriverManager;
  58. import java.sql.Connection;
  59. import java.sql.ResultSet;
  60. import java.sql.SQLException;
  61. import java.sql.Statement;
  62. import java.util.logging.Logger;
  63.  
  64. import org.rscdaemon.server.util.Config;
  65.  
  66. /**
  67. * Used to interact with the database.
  68. *
  69. * @author ICodeForFame
  70. */
  71. public class DatabaseConnection {
  72.  
  73. /**
  74. * The logger for this class
  75. */
  76. final static Logger logger = Logger.getLogger(DatabaseConnection.class
  77. .getName());
  78.  
  79. /**
  80. * The database connection in use
  81. */
  82. private Connection con;
  83. /**
  84. * A statement for running queries on
  85. */
  86. private Statement statement;
  87. /**
  88. * The last query being executed
  89. */
  90. private String lastQuery;
  91.  
  92. static {
  93. testForDriver();
  94. }
  95.  
  96. /**
  97. * Tests we have a mysql Driver
  98. */
  99. private static void testForDriver() {
  100. try {
  101. Class.forName("com.mysql.jdbc.Driver");
  102. } catch (ClassNotFoundException cnfe) {
  103. logger.fine("Class not found exception");
  104. }
  105. }
  106.  
  107. /**
  108. * Instantiates a new database connection
  109. */
  110. public DatabaseConnection() {
  111. if (!createConnection()) {
  112. logger.fine("Unable to connect to MySQL");
  113. }
  114. }
  115.  
  116. public boolean createConnection() {
  117. try {
  118. con = DriverManager.getConnection("jdbc:mysql://" + Config.dbHost + ":"
  119. + Config.dbPort + "/" + Config.dbName, Config.dbUser, Config.dbPass);
  120. statement = con.createStatement();
  121. statement.setEscapeProcessing(true);
  122. return isConnected();
  123. } catch (SQLException e) {
  124. logger.fine(e.getMessage());
  125. return false;
  126. }
  127. }
  128.  
  129. public boolean isConnected() {
  130. try {
  131. return !con.isClosed();
  132. } catch (SQLException e) {
  133. e.printStackTrace();
  134. }
  135. return false;
  136. }
  137.  
  138. /**
  139. * Runs a select query on the current database connection
  140. *
  141. * @param s
  142. * The query to be ran
  143. */
  144. public ResultSet getQuery(String q) throws SQLException {
  145. try {
  146. lastQuery = q;
  147. return statement.executeQuery(q);
  148. } catch (SQLException e) {
  149. if (!isConnected() && createConnection()) {
  150. return getQuery(q);
  151. }
  152. throw new SQLException(e.getMessage() + ": '" + lastQuery + "'", e
  153. .getSQLState(), e.getErrorCode());
  154. }
  155. }
  156.  
  157. /**
  158. * Runs a update/insert/replace query on the current database connection
  159. *
  160. * @param s
  161. * The query to be ran
  162. */
  163. public int updateQuery(String q) throws SQLException {
  164. try {
  165. lastQuery = q;
  166. return statement.executeUpdate(q);
  167. } catch (SQLException e) {
  168. if (!isConnected() && createConnection()) {
  169. return updateQuery(q);
  170. }
  171. throw new SQLException(e.getMessage() + ": '" + lastQuery + "'", e
  172. .getSQLState(), e.getErrorCode());
  173. }
  174. }
  175.  
  176. /**
  177. * Closes the database conection.
  178. *
  179. * @throws SQLException
  180. * if there was an error when closing the connection
  181. */
  182. public void close() throws SQLException {
  183. con.close();
  184. con = null;
  185. }
  186. }
  187.  
  188. Edit: Updated the isConnected method.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement