Advertisement
Guest User

DatabaseConnection

a guest
Apr 21st, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.48 KB | None | 0 0
  1. package sql;
  2.  
  3. //aici astea vin incluse
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. import java.sql.Connection;
  7.  
  8. public class DatabaseConnection {
  9.  
  10. private String driver= "com.mysql.jdbc.Driver"; //asta ii de pe net il pun in biblioteci.adica ii un fisier si il includ
  11. private String dbName= "hospital"; //nume db creataa inainte
  12. private String connectionURL = "jdbc:mysql://localhost:3306/"; //psth db, asa ar trebui sa fie
  13. private String username="root"; //cum am pus la mysql
  14. private String password = "Best"; //parola aleasa de mine
  15.  
  16. /**
  17. * @author Andreea Maria GUI
  18. * the private Constructor of @DatabaseConnection
  19. */
  20.  
  21. private DatabaseConnection() { // is alea incluse sus
  22. try{
  23. Class.forName(driver); // asa ii sa fie conectat, is pe acolo prin includes
  24. System.out.println("Database successfully connected!");
  25. }
  26. catch(Exception e) {
  27. e.printStackTrace();
  28. };
  29. }
  30.  
  31. private static DatabaseConnection instance = new DatabaseConnection(); //o instanta, instanta din propria clasa asta
  32.  
  33. /**
  34. *
  35. * @return the created connection which is private in order not to open more than one connection at the same time
  36. * @throws Exception
  37. */
  38. private Connection createConnection() throws Exception{ // bibliotecile
  39.  
  40. Connection connection = DriverManager.getConnection(connectionURL+dbName, username, password); //asa ii
  41.  
  42. return connection;
  43. }
  44.  
  45. /**
  46. *
  47. * @return the connection created especially for our instance object of DatabaseConnection class; this is the public method that gives the connection
  48. * to the database
  49. */
  50.  
  51. public static Connection getConnection() { // getter
  52. try {
  53. return instance.createConnection();
  54.  
  55. } catch (Exception e) {
  56. // TODO Auto-generated catch block
  57. e.printStackTrace();
  58. return null;
  59. }
  60. }
  61. /**
  62. * the public method for closing the connection
  63. */
  64. public static void closeConnection() { // important sa inchi mereu
  65. try {
  66. instance.createConnection().close();
  67. } catch (SQLException e) {
  68. // TODO Auto-generated catch block
  69. e.printStackTrace();
  70. } catch (Exception e) {
  71. // TODO Auto-generated catch block
  72. e.printStackTrace();
  73. }
  74. }
  75.  
  76. public static void main(String[] args) {
  77.  
  78. DatabaseConnection db = new DatabaseConnection();
  79. try {
  80. Connection conn = db.getConnection();
  81. System.out.println("Database successfully connected!");
  82. conn.close();
  83. }catch(Exception e) {
  84. e.printStackTrace();
  85. }
  86. }
  87.  
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement