Advertisement
Guest User

Untitled

a guest
Mar 11th, 2017
129
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.17 KB | None | 0 0
  1. package me.ollie_2411.t2.mysql;
  2.  
  3. import java.sql.*;
  4.  
  5. /**
  6.  * Created by Ollie on 04/01/2017.
  7.  */
  8. public class MySQL {
  9.  
  10.     private String host, database, username, password;
  11.     private int port;
  12.     private Connection connection;
  13.  
  14.     public MySQL(String host, int port, String database, String username, String password) {
  15.         this.host = host; // retrieves the details of the database from when the class is initialised in the Game class
  16.         this.port = port;
  17.         this.database = database;
  18.         this.username = username;
  19.         this.password = password;
  20.     }
  21.  
  22.     public void openConnection() { // opens the connection with the databse
  23.         try {
  24.             Class.forName("com.mysql.jdbc.Driver"); // the jdbc driver class must be loaded in order to use the MySQL connection
  25.         } catch (ClassNotFoundException e) { // throws the error if the jdbc driver class is not present
  26.             e.printStackTrace(); // prints the error log to the console
  27.             return; // returns out of the method as the jdbc drivers have not be installed
  28.         }
  29.  
  30.         try {
  31.             if (connection != null) { // if there already is an open connection
  32.                 closeConnection(); // close the connection as multiple connections could cause problems
  33.             }
  34.             connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password); // connects to the database
  35.         } catch (SQLException e) { // the above code could throw a SQL exception
  36.             e.printStackTrace(); // prints the SQL error to console
  37.         }
  38.     }
  39.  
  40.     public void closeConnection() { // closes the connection with the database
  41.         try {
  42.             if (connection != null) { // if there already is an open connection
  43.                 connection.close(); // close the connection as multiple connections could cause problems
  44.             }
  45.         } catch (SQLException e) { // the above code could throw a SQL exception
  46.             e.printStackTrace(); // prints the SQL error to console
  47.         }
  48.     }
  49.  
  50.     public Statement createStatement(String query) {
  51.         Statement statement = null; // used for executing a SQL query
  52.         try {
  53.             if (connection != null) { // if there already is an open connection
  54.                 statement = connection.createStatement(); // creates a statement object for sending SQL queries to the database
  55.                 statement.executeQuery(query); // executes the given SQL statement
  56.             }
  57.         } catch (SQLException e) { // the above code could throw a SQL exception
  58.             e.printStackTrace(); // prints the SQL error to console
  59.         }
  60.         return statement; // returns the statement
  61.     }
  62.  
  63.     public PreparedStatement preparedStatement(String query) {
  64.         PreparedStatement statement = null; // an object that represents a precompiled SQL statement
  65.         try {
  66.             if (connection != null) { // if there already is an open connection
  67.                 statement = connection.prepareStatement(query); // generates a ResultSet object with the given type
  68.             }
  69.         } catch (SQLException e) { // the above code could throw a SQL exception
  70.             e.printStackTrace(); // prints the SQL error to console
  71.         }
  72.         return statement; // returns the statement
  73.     }
  74.  
  75.     public void executeUpdate(PreparedStatement statement) { // used if no result is returned when executing the query
  76.         try {
  77.             statement.executeUpdate(); // executes the given SQL statement which doesn't return any results
  78.         } catch (SQLException e) { // could throw a SQL exception
  79.             e.printStackTrace(); // print the SQL error to console
  80.         } finally { // the code within the finally block will run not matter whether an exception is thrown or not
  81.             try {
  82.                 if (!statement.isClosed()) { // checks if the SQL query is still running
  83.                     statement.close(); // closes the SQL query
  84.                 }
  85.             } catch (SQLException e) { // could throw a SQL exception
  86.                 e.printStackTrace(); // prints the SQL error to console
  87.             }
  88.         }
  89.     }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement