JDBC
概念
Java DataBase Connectivity(java数据库连接,java语言操作数据库)
本质:其实是官方(Sun公司)定义的一套操作所有关系型数据库的规则,即接口。各个数据库厂商去实现这套接口,提供数据库驱动jar包。我们可以使用这套接口(JDBC)编程,真正执行的代码是驱动jar包中的实现类。
快速入门
步骤:
- 导入驱动jar包
- 注册驱动
- 获取数据库连接对象 Connection
- 定义sql
- 获取执行sql语句的对象,statement
- 执行sql,接收返回结果
- 处理结果
- 释放资源
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
| package com.zhuixun.jdbc;
import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement;
public class JDBCDemo2 { public static void main(String[] args) { Statement stmt = null; Connection conn = null; try { Class.forName("com.mysql.jdbc.Driver"); String sql = "insert into account values(null,'王五',3000)"; conn = DriverManager.getConnection("jdbc:mysql:///db3", "root", "root"); stmt = conn.createStatement(); int count = stmt.executeUpdate(sql); System.out.println(count); if (count > 0) { System.out.println("添加成功!"); } else { System.out.println("添加失败!"); } } catch ( ClassNotFoundException e) { e.printStackTrace(); } catch ( SQLException e) { e.printStackTrace(); } finally { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
} }
|
详解各个对象
DriverManager:驱动管理对象
功能:
注册驱动(告诉程序该使用哪一个数据库驱动jar包)
static void registerDriver(Driver driver):注册与给定的驱动程序DriverManager
写代码使用:Class.forName(“com.mysql.jdbc.Driver”)去注册驱动,通过查看源码可以发现,在com.mysql.jdbc.Driver类中存在静态代码块
1 2 3 4 5 6 7
| static { try { java.sql.DriverManager.registerDriver(new Driver()); } catch (SQLException E) { throw new RuntimeException("Can't register driver"); } }
|
注意:mysql5之后的驱动jar包可以省略注册驱动的步骤,因为在jar包中的META-INF中的services中的java.sql.Driver中写了驱动的路径
获取数据库连接
DriverManager.getConnection(String url, String user, String password);
Connection:数据库连接对象
- 功能
- 获取执行sql的对象
- Statement createStatement()
- PreparedStatement prepareStatement(String sql)
- 管理事务
- 开始事务:void setAutoCommit(boolean autoCommit) 调用该方法设置参数为false,即开启事务
- 提交事务:commit()
- 回滚事务:rollback()
Statement:执行sql的对象(用于执行静态sql,就是参数都是确定的)
执行sql
boolean execute(String sql):可以执行任意的sql
int executeUpdate(String sql) :执行DML(insert、update、delete)语句,DDL(create、alter、drop表)语句
返回值:影响的行数,不管新增、更新、删除都会有影响的行数,可以通过这个影响行数判断DML语句是否执行成功,如果返回值大于0,那么表示执行成功,反之则失败
ResultSet executeQuery(String sql) 执行DQL(select)语句
ResultSet:结果集对象(封装查询结果)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
| public class JDBCDemo7 { public static void main(String[] args) { Connection conn = null; Statement stmt = null; ResultSet rs = null; try { Class.forName("com.mysql.jdbc.Driver"); conn = DriverManager.getConnection("jdbc:mysql:///db3", "root", "root"); String sql = "select * from account"; stmt = conn.createStatement(); rs = stmt.executeQuery(sql); while (rs.next()) { int id = rs.getInt(1); String name = rs.getString("name"); double balance = rs.getDouble(3); System.out.println(id + "---" + name + "---" + balance); } } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (SQLException e) { e.printStackTrace(); } finally { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } } }
|
PreparedStatement:执行sql对象
SQL注入问题:在拼接sql时,有一些sql的特殊关键字参与字符串的拼接。会造成安全性问题
- 输入用户随便,输入密码:a’ or ‘a’ = ‘a
- sql:select * from user where username = ‘fhdsjkf’ and password = ‘a’ or ‘a’ = ‘a’
解决sql注入问题:使用PreparedStatement对象来解决
预编译的SQL:参数使用?作为占位符
步骤
- 导入驱动jar包 mysql-connector-java-5.1.37-bin.jar
- 注册驱动
- 获取数据库连接对象Connection
- 定义sql(sql的参数使用?作为占位符。如:select * from user where username = ? and password = ?;)
- 获取执行sql的语句对象PreparedStatement(Connection.prepareStatement(String sql) )
- 给?赋值
- 方法setXxx(参数1,参数2)
- 参数1:?的位置编号,从1开始
- 参数2:?的值
- 执行sql,接收返回结果,不需要传递sql语句
- 处理结果
- 释放资源
注意:后期都会使用PreparedStatement来完成增删改查的所有操作
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96
| public class JDBCDemo9 { public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入用户名:"); String username = sc.nextLine(); System.out.println("请输入密码:"); String password = sc.nextLine(); boolean flag = new JDBCDemo9().login2(username, password); if (flag) { System.out.println("登录成功!"); } else { System.out.println("用户名或密码错误!"); } }
public boolean login(String username, String password) { if (username == null || password == null) { return false; } Connection conn = null; Statement stmt = null; ResultSet rs = null; try { conn = JDBCUtils.getConnection(); String sql = "select * from user where username = '" + username + "' and password = '" + password + "' "; System.out.println(sql); stmt = conn.createStatement(); rs = stmt.executeQuery(sql);
return rs.next(); } catch (SQLException e) { e.printStackTrace(); } finally { JDBCUtils.close(rs, stmt, conn); } return false; }
public boolean login2(String username, String password) { if (username == null || password == null) { return false; } Connection conn = null; PreparedStatement pstmt = null; ResultSet rs = null; try { conn = JDBCUtils.getConnection(); String sql = "select * from user where username = ? and password = ?"; pstmt = conn.prepareStatement(sql); pstmt.setString(1, username); pstmt.setString(2, password); rs = pstmt.executeQuery();
return rs.next(); } catch (SQLException e) { e.printStackTrace(); } finally { JDBCUtils.close(rs, pstmt, conn); } return false; } }
|
JDBC工具类
为了简化书写
- 注册驱动抽取
- 抽取一个方法获取连接对象,为了保证工具的通用性,使用配置文件解决:jdbc.properties
1 2 3 4
| url=jdbc:mysql:///db3 user=root password=root driver=com.mysql.jdbc.Driver
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| import java.io.FileReader; import java.io.IOException; import java.net.URL; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.Properties;
public class JDBCUtils { private static String url; private static String user; private static String password; private static String driver;
static { try { Properties pro = new Properties(); ClassLoader classLoader = JDBCUtils.class.getClassLoader(); URL res = classLoader.getResource("jdbc.properties"); String path = res.getPath(); System.out.println(path); pro.load(new FileReader(path)); url = pro.getProperty("url"); user = pro.getProperty("user"); password = pro.getProperty("password"); driver = pro.getProperty("driver"); Class.forName(driver); } catch (IOException e) { e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } }
public static Connection getConnection() throws SQLException { return DriverManager.getConnection(url, user, password); }
public static void close(Statement stmt, Connection conn) { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
public static void close(ResultSet rs, Statement stmt, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
|
JDBC控制事务
事务:一个包含各个步骤的业务操作。如果这个业务操作被事务管理,则这个多个步骤要么同时成功,要么同时失败。
操作
开始事务(setAutoCommit(boolean autoCommit) )
调用该方法设置参数为false,即开启事务,在执行sql之前开启事务
提交事务(commit() )
当所有sql都执行完提交事务
回滚事务(rollback() )
在catch中回滚事务
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53
| import java.sql.PreparedStatement; import java.sql.SQLException;
public class JDBCDemo10 { public static void main(String[] args) { Connection conn = null; PreparedStatement pstmt1 = null; PreparedStatement pstmt2 = null; try { conn = JDBCUtils.getConnection(); conn.setAutoCommit(false); String sql1 = "update account set balance = balance - ? where id = ?"; String sql2 = "update account set balance = balance + ? where id = ?"; pstmt1 = conn.prepareStatement(sql1); pstmt2 = conn.prepareStatement(sql2); pstmt1.setDouble(1, 500); pstmt1.setInt(2, 1); pstmt2.setDouble(1, 500); pstmt2.setInt(2, 2); pstmt1.executeUpdate(); int i = 3 / 0; pstmt2.executeUpdate(); conn.commit(); } catch (Exception e) { try { if (conn != null) { conn.rollback(); } } catch (SQLException e1) { e1.printStackTrace(); } e.printStackTrace(); } finally { JDBCUtils.close(pstmt1, conn); JDBCUtils.close(pstmt2, null); } } }
|
数据库连接池
概念
其实就是一个容器(集合)存放数据库连接的容器
当系统初始化后,容器被创建,容器中会申请一些连接对象,当用于来访问数据库时,从容器中获取连接对象,用户访问完之后,会将连接对象归还给容器
好处:
- 节约资源
- 用户访问高效
实现
在javax.sql包下提供了一个标准接口:DataSource,
- 接口下面有个getConnection()获取连接的方法。
- 归还连接:如果连接对象Connection是从连接池中获取的,那么调用Connection.close()方法,则不会在关闭连接了,而是归还连接。
一般我们不会去实现它,有数据库厂商来实现
C3P0(数据库连接池技术)
步骤
导入jar包(c3p0-0.9.5.2.jar、mchange-commons-java-0.2.12.jar )
定义配置文件
- 名称:c3p0.properties或者c3p0-config.xml
- 路径:默认在项目的类路径(src目录下)
创建核心对象,数据库连接池对象ComboPooledDataSource
获取连接getConnection
1 2 3 4 5 6 7 8 9
| public class C3p0Demo { public static void main(String[] args) throws Exception { ComboPooledDataSource ds = new ComboPooledDataSource(); Connection conne = ds.getConnection(); System.out.println(conne); } }
|
Druid(数据库连接池,由阿里巴巴提供)
步骤:
- 导入jar包(bruid-1.0.9.jar)
- 定义配置文件
- 是properties形式的
- 可以叫任意名称,可以放在任务目录下
- 加载配置文件 (Properties方式)
- 获取数据库来连接池对象:通过工厂类来获取 (DruidDataSourceFactory)
- 获取连接
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| package com.zhuixun.jdbc;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource; import java.io.InputStream; import java.sql.Connection; import java.util.Properties;
public class DruidDemo { public static void main(String[] args) throws Exception { Properties pro = new Properties(); InputStream is = DruidDemo.class.getClassLoader().getResourceAsStream("druid.properties"); pro.load(is); DataSource dataSource = DruidDataSourceFactory.createDataSource(pro); Connection connection = dataSource.getConnection(); System.out.println(connection); } }
|
定义工具类
- 定义一个类JDBCUtils
- 提供静态代码块加载配置文件,初始化连接池对象
- 提供方法
- 获取连接方法,通过数据库连接池获取连接
- 释放资源
- 获取连接池的方法
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106
| package com.zhuixun.utils;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource; import java.sql.Connection; import java.sql.ResultSet; import java.sql.SQLException; import java.sql.Statement; import java.util.Properties;
public class JDBCUtils { private static DataSource ds;
static { Properties pro = new Properties(); try { pro.load(JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties")); ds = DruidDataSourceFactory.createDataSource(pro); } catch (Exception e) { e.printStackTrace(); } }
public static Connection getConnection() throws SQLException { return ds.getConnection(); }
public static void close(Statement stmt, Connection conn) { if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
public static void close(ResultSet rs, Statement stmt, Connection conn) { if (rs != null) { try { rs.close(); } catch (SQLException e) { e.printStackTrace(); } } if (stmt != null) { try { stmt.close(); } catch (SQLException e) { e.printStackTrace(); } } if (conn != null) { try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } }
public static DataSource getDataSource() { return ds; } }
|
Spring JDBC
Spring 框架对JDBC的简单封装。提供了一个JDBCTemplate对象简化JDBC的开发
步骤:
导入jar包
创建JDBCTemplate对象。依赖于数据源DataSource
- JDBCTemplate template = new JDBCTemplate(DataSource ds);
调用JdbcTemplate的方法来完成CRUD的操作
update()
执行DML语句。增、删、改语句
queryForMap()
查询结果将结果集封装为map集合,将列名作为key,将值作为value,将这条记录封装为一个map集合
注意:这个方法查询结果集长度只能是1
queryForList()
查询结果将结果集封装为list集合
注意:将每一条记录封装为一个Map集合,再将Map集合装载到List集合中
query()
查询结果,将结果封装为javaBean对象
queryForObject() 查询结果,将结果封装为对象
练习
- 修改1号数据的 salary 为 10000
- 添加一条记录
- 删除刚才添加的记录
- 查询id为1的记录,将其封装为Map集合
- 查询所有记录,将其封装为List
- 查询所有记录,将其封装为Emp对象的List集合
- 查询总记录数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
| package com.zhuixun.jdbc;
import org.junit.Test;
import java.sql.ResultSet; import java.sql.SQLException;
public class JdbcTemplateDemo2 { private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());
@Test public void test1() { String sql = "update emp set salary = 10000 where id = 1001"; int count = template.update(sql); System.out.println(count); }
@Test public void test2() { String sql = "insert into emp(id,ename,dept_id) values(?,?,?)"; int count = template.update(sql, 1015, "郭靖", 10); System.out.println(count); }
@Test public void test3() { String sql = "delete from emp where id = ?"; int count = template.update(sql, 1015); System.out.println(count); }
@Test public void test4() { String sql = "select * from emp where id = ? or id = ?"; Map<String, Object> map = template.queryForMap(sql, 1001, 1002); System.out.println(map); }
@Test public void test5() { String sql = "select * from emp"; List<Map<String, Object>> list = template.queryForList(sql); for (Map<String, Object> stringObjectMap : list) { System.out.println(stringObjectMap); } }
@Test public void test6() { String sql = "select * from emp"; List<Emp> list = template.query(sql, new RowMapper<Emp>() { @Override public Emp mapRow(ResultSet rs, int i) throws SQLException { Emp emp = new Emp(); int id = rs.getInt("id"); String ename = rs.getString("ename"); int job_id = rs.getInt("job_id"); int mgr = rs.getInt("mgr"); Date joindate = rs.getDate("joindate"); double salary = rs.getDouble("salary"); double bonus = rs.getDouble("bonus"); int dept_id = rs.getInt("dept_id"); emp.setId(id); emp.setEname(ename); emp.setJob_id(job_id); emp.setMgr(mgr); emp.setJoindate(joindate); emp.setSalary(salary); emp.setBonus(bonus); emp.setDept_id(dept_id); return emp; } }); for (Emp emp : list) { System.out.println(emp); } }
@Test public void test6_2() { String sql = "select * from emp"; List<Emp> list = template.query(sql, new BeanPropertyRowMapper<Emp>(Emp.class)); for (Emp emp : list) { System.out.println(emp); } }
@Test public void test7() { String sql = "select count(id) from emp"; Long total = template.queryForObject(sql, Long.class); System.out.println(total); } }
|