Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package me.ollie_2411.t2.mysql;
- import java.sql.*;
- /**
- * Created by Ollie on 04/01/2017.
- */
- public class MySQL {
- private String host, database, username, password;
- private int port;
- private Connection connection;
- public MySQL(String host, int port, String database, String username, String password) {
- this.host = host; // retrieves the details of the database from when the class is initialised in the Game class
- this.port = port;
- this.database = database;
- this.username = username;
- this.password = password;
- }
- public void openConnection() { // opens the connection with the databse
- try {
- Class.forName("com.mysql.jdbc.Driver"); // the jdbc driver class must be loaded in order to use the MySQL connection
- } catch (ClassNotFoundException e) { // throws the error if the jdbc driver class is not present
- e.printStackTrace(); // prints the error log to the console
- return; // returns out of the method as the jdbc drivers have not be installed
- }
- try {
- if (connection != null) { // if there already is an open connection
- closeConnection(); // close the connection as multiple connections could cause problems
- }
- connection = DriverManager.getConnection("jdbc:mysql://" + host + ":" + port + "/" + database, username, password); // connects to the database
- } catch (SQLException e) { // the above code could throw a SQL exception
- e.printStackTrace(); // prints the SQL error to console
- }
- }
- public void closeConnection() { // closes the connection with the database
- try {
- if (connection != null) { // if there already is an open connection
- connection.close(); // close the connection as multiple connections could cause problems
- }
- } catch (SQLException e) { // the above code could throw a SQL exception
- e.printStackTrace(); // prints the SQL error to console
- }
- }
- public Statement createStatement(String query) {
- Statement statement = null; // used for executing a SQL query
- try {
- if (connection != null) { // if there already is an open connection
- statement = connection.createStatement(); // creates a statement object for sending SQL queries to the database
- statement.executeQuery(query); // executes the given SQL statement
- }
- } catch (SQLException e) { // the above code could throw a SQL exception
- e.printStackTrace(); // prints the SQL error to console
- }
- return statement; // returns the statement
- }
- public PreparedStatement preparedStatement(String query) {
- PreparedStatement statement = null; // an object that represents a precompiled SQL statement
- try {
- if (connection != null) { // if there already is an open connection
- statement = connection.prepareStatement(query); // generates a ResultSet object with the given type
- }
- } catch (SQLException e) { // the above code could throw a SQL exception
- e.printStackTrace(); // prints the SQL error to console
- }
- return statement; // returns the statement
- }
- public void executeUpdate(PreparedStatement statement) { // used if no result is returned when executing the query
- try {
- statement.executeUpdate(); // executes the given SQL statement which doesn't return any results
- } catch (SQLException e) { // could throw a SQL exception
- e.printStackTrace(); // print the SQL error to console
- } finally { // the code within the finally block will run not matter whether an exception is thrown or not
- try {
- if (!statement.isClosed()) { // checks if the SQL query is still running
- statement.close(); // closes the SQL query
- }
- } catch (SQLException e) { // could throw a SQL exception
- e.printStackTrace(); // prints the SQL error to console
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement