Advertisement
mojo2016

Untitled

Oct 14th, 2016
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.39 KB | None | 0 0
  1. //package model;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.util.Properties;
  8. import java.sql.PreparedStatement;
  9. import com.mysql.jdbc.Driver;
  10.  
  11. /**
  12.  * A somewhat generic class used for establishing connections to a
  13.  * MySQL database.
  14.  * @author Josh Monague
  15.  */
  16. public class DatabaseAccess
  17. {
  18.     private Properties connectionProperties;
  19.     private final String connectionString;
  20.     private Connection connection;
  21.    
  22.     /**
  23.      * The recent SQLException. Set to NULL if a method successfully interacts with the database.
  24.      */
  25.     protected SQLException sql_error;
  26.     /**
  27.      * The database table where data is read/written
  28.      */
  29.     protected String tableName;
  30.    
  31.     /**
  32.      * Assumes that the database is located under localhost.
  33.      * @param db - The name of the database
  34.      * @param userName - The user's name
  35.      * @param password - The user's password
  36.      */
  37.     public DatabaseAccess(String db, String userName, String password)
  38.     {
  39.         connectionString = String.format("jdbc:mysql://localhost/%s", db);
  40.         connectionProperties = new Properties();
  41.         connectionProperties.put("user", userName);
  42.         connectionProperties.put("password", password);
  43.     }
  44.    
  45.     /**
  46.      * Use this constructor if the database is not local to the current machine.
  47.      * @param db - The name of the database
  48.      * @param dbIPaddress - The network IP of the database
  49.      * @param userName - The user's name
  50.      * @param password - The user's password
  51.      */
  52.     public DatabaseAccess(String db, String dbIPaddress, String userName, String password)
  53.     {
  54.         connectionString = String.format("jdbc:mysql://%s/%s", dbIPaddress, db);
  55.         connectionProperties = new Properties();
  56.         connectionProperties.put("user", userName);
  57.         connectionProperties.put("password", password);
  58.     }
  59.    
  60.     /**
  61.      * Attempts to establish a connection to the MySQL database.
  62.      * Clears recent sql error on success. NEW (10/10/2016): this method
  63.      * now throws NoClassDefFoundError if driver is not linked properly.
  64.      * @return TRUE if the connection was successful, otherwise FALSE
  65.      * @see DatabaseAccess#getRecentSQLError()
  66.      */
  67.     public boolean connect() throws NoClassDefFoundError
  68.     {
  69.         connection = null;
  70.         try
  71.         {
  72.             //need this first line, or else the driver will not load like it would in regular java projects...
  73.             DriverManager.registerDriver(new Driver());
  74.             connection = DriverManager.getConnection(connectionString, connectionProperties);
  75.             sql_error = null;
  76.         }
  77.         catch(SQLException e)
  78.         {
  79.             sql_error = e;
  80.             return false;
  81.         }
  82.         return true;
  83.     }
  84.    
  85.     /**
  86.      * Attempts to disconnect from a MySQL database.
  87.      * Clears recent sql error on successful disconnection.
  88.      * @return TRUE if successfully disconnected, otherwise FALSE
  89.      * @see DatabaseAccess#getRecentSQLError()
  90.      */
  91.     public boolean disconnect()
  92.     {
  93.         try
  94.         {
  95.             connection.close();
  96.             connection = null;
  97.             sql_error = null;
  98.         }
  99.         catch(SQLException e)
  100.         {
  101.             sql_error = e;
  102.             return false;
  103.         }
  104.         catch(NullPointerException e)
  105.         {
  106.             ; //tried to close a null Connection object. Let's treat this case as a successful disconnect and return TRUE
  107.         }
  108.         return true;
  109.     }
  110.    
  111.     /**
  112.      * Gets the status of the MySQL connection. Use sparingly, as this method queries the database!
  113.      * Clears recent sql error if a connection is successfully established.
  114.      * @return
  115.      * -1 if a connection was never made, <br>
  116.      * 0 if there is a connection that is currently established, <br>
  117.      * 1 if the connection is closed <br>
  118.      * 2 if there was an SQLException retrieving the status (check getRecentSQLError for details)
  119.      * @see DatabaseAccess#getRecentSQLError()
  120.      */
  121.     public int getStatus()
  122.     {
  123.         if(connection == null)
  124.             return -1;
  125.         try
  126.         {
  127.             Statement s = connection.createStatement();
  128.             s.executeQuery("select 1");
  129.             sql_error = null;
  130.             return 0;
  131.         }
  132.         catch(SQLException e)
  133.         {
  134.             sql_error = e;
  135.             return 2;
  136.         }
  137.     }
  138.    
  139.     /**
  140.      * Returns information about the most recent SQL query that has failed.
  141.      * @return the most recent SQL error
  142.      */
  143.     public SQLException getRecentSQLError()
  144.     {
  145.         return sql_error;
  146.     }
  147.    
  148.     /**
  149.      * @param tableName - the new database table name to be used for reading and writing
  150.      */
  151.     public void setTableName(String tableName)
  152.     {
  153.         this.tableName = tableName;
  154.     }
  155.    
  156.     /**
  157.      * Creates a new java.sql.Statement object using the current Connection instance.
  158.      * Clears recent sql error when a Statement object is successfully instantiated.
  159.      * @return a new Statement object
  160.      * @see Statement
  161.      */
  162.     protected Statement makeStatement()
  163.     {
  164.         if(connection == null)
  165.             return null;
  166.        
  167.         Statement s = null;
  168.         try
  169.         {
  170.             s = connection.createStatement();
  171.             sql_error = null;
  172.         }
  173.         catch(SQLException e)
  174.         {
  175.             sql_error = e;
  176.         }
  177.         return s;
  178.     }
  179.    
  180.     /**
  181.      * Creates a new java.sql.PreparedStatement object using the current Connection instance.
  182.      * Clears recent sql error if a PreparedStatement object is successfully instantiated.
  183.      * @param sql - The sql query to be used by the returned PreparedStatement object
  184.      * @return a new PreparedStatement object
  185.      * @see PreparedStatement
  186.      */
  187.     protected PreparedStatement makePreparedStatement(String sql)
  188.     {
  189.         if(connection == null)
  190.             return null;
  191.        
  192.         PreparedStatement ps = null;
  193.         try
  194.         {
  195.             ps = connection.prepareStatement(sql);
  196.             sql_error = null;
  197.         }
  198.         catch(SQLException e)
  199.         {
  200.             sql_error = e;
  201.         }
  202.         return ps;
  203.     }
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement