Advertisement
Guest User

qsdfqsdfqsdf

a guest
Dec 12th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.66 KB | None | 0 0
  1. package finall;
  2.  
  3. import java.sql.Connection;
  4. import java.sql.DriverManager;
  5. import java.sql.SQLException;
  6. import java.sql.Statement;
  7. import java.util.Properties;
  8.  
  9. public class DBConnection {
  10. private static DBConnection db;
  11. private Connection connect;
  12. private static String userName = "root";
  13. private static String password = "";
  14. private static String serverName = "localhost";
  15. //Attention, sous MAMP, le port est 8889
  16. private static String portNumber = "3306";
  17. private static String tableName = "personne";
  18.  
  19. // iL faut une base nommee testPersonne !
  20. private static String dbName = "testpersonne";
  21.  
  22. // creation de la connection
  23.  
  24.  
  25. private DBConnection(){
  26. Properties connectionProps = new Properties();
  27. connectionProps.put("user", userName);
  28. connectionProps.put("password", password);
  29. String urlDB = "jdbc:mysql://" + serverName + ":";
  30. urlDB += portNumber + "/" + dbName;
  31. try {
  32. connect = DriverManager.getConnection(urlDB, connectionProps);
  33. } catch (SQLException e) {
  34. // TODO Auto-generated catch block
  35. e.printStackTrace();
  36. }
  37. }
  38.  
  39.  
  40. public static synchronized DBConnection getInstance() {
  41. if(db == null)
  42. db = new DBConnection();
  43. return db;
  44. }
  45.  
  46. public static Connection getConnection(){
  47. return DBConnection.getInstance().connect;
  48. }
  49.  
  50. public static void setNomDB(String dbName){
  51. DBConnection.dbName = dbName;
  52. try {
  53. DBConnection.getConnection().close();
  54. } catch (SQLException e) {
  55. // TODO Auto-generated catch block
  56. e.printStackTrace();
  57. }
  58. DBConnection.db = null;
  59. }
  60.  
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement