mysql连接数据库

需要导的jar包

 

 

import org.apache.commons.dbutils.DbUtils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DBUtil {
public DBUtil(){
}
// static {
// try {
// Class.forName("com.mysql.cj.jdbc.Driver");
// } catch (ClassNotFoundException e) {
// e.printStackTrace();
// }
// }

static {
DbUtils.loadDriver("com.mysql.cj.jdbc.Driver");
System.out.println("驱动连接成功...........");
}
public static Connection getcConnection(){
try {
return DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:3306/bookstore?serverTimezone=UTC&useUnicode=true&characterEncoding=UTF-8",
"root","root123");
} catch (SQLException e) {
throw new RuntimeException("数据库连接失败....",e);
}
}

}
posted @ 2019-07-03 11:10  什么都不懂的程序猿  阅读(252)  评论(0编辑  收藏  举报