1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
|
public class ConDB
{
private static Connection con = null;
private String dbName = "NameDB";
private String user = "root";
private String pass = "PWD";
private String server = "localhost";
private ConDB()
{
try
{
Class.forName("com.mysql.jdbc.Driver");
String encoding = "?useUnicode=true&characterEncoding=UTF-8";
String conString = "jdbc:mysql://" + server + "/" + dbName + encoding;
con = (Connection) DriverManager.getConnection(conString,user,pass);
}
catch(ClassNotFoundException e)
{
e.printStackTrace();
}
catch(SQLException e1)
{
e1.printStackTrace();
}
}
public static Connection getInstance()
{
if(con == null)
new ConDB();
return con;
}
} |
Partager