Advertisement
Guest User

Untitled

a guest
Jun 30th, 2017
233
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.40 KB | None | 0 0
  1. package com.unknowncall.wipeout;
  2.  
  3. import java.io.PrintStream;
  4. import java.sql.Connection;
  5. import java.sql.DriverManager;
  6. import java.sql.SQLException;
  7.  
  8. public class DatabaseConnection
  9. {
  10. private Connection connection;
  11. private String host;
  12. private int port;
  13. private String databaseName;
  14. private String username;
  15. private String password;
  16.  
  17. public DatabaseConnection(String host, int port, String databaseName, String username, String password)
  18. {
  19. this.host = host;
  20. this.port = port;
  21. this.databaseName = databaseName;
  22. this.username = username;
  23. this.password = password;
  24. try
  25. {
  26. openConnection();
  27. }
  28. catch (ClassNotFoundException|SQLException e)
  29. {
  30. e.printStackTrace();
  31. }
  32. }
  33.  
  34. public void openConnection()
  35. throws SQLException, ClassNotFoundException
  36. {
  37. if ((this.connection != null) && (!this.connection.isClosed())) {
  38. return;
  39. }
  40. synchronized (this)
  41. {
  42. if ((this.connection != null) && (!this.connection.isClosed())) {
  43. return;
  44. }
  45. Class.forName("com.mysql.jdbc.Driver");
  46. this.connection = DriverManager.getConnection("jdbc:mysql://" + this.host + ":" + this.port + "/" + this.databaseName, this.username, this.password);
  47. System.out.println("Connected to database.");
  48. }
  49. }
  50.  
  51. public Connection getConnection()
  52. {
  53. return this.connection;
  54. }
  55. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement