JDBC链接数据库
JDBC连接MySQL数据库
Java DataBase Connectivity数据库连接
1.添加驱动
1.在项目目录下添加lib文件夹,并在其中加入mysql-connector-java-5.1.7-bin.jar驱动器。
2.在驱动文件上右键选中build path-->add to build path;
2.注册驱动
DriverManager.registerDriver(new Driver());
3.建立连接
Connection conn =DriverManager.getConnection("jdbc:mysql://localhost/test01","root","123456");
//参数1:协议+访问的数据库
//参数2:用户名
//参数3:密码
第一次代码实现:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.nike.demo01; 2 3 import java.sql.Connection; 4 import java.sql.DriverManager; 5 import java.sql.ResultSet; 6 //import java.sql.SQLException; 7 import java.sql.Statement; 8 9 import com.nike.demo01.util.JDBCUtil; 10 11 //import com.mysql.jdbc.Driver; 12 13 public class Test01 { 14 private static String url = "jdbc:mysql://localhost/test01?useUnicode=true&characterEncoding=utf8"; 15 private static String user = "root"; 16 private static String pwd = "Hwp971102"; 17 18 public static void main(String[] args) { 19 Connection conn = null; 20 Statement st = null; 21 ResultSet rs = null; 22 try { 23 //注册驱动 24 //DriverManager.registerDriver(new Driver()); 25 Class.forName("com.mysql.jdbc.Driver").newInstance(); 26 //建立链接 27 conn = DriverManager.getConnection(url, user, pwd); 28 st = conn.createStatement(); 29 String sql = "SELECT * FROM stu"; 30 rs = st.executeQuery(sql); 31 while(rs.next()){ 32 String id = rs.getString("id"); 33 String name = rs.getString("name"); 34 int age = rs.getInt("age"); 35 System.out.println(id + "\t" + name +"\t" + age); 36 } 37 rs.close(); 38 st.close(); 39 conn.close(); 40 41 } catch (Exception e) { 42 e.printStackTrace(); 43 }finally{ 44 JDBCUtil.release(conn, st, rs); 45 } 46 } 47 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.nike.demo01.util; 2 3 import java.sql.Statement; 4 import java.sql.Connection; 5 import java.sql.ResultSet; 6 import java.sql.SQLException; 7 8 public class JDBCUtil { 9 10 public static void release(Connection conn,Statement st,ResultSet rs){ 11 closeConn(conn); 12 closeSt(st); 13 closeRs(rs); 14 } 15 16 public static void closeConn(Connection conn){ 17 18 try { 19 if(conn != null){ 20 conn.close(); 21 } 22 } catch (SQLException e) { 23 e.printStackTrace(); 24 }finally{ 25 conn = null; 26 } 27 28 } 29 30 public static void closeSt(Statement st){ 31 try { 32 if(st != null){ 33 st.close(); 34 } 35 } catch (SQLException e) { 36 e.printStackTrace(); 37 }finally{ 38 st = null; 39 } 40 41 } 42 public static void closeRs(ResultSet rs){ 43 try { 44 if(rs != null){ 45 rs.close(); 46 } 47 } catch (SQLException e) { 48 e.printStackTrace(); 49 }finally{ 50 rs = null; 51 } 52 } 53 }
1 //注册驱动
2 //DriverManager.registerDriver(new Driver());
3 Class.forName("com.mysql.jdbc.Driver").newInstance();
4 //建立链接
5 conn = DriverManager.getConnection(url, user, pwd);
6 //Driver 这个类里面有静态代码块,一上来就执行了,所以等同于我们注册了两遍驱动。
JDBC工具类
1.创建properties配置文件,在src目录下声明一个xxx.properties文件。
1 driverClass=com.mysql.jdbc.Driver
2 url=jdbc:mysql://localhost/test01?useUnicode=true&characterEncoding=utf8
3 user=root
4 pwd=Hwp971102
2.读取properties文件。
1 static {
2 try {
3 Properties properies = new Properties();
4 InputStream is = JDBCUtil.class.getClassLoader()
5 .getResourceAsStream("sql.properies");
6 properies.load(is);
7 driver = properies.getProperty("driverClass");
8 url = properies.getProperty("url");
9 user = properies.getProperty("user");
10 pwd = properies.getProperty("pwd");
11 } catch (IOException e) {
12 e.printStackTrace();
13 }
14 }