JDBC连接MySQL
创建用于数据库连接的工具类
添加open、close(Connection conn)两个方法
open方法:
- URL
- Driver
- UserName
- PassWord
eg:(核心代码)
1 String url = "jdbc:mysql://localhost:3306/WL_DB?useUnicode=true&characterEncoding=gbk"
2 String driver = "com.mysql.jdbc.Driver"
3 String username = "root"
4 String password = "root"
5 //先注册数据库驱动
6 Class.forName(driver);
7 return DriverManager.getConnection(url,username,password);
8 //把上两句用try...catch包含
close方法:
1 if(conn != null){
2 conn.close();
3 }

浙公网安备 33010602011771号