Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 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...
- Note: this is not everything you need, you'll need to add some variables to your Config class, and initialize it somewhere.
- Code: [Select]
- package org.rscdaemon.server.net;
- import java.util.concurrent.ConcurrentLinkedQueue;
- import org.rscdaemon.server.util.Config;
- /**
- *
- * @author ICodeForFame
- *
- */
- public class DatabaseManager {
- private static ConcurrentLinkedQueue<DatabaseConnection> conns = new ConcurrentLinkedQueue<DatabaseConnection>();
- static {
- driverTest();
- }
- public DatabaseManager() {
- for (int i = 0; i < Config.dbConns; i++)
- addNewDatabaseConnection();
- }
- private static void driverTest() {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- public DatabaseConnection addNewDatabaseConnection() {
- String pass = Config.dbPass;
- DatabaseConnection conn = new DatabaseConnection();
- conns.add(conn);
- return conn;
- }
- public DatabaseConnection getDatabaseConnection() {
- return conns.poll();
- }
- public void returnDatabaseConnection(DatabaseConnection conn) {
- conns.offer(conn);
- }
- }
- Code: [Select]
- package org.rscdaemon.server.net;
- import java.sql.DriverManager;
- import java.sql.Connection;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.logging.Logger;
- import org.rscdaemon.server.util.Config;
- /**
- * Used to interact with the database.
- *
- * @author ICodeForFame
- */
- public class DatabaseConnection {
- /**
- * The logger for this class
- */
- final static Logger logger = Logger.getLogger(DatabaseConnection.class
- .getName());
- /**
- * The database connection in use
- */
- private Connection con;
- /**
- * A statement for running queries on
- */
- private Statement statement;
- /**
- * The last query being executed
- */
- private String lastQuery;
- static {
- testForDriver();
- }
- /**
- * Tests we have a mysql Driver
- */
- private static void testForDriver() {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- } catch (ClassNotFoundException cnfe) {
- logger.fine("Class not found exception");
- }
- }
- /**
- * Instantiates a new database connection
- */
- public DatabaseConnection() {
- if (!createConnection()) {
- logger.fine("Unable to connect to MySQL");
- }
- }
- public boolean createConnection() {
- try {
- con = DriverManager.getConnection("jdbc:mysql://" + Config.dbHost + ":"
- + Config.dbPort + "/" + Config.dbName, Config.dbUser, Config.dbPass);
- statement = con.createStatement();
- statement.setEscapeProcessing(true);
- return isConnected();
- } catch (SQLException e) {
- logger.fine(e.getMessage());
- return false;
- }
- }
- public boolean isConnected() {
- try {
- return !con.isClosed();
- } catch (SQLException e) {
- e.printStackTrace();
- }
- return false;
- }
- /**
- * Runs a select query on the current database connection
- *
- * @param s
- * The query to be ran
- */
- public ResultSet getQuery(String q) throws SQLException {
- try {
- lastQuery = q;
- return statement.executeQuery(q);
- } catch (SQLException e) {
- if (!isConnected() && createConnection()) {
- return getQuery(q);
- }
- throw new SQLException(e.getMessage() + ": '" + lastQuery + "'", e
- .getSQLState(), e.getErrorCode());
- }
- }
- /**
- * Runs a update/insert/replace query on the current database connection
- *
- * @param s
- * The query to be ran
- */
- public int updateQuery(String q) throws SQLException {
- try {
- lastQuery = q;
- return statement.executeUpdate(q);
- } catch (SQLException e) {
- if (!isConnected() && createConnection()) {
- return updateQuery(q);
- }
- throw new SQLException(e.getMessage() + ": '" + lastQuery + "'", e
- .getSQLState(), e.getErrorCode());
- }
- }
- /**
- * Closes the database conection.
- *
- * @throws SQLException
- * if there was an error when closing the connection
- */
- public void close() throws SQLException {
- con.close();
- con = null;
- }
- }
- Edit: Updated the isConnected method.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement