Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.unknowncall.wipeout;
- import java.io.PrintStream;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- public class DatabaseConnection
- {
- private Connection connection;
- private String host;
- private int port;
- private String databaseName;
- private String username;
- private String password;
- public DatabaseConnection(String host, int port, String databaseName, String username, String password)
- {
- this.host = host;
- this.port = port;
- this.databaseName = databaseName;
- this.username = username;
- this.password = password;
- try
- {
- openConnection();
- }
- catch (ClassNotFoundException|SQLException e)
- {
- e.printStackTrace();
- }
- }
- public void openConnection()
- throws SQLException, ClassNotFoundException
- {
- if ((this.connection != null) && (!this.connection.isClosed())) {
- return;
- }
- synchronized (this)
- {
- if ((this.connection != null) && (!this.connection.isClosed())) {
- return;
- }
- Class.forName("com.mysql.jdbc.Driver");
- this.connection = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.databaseName, this.username, this.password);
- System.out.println("Connected to database.");
- }
- }
- public Connection getConnection()
- {
- return this.connection;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement