标签:nbsp mys try jar username 驱动 drive nic jdb
1、在lib中添加jar包;
2、DBhelper类:
public class DBHelper{
private static final String driver = "com.mysql.jdbc.Driver"; //数据库驱动
private static final String url = "jdbc:mysql://localhost:3306/shopping?useUnicon = true&ccharacterEncoding=UTF-8"; //连接地址
private static final String username = "root"; //数据库用户名
private static final String password = ""; //数据库密码
private static Connection coon = null;
//静态代码块负责加载驱动
static{
try{
Class.forName(driver);
}
catch(Exception ex){
ex.printStackTrace();
}
}
//单例模式返回数据库连接对象
public static Connection getConnection() throws Exception{
if(conn == null){
Connection con = DriverManager.getConnection(url,username,password);
return conn;
}
return conn;
}
}
标签:nbsp mys try jar username 驱动 drive nic jdb
原文地址:https://www.cnblogs.com/chengshun/p/8992222.html