Advertisement
Guest User

Untitled

a guest
Jan 20th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.11 KB | None | 0 0
  1. package factory;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6.  
  7. public class DatabaseConnection {
  8. private static DatabaseConnection INSTANCE;
  9. private static Connection connection = null;
  10.  
  11. public DatabaseConnection() {
  12.  
  13. try {
  14. Class.forName("com.mysql.jdbc.Driver");
  15.  
  16. connection = DriverManager.getConnection(
  17. "jdbc:mysql://localhost:3306/firma",
  18. "root",
  19. "Grunwaldzka16");
  20. System.out.println("Successfully connected to database!");
  21.  
  22. } catch (ClassNotFoundException e) {
  23.  
  24. e.printStackTrace();
  25. System.out.append("No MySQL library found.");
  26.  
  27. } catch (SQLException e) {
  28.  
  29. e.printStackTrace();
  30. System.out.append("Database login failed! Check Your username and/or password.");
  31.  
  32. }
  33. }
  34. public DatabaseConnection(String password, String username) {
  35.  
  36. try {
  37. Class.forName("org.mysql.jdbc.Driver");
  38.  
  39. connection = DriverManager.getConnection(
  40. "jdbc:mysql://localhost:3306/firma",
  41. username,
  42. password);
  43. System.out.println("Successfully connected to database!");
  44.  
  45. } catch (ClassNotFoundException e) {
  46.  
  47. e.printStackTrace();
  48. System.out.append("No MySQL library found.");
  49.  
  50. } catch (SQLException e) {
  51.  
  52. e.printStackTrace();
  53. System.out.append("Database login failed! Check Your username and/or password.");
  54.  
  55. }
  56. }
  57.  
  58. public static DatabaseConnection getInstance() {
  59. if (INSTANCE == null) {
  60. synchronized (DatabaseConnection.class) {
  61. if (INSTANCE == null) {
  62. INSTANCE = new DatabaseConnection();
  63. System.out.println("Database connection instance not found, created new one.");
  64. }
  65. }
  66. }
  67.  
  68. return INSTANCE;
  69. }
  70. public Connection getConnection() {
  71. return connection;
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement