JDBC
## JDBC
JAVA Database Connectivity Java 数据库连接
###使用JDBC的基本步骤
在项目中添加JDBC驱动,mysql-connector-java-5.1.7-bin.jar
下载地址:https://dev.mysql.com/downloads/file/?id=489462
1.注册驱动
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
2.建立连接
// 参数1:协议+访问数据库。参数2:用户名 参数3:密码 Connection co =DriverManager.getConnection("jdbc:mysql://localhost/phal", "root", "*****");
3.创建statement
Statement st = co.createStatement();
4.执行sql语句,得到ResultSet
String sql = "select * from tab_user";
ResultSet rs = st.executeQuery(sql);
5.遍历结果集
while (rs.next()) {
//getInt("id"):获取数据表中的id int id = rs.getInt("id"); String name = rs.getString("name"); int age = rs.getInt("age"); System.out.println("id=" + id + "name=" + name + "age=" + age); }
6.释放资源
//需要释放的数量根据自己,模板类似
try {
if (rs!=null) {
rs.close();
}
} catch (SQLException e) {
// TODO: handle exception
e.printStackTrace();
}finally {
rs=null;
}
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.IT.www; 2 import java.sql.Connection; 3 import java.sql.DriverManager; 4 import java.sql.ResultSet; 5 import java.sql.SQLException; 6 import java.sql.Statement; 7 8 import com.Itheima.www.JDBCutil; 9 10 public class Maintext { 11 12 public static void main(String[] args) { 13 // TODO 自动生成的方法存根 14 Connection co = null; 15 Statement st = null; 16 ResultSet rs = null; 17 try { 18 // 1.注册驱动 19 DriverManager.registerDriver(new com.mysql.jdbc.Driver()); 20 // 2.建立连接 21 // 参数1:协议+访问数据库。参数2:用户名 参数3:密码 22 co = DriverManager.getConnection("jdbc:mysql://localhost/phal", "root", "123456789"); 23 // 3.创建Statement ,跟数据库打交道,一定需要这个对象 24 st = co.createStatement(); 25 // 4.执行查询,得到结果集 26 String sql = "select * from tab_user"; 27 rs = st.executeQuery(sql); 28 while (rs.next()) { 29 int id = rs.getInt("id"); 30 String name = rs.getString("name"); 31 int age = rs.getInt("age"); 32 33 System.out.println("id=" + id + "name=" + name + "age=" + age); 34 35 } 36 37 } catch (SQLException e) { 38 // TODO 自动生成的 catch 块 39 e.printStackTrace(); 40 } finally { 41 JDBCutil.releae(co, st, rs); 42 } 43 } 44 45 }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package com.Itheima.www; 2 3 import java.sql.Connection; 4 import java.sql.ResultSet; 5 import java.sql.SQLException; 6 import java.sql.Statement; 7 8 public class JDBCutil { 9 10 /** 11 * 释放资源 12 * @param co 13 * @param st 14 * @param rs 15 */ 16 public static void releae(Connection co,Statement st,ResultSet rs) { 17 colseco(co); 18 colsers(rs); 19 colseSt(st); 20 21 } 22 23 private static void colsers(ResultSet rs) { 24 try { 25 if (rs!=null) { 26 rs.close(); 27 } 28 } catch (SQLException e) { 29 // TODO: handle exception 30 e.printStackTrace(); 31 }finally { 32 rs=null; 33 } 34 } 35 private static void colseSt(Statement st) { 36 try { 37 if (st!=null) { 38 st.close(); 39 } 40 } catch (SQLException e) { 41 // TODO: handle exception 42 e.printStackTrace(); 43 }finally { 44 st=null; 45 } 46 } 47 private static void colseco(Connection co) { 48 try { 49 if (co!=null) { 50 co.close(); 51 } 52 } catch (SQLException e) { 53 // TODO: handle exception 54 e.printStackTrace(); 55 }finally { 56 co=null; 57 } 58 } 59 }