JDBC连接MySQL

创建用于数据库连接的工具类
添加open、close(Connection conn)两个方法

open方法:

  1. URL
  2. Driver
  3. UserName
  4. 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 }

 

posted @ 2015-03-17 23:03  喵~~  阅读(115)  评论(0)    收藏  举报