Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package connector;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- public class Connector {
- private static final Logger LOGGER = Logger.getLogger(Connector.class.getName());
- private static final String DRIVER = "com.mysql.cj.jdbc.Driver";
- private static final String DBURL = "jdbc:mysql://localhost:3306/ass2db?autoReconnect=true&useSSL=false";
- private static final String USER = "root";
- private static final String PASS = "password";
- private static Connector singleInstance = new Connector();
- private Connector() {
- try {
- Class.forName(DRIVER);
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- }
- }
- private Connection createConnection() {
- Connection connection = null;
- try {
- connection = DriverManager.getConnection(DBURL, USER, PASS);
- return connection;
- } catch (SQLException e) {
- LOGGER.log(Level.WARNING, "An error occured while trying to connect to the database");
- e.printStackTrace();
- return null;
- }
- }
- public static Connection getConnection() {
- return singleInstance.createConnection();
- }
- public static void close(Connection connection) {
- if (connection != null) {
- try {
- connection.close();
- } catch (SQLException e) {
- LOGGER.log(Level.WARNING, "An error occured while trying to close the connection");
- }
- }
- }
- public static void close(Statement statement) {
- if (statement != null) {
- try {
- statement.close();
- } catch (SQLException e) {
- LOGGER.log(Level.WARNING, "An error occured while trying to close the statement");
- }
- }
- }
- public static void close(ResultSet resultSet) {
- if (resultSet != null) {
- try {
- resultSet.close();
- } catch (SQLException e) {
- LOGGER.log(Level.WARNING, "An error occured while trying to close the ResultSet");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement