Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package finall;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.SQLException;
- import java.sql.Statement;
- import java.util.Properties;
- public class DBConnection {
- private static DBConnection db;
- private Connection connect;
- private static String userName = "root";
- private static String password = "";
- private static String serverName = "localhost";
- //Attention, sous MAMP, le port est 8889
- private static String portNumber = "3306";
- private static String tableName = "personne";
- // iL faut une base nommee testPersonne !
- private static String dbName = "testpersonne";
- // creation de la connection
- private DBConnection(){
- Properties connectionProps = new Properties();
- connectionProps.put("user", userName);
- connectionProps.put("password", password);
- String urlDB = "jdbc:mysql://" + serverName + ":";
- urlDB += portNumber + "/" + dbName;
- try {
- connect = DriverManager.getConnection(urlDB, connectionProps);
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public static synchronized DBConnection getInstance() {
- if(db == null)
- db = new DBConnection();
- return db;
- }
- public static Connection getConnection(){
- return DBConnection.getInstance().connect;
- }
- public static void setNomDB(String dbName){
- DBConnection.dbName = dbName;
- try {
- DBConnection.getConnection().close();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- DBConnection.db = null;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement