Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/mydata?userUnicode=true&characterEncoding=utf-8&useSSL=false"
其中jdbc是调用jdbc驱动,mysql是指数据库类型此处为mysql数据库,localhost指链接地址,此处指本地mysql数据库,3306为mysql所使用的端口号,useUnicode=true&characterEncoding=utf-8是显示数据库数据时所使用的编码格式,useSSl=false制定ssl 链接为关闭,在较高版本中必须制定ssl是否连接,否则会报错
Strinf user = “root”; String psaawd = "123456"
user为mysql用户名,passwd为密码
Connection con = DriverManager.getConnection(url,user,passwd);
作为初始化的一部分,DriverManager 类会尝试加载在 "jdbc.drivers" 系统属性中引用的驱动程序类
getConnection(String url, String user, String password) 方法试图建立到给定数据库 URL 的连接
自此就与数据库建立了链接
public static Connection getConnection () throws SQLException,
java.lang.ClassNotFoundException
{
//加载mysql的jdbc驱动
Class.forName("com.mysql.jdbc.Driver");
//取得连接的url和用户名和密码
String url = "jdbc:mysql://localhost:3306/mydate?useUnicode=true&characterEncoding=utf-8&useSSL=false";
String user = "root";
String passwd = "123456";
//创建与数据库的实例连接
Connection con = DriverManager.getConnection(url, user,passwd);
return con;
}
原文:http://www.cnblogs.com/lt163it/p/7372828.html