mysql的JDBC和连接池
目录
- JDBC和连接池
- 1. JDBC
- 2. 连接池
- 1. 概念
- 2. 分类
- 3. 步骤
- 3. Apache-dbUtils
- 1. 概念
- 2. 以下是相关类
- 3. 示例
- 4. BasicDao
- 基础理解
JDBC和连接池
1. JDBC
- 概念:jdbc是 Java 访问数据库的标准 API,为访问不同的数据库提供了统一接口。
- 以下是常用的API:
- 其中因为普通的Statement有SQL注入安全隐患,一般使用PrepareStatement,该接口在sql查询语句中用
?
作占位符,再用setXxx()传入参数。String sql = "select name , pwd from admin where name =? and pwd = ?"; PreparedStatement preparedStatement = connection.prepareStatement(sql); preparedStatement.setString(1, admin_name); preparedStatement.setString(2, admin_pwd);
- 编写步骤:基础的四步骤为注册驱动,获取连接,执行SQL语句,释放资源,因为实际项目一般要用到连接池,这部分就不过多赘述,以下为示例代码:
Class.forName("com.mysql.cj.jdbc.Driver");
// String url = "jdbc:mysql://local:3306/test";String url = "jdbc:mysql://localhost:3306/test?serverTimezone=UTC";String user = "root";String password = "12345689";Connection connection = DriverManager.getConnection(url, user, password);System.out.println(connection);Statement statement = connection.createStatement();String sql = "select * from dept";ResultSet resultSet = statement.executeQuery(sql);while(resultSet.next()){int deptno = resultSet.getInt("DEPTNO");String dname = resultSet.getString("DNAME");String loc = resultSet.getString("LOC");System.out.println(deptno+"\t"+dname+"\t"+loc);}resultSet.close();statement.close();connection.close();
2. 连接池
1. 概念
连接池本质就是为了减少频繁连接带来的损耗,维持一定数量的连接,需要连接时从池中取出,用完放回即可。
2. 分类
常见的连接池有C3P0和Druid,Druid得益于其优秀的性能在当下被广泛使用
3. 步骤
因为连接池可以使用配置文件,与直接用JDBC只有在前两步有所区别,或者说更为简便
//c3p0ComboPooledDataSource test = new ComboPooledDataSource("test");String sql = "select * from dept";Connection connection = test.getConnection();
//Druid
// Properties properties = new Properties();
// properties.load(new FileInputStream("src\\druid.properties"));
// DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);
// Connection connection = dataSource.getConnection();Statement statement = connection.createStatement();ResultSet resultSet = statement.executeQuery(sql);while(resultSet.next()){int deptno = resultSet.getInt("DEPTNO");String dname = resultSet.getString("DNAME");String loc = resultSet.getString("LOC");System.out.println(deptno+"\t"+dname+"\t"+loc);}resultSet.close();statement.close();connection.close();
3. Apache-dbUtils
1. 概念
为了解决在connection关闭后仍然可以获取到resulteSet中的数据,于是Apache组织提供的commons-dbutils工具库可以解决这个问题,在这里还需要用到Java Bean, 是一种遵循特定编程规范的 Java 类,用于封装数据,简单来讲就是将表中的各个字段转换成类的属性。
2. 以下是相关类
3. 示例
Properties properties = new Properties();properties.load(new FileInputStream("src\\druid.properties"));DataSource dataSource = DruidDataSourceFactory.createDataSource(properties);Connection connection = dataSource.getConnection();QueryRunner queryRunner = new QueryRunner();
// String sql = "select * from dept where DEPTNO >= ?";
// List<Dept> list = queryRunner.query(connection, sql, new BeanListHandler<>(Dept.class), 1);
// for (Dept dept : list) {
// System.out.println(dept);
// }
// String sql = "select * from dept where DEPTNO = ?";
// Dept dept = queryRunner.query(connection, sql, new BeanHandler<>(Dept.class), 10);
// String sql = "select ? from dept where DEPTNO = ?";
// Object dept = queryRunner.query(connection, sql, new ScalarHandler(), "DEPTNO", 10);
// System.out.println(dept);String sql = "insert into dept values (?,?,?)";int update = queryRunner.update(connection, sql, 50, "DISCUSSION", "MEXICO");System.out.println(update);connection.close();
4. BasicDao
基础理解
Dao即为数数据对象,它的作用是封装对数据库的数据库的访问,将各个Dao共同操作放在一起即为BasicDao,这样数据库的每张表都可以在BasicDao的基础上实现自己的Dao,大大提高了代码的维护性,再结合apche-dbutils和Druid,可以形成以下开发中的层次结构图:
本文部分截图自于【零基础 快速学Java】韩顺平 零基础30天学会Java