目录
一、简介
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.