Advertisement
Guest User

Untitled

a guest
Apr 5th, 2019
132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.04 KB | None | 0 0
  1. package dao;
  2.  
  3.  
  4.  
  5. import java.sql.Connection;
  6. import java.sql.DriverManager;
  7.  
  8. import java.sql.SQLException;
  9.  
  10.  
  11.  
  12. public class Connect {
  13. //modif par ma basse de données
  14. private static final String DRIVER = "com.mysql.jdbc.Driver";
  15. private static final String DBNAME = "gestion";
  16. private static final String URL = "jdbc:mysql://localhost/"+DBNAME;
  17. private static final String USER = "root";
  18. private static final String PASSWORD = "";
  19. private static Connect instance;
  20.  
  21. private Connect() {
  22. try {
  23. Class.forName(DRIVER).newInstance();
  24. System.out.println("*** Driver loaded. ***");
  25. }
  26. catch (ClassNotFoundException e) {
  27. System.err.println("*** ERROR: Driver " + DRIVER + " non trouvé ***");
  28. }
  29. catch (InstantiationException e) {
  30. System.err.println("*** ERROR: Impossible to create an instance of " + "org.apache.derby.jdbc.ClientDriver");
  31. System.err.println(e.getMessage());
  32. }
  33. catch (IllegalAccessException e) {
  34. System.err.println("*** ERROR: Impossible to create an instance of " + "org.apache.derby.jdbc.ClientDriver");
  35. System.err.println(e.getMessage());
  36. }
  37. }
  38. //
  39. public static Connect getInstance() {
  40. if (instance == null) {
  41. instance = new Connect();
  42. }
  43. return instance;
  44. }
  45.  
  46. public static String getDBNAME() {
  47. return DBNAME;
  48. }
  49.  
  50.  
  51. public Connection getConnection() throws SQLException {
  52. Connection cx = DriverManager.getConnection(URL, USER, PASSWORD);
  53. return cx;
  54. }
  55.  
  56. public void close(final Connection cx) {
  57. if (cx != null) {
  58. try {
  59. cx.close();
  60. }
  61. catch (SQLException e) {
  62. // très rare a priori donc pas de throws
  63. System.err.println("Impossible to close connection");
  64. System.err.println(e.getMessage());
  65. }
  66. }
  67. }
  68.  
  69.  
  70. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement