DriverManager驱动管理器
getConnection方法连接数据库
DriverManager驱动管理器
getConnection方法连接数据库
//使用驱动,相对路径
Class.forname("com.mysql.jdbc.Driver"); 、
//创建连接密码等//返回Connection
DriverManager. getConnectin(url,user,password);
//连接服务器的地址用户名密码和指定库
url=“jdbc:mysql://127.0.0.1:3306/xxxx”;
//设置编码的连接方式(防止乱码)
url=“jdbc:mysql://127.0.0.1:3306/xxxx?useUnicode=true&characterEncoding=UTF-8”;
public static void main(String[] args) {
try {
Class.forName("com.mysql.jdbc.Driver");
String url="jdbc:mysql://localhost:3306/webdb_9?useUnicode=true&charterEncoding=UTF-8";
String user="root";
String password="root";
Connection C=DriverManager.getConnection(url, user, password);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}