Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package factory;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- public class DatabaseConnection {
- private static DatabaseConnection INSTANCE;
- private static Connection connection = null;
- public DatabaseConnection() {
- try {
- Class.forName("com.mysql.jdbc.Driver");
- connection = DriverManager.getConnection(
- "jdbc:mysql://localhost:3306/firma",
- "root",
- "Grunwaldzka16");
- System.out.println("Successfully connected to database!");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- System.out.append("No MySQL library found.");
- } catch (SQLException e) {
- e.printStackTrace();
- System.out.append("Database login failed! Check Your username and/or password.");
- }
- }
- public DatabaseConnection(String password, String username) {
- try {
- Class.forName("org.mysql.jdbc.Driver");
- connection = DriverManager.getConnection(
- "jdbc:mysql://localhost:3306/firma",
- username,
- password);
- System.out.println("Successfully connected to database!");
- } catch (ClassNotFoundException e) {
- e.printStackTrace();
- System.out.append("No MySQL library found.");
- } catch (SQLException e) {
- e.printStackTrace();
- System.out.append("Database login failed! Check Your username and/or password.");
- }
- }
- public static DatabaseConnection getInstance() {
- if (INSTANCE == null) {
- synchronized (DatabaseConnection.class) {
- if (INSTANCE == null) {
- INSTANCE = new DatabaseConnection();
- System.out.println("Database connection instance not found, created new one.");
- }
- }
- }
- return INSTANCE;
- }
- public Connection getConnection() {
- return connection;
- }
- }
Add Comment
Please, Sign In to add comment