DBUtils

上次学到dbcp或者c3p0可创建连接池,但是Connection创建了,用什么来访问数据呢,答案是:DBUtils进行CRUD操作即可。

QueryRunner类
顾名思义,该类是用来执行Query语句的。其中构造方法有
QueryRunner() //不带事务的话,用这个就可以啦。
QueryRunner(DataSource ds) //如果用到事务,则需要带datasource的构造器。
删除、追加、修改
Connection conn = DBUtils.getConnection();
String delete = "delete from student where name=?";
String insert = "insert into student (name) values(?)";
String modify = "update student set name=? where id=?";

//执行sql语句
QueryRunner queryRunner = new QueryRunner();
queryRunner.update(conn, insert, "hello");
queryRunner.update(conn, delete, "hello");
原始方式查询
String sql=“select name from student where id=?”;
DBUtils将PreparedStatement进行封装,不需要用户自己进行繁琐的处理。
在查询中只需要通过实现ResultSetHandler接口,实现handle方法,处理ResultSet即可。
query(Connection conn, String sql, ResultSetHandler<T> rsh, param1, param2)
public class DBUtilsTest {
	public static void main(String[] args) throws Exception{
		Connection conn = DAO.getConnection();
		QueryRunner queryRunner = new QueryRunner();
		
		String sql = "select name, sex, age from student where id=";	
		class MyResultSetHandler implements ResultSetHandler<List<Student>>{
			@Override
			public List<Student> handle(ResultSet arg0) throws SQLException {
				List<Student> list = new ArrayList<Student>();
				while(arg0.next())
				{
					Student s = new Student(arg0.getString(1), arg0.getString(2), arg0.getInt(3));
					list.add(s);
				}
				return list;
			}
		}
		
		List<Student> rs= queryRunner.query(conn, sql, new MyResultSetHandler());
		for(Student s:rs){
			System.out.println(s);
		}
	}
}
高级的BeanHandler举例
上面说的实现ResultSetHanlder,其实在DBUtils中已经有很多默认的实现方法,用起来更方便。
BeanHandler,返回记录中的第一条记录,并且转为指定的Bean对象。
※注意:由于BeanHandler是通过反射来创建对应类的,所以指定的类必须有一个public的无参构造器。
public class <span style="font-family: Arial, Helvetica, sans-serif;">DBUtilsTest </span>
{
	public static void main(String[] args) throws Exception{
		Connection conn = DAO.getConnection();
		QueryRunner queryRunner = new QueryRunner();
		
		String sql = "select name, sex, age from student where id=?";	
		Student rs= queryRunner.query(conn, sql, new BeanHandler<Student>(Student.class), 3);
		System.out.println(rs);
	}
}
BeanListHandler举例:
public class DBUtilsTest  {
	public static void main(String[] args) throws Exception{
		Connection conn = DAO.getConnection();
		QueryRunner queryRunner = new QueryRunner();
		
		String sql = "select name, sex, age from student";	
		List<Student> rs= queryRunner.query(conn, sql, new BeanListHandler<Student>(Student.class));
		System.out.println(rs);
	}
}
MapHandler,MapListHandler
也可以不用集合,用MapHandler,MapListHandler返回键值对的形式。代码和上面一样不再重复。
{name=student1, sex=Female, age=20}
ScalarHandler,返回一个单个值。
public class DBUtilsTest  {
	public static void main(String[] args) throws Exception{
		Connection conn = DAO.getConnection();
		QueryRunner queryRunner = new QueryRunner();
		
		String sql = "select count(id) from student";	
		Long rs= queryRunner.query(conn, sql, new ScalarHandler<Long>(), 3);
		System.out.println(rs);
	}
}

<完>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Python DBUtils 是一个轻量级的 Python 数据库连接池库,支持多个线程和多个进程,并可与各种数据库后端一起使用。DBUtils 是一个纯 Python 库,没有其他依赖项。 DBUtils 的主要目的是提供一个共享数据库连接池,这样可以避免在不同的线程和进程中频繁地打开和关闭数据库连接。这可以极大地提高应用程序的性能和响应速度。 DBUtils 提供了一些常见的数据库连接池实现,包括 PooledDB,PersistentDB 和 StackedObjectPool。这些实现都提供了相同的接口,因此可以很容易地将它们用于不同的应用程序。 使用 DBUtils 可以在保持代码简洁的同时获得数据库连接池的好处。以下是一个使用 DBUtils 连接 MySQL 数据库的示例: ```python import pymysql from dbutils.pooled_db import PooledDB POOL = PooledDB( creator=pymysql, maxconnections=5, mincached=2, maxcached=5, blocking=True, maxusage=None, host='localhost', port=3306, user='root', password='password', database='test', charset='utf8mb4' ) def get_conn(): return POOL.connection() def query_data(sql): conn = get_conn() cursor = conn.cursor() cursor.execute(sql) result = cursor.fetchall() cursor.close() conn.close() return result ``` 在上面的示例中,我们使用 `PooledDB` 创建了一个 MySQL 数据库连接池,并使用 `get_conn` 获取一个连接对象,然后使用 `query_data` 函数执行 SQL 查询。注意,我们在使用完连接后需要手动关闭连接。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值