JDBC学习笔记 | DBUtils的使用

这篇博客详细介绍了Apache的DBUtils工具库在JDBC操作中的应用,包括需要的jar包、QueryRunner的update和query方法,以及BeanHandler、BeanListHandler、MapHandler、MapListHandler和ScalarHandler的使用示例,帮助读者理解如何简化JDBC操作。
摘要由CSDN通过智能技术生成

目录

一、简介

二、使用

1.需要的jar包

2.一些准备工作

3.QueryRunner

(1).使用update方法进行更新操作

(2).使用query方法进行查询操作

4.BeanHandler

5.BeanListHandler

6.MapHandler

7.MapListHandler 

8.ScalarHandler


一、简介

commons-dbutils是Apache组织提供的一个开源JDBC工具类库

二、使用

1.需要的jar包

2.一些准备工作

数据库数据表如下:

创建一个customer类:


public class Customer {
	private Integer id;
	private String name;
	private String email;
	private String birth;
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getBirth() {
		return birth;
	}
	public void setBirth(String birth) {
		this.birth = birth;
	}
	
	public Customer(Integer id, String name, String email, String birth) {
		super();
		this.id = id;
		this.name = name;
		this.email = email;
		this.birth = birth;
	}
	
	@Override
	public String toString() {
		return "Customer [id=" + id + ", name=" + name + ", email=" + email + ", birth=" + birth + "]";
	}
	public Customer() {
		
	}
}

编写的JDBCTools工具类代码如下:

public class JDBCTools {
	
	private static DataSource dataSource = null;
	static {
		dataSource =  new ComboPooledDataSource();	
	}
	
	//获取数据库连接
	public static Connection getConnection() throws IOException, ClassNotFoundException, SQLException {
		return dataSource.getConnection();
	}

	
	
	
	public static void releaseSource(ResultSet res,Statement statement,Connection connection) {
		if(res != null) {
			try {
				res.close();
			}catch(Exception e) {
				e.printStackTrace();
			}
		}
		if(statement != null) {
			try {
				statement.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值