TestDBUtil.java
package day20;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import com.mysql.jdbc.Connection;
public class TestDBUtilSelect {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
testDBUtilSelect();
}
public static void testDBUtilSelect() throws Exception {
//注册驱动
DBManager dbManager=new DBManager();
//获取连接
Connection conn=dbManager.getConnection();
String sql="select id,姓名,email from students ";
/*
* 执行sql的类,该类中提供了增删改查语句的方法
* query(conn,sql,rsh)第三个参数:转化结果集--->对象ResultHandler
* ResultHandler实现类接口的返回值就是query 方法的返回值
* query.query(conn, sql,new ResultSetHandlerImpl())
* *该方法的返回值就是ResultHandler实现类接口ResultSetHandlerImpl的
* *的方法public Object handle(ResultSet rs) throws SQLException的返回值
* * 是由底层开发者调用,不需要自己调用
*/
QueryRunner query=new QueryRunner();
Object obj=query.query(conn, sql,new ResultSetHandlerImpl());
System.out.println("obj=== "+obj);
// Person p1=(Person)obj;
// System.out.println(p1.getId()+" "+p1.get姓名()+" "+p1.getEmail());
List<Person> list1=(List)obj;
for(Person p:list1){
System.out.println(p.getId()+" "+p.get姓名()+" "+p.getEmail());
}
System.out.println("--------D调的分割线-----------");
List<Person> list=(List)query.query(conn,sql,new BeanListHandler(Person.class));
for(Person p:list){
System.out.println(p.getId()+" "+p.get姓名()+" "+p.getEmail());
}
dbManager.closeResource(conn, null, null);
}
}
通用类DBManager.java
package day20;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import com.mysql.jdbc.Connection;
import com.mysql.jdbc.PreparedStatement;
public class DBManager {
private String url="jdbc:mysql://localhost:3306/test";
Connection conn=null;
public DBManager() throws Exception{
Class.forName("com.mysql.jdbc.Driver");
}
public Connection getConnection() throws SQLException{
conn=(Connection) DriverManager.getConnection(url, "root", "");
return conn;
}
public void closeResource(Connection conn, PreparedStatement pstmt,
ResultSet rs) throws SQLException {
if(rs!=null){
rs.close();
}
if(pstmt!=null){
pstmt.close();
}
if(conn!=null){
conn.close();
}
}
public void closeResource(Connection conn, Statement pstmt,
ResultSet rs) throws SQLException {
if(rs!=null){
rs.close();
}
if(pstmt!=null){
pstmt.close();
}
if(conn!=null){
conn.close();
}
}
}
Person.java
package day20;
public class Person {
private Integer id;
private String 姓名;
private String email;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String get姓名() {
return 姓名;
}
public void set姓名(String 姓名) {
this.姓名 = 姓名;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
可选类:ResultSetHandlerImpl.java
package day20;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
import org.apache.commons.dbutils.ResultSetHandler;
public class ResultSetHandlerImpl implements ResultSetHandler{
@Override
public Object handle(ResultSet rs) throws SQLException {
System.out.println("ResultSetHandlerImpl****************");
//String str="abcdef";
//return str;
// Person p=new Person();
// p.setEmail("abc@126.com");
// p.setId(100);
// p.set姓名("小芳");
// return p;
List<Person> list=new ArrayList<Person>();
while(rs.next()){
Person p=new Person();
p.setEmail(rs.getString(3));
p.setId(rs.getInt(1));
p.set姓名(rs.getString(2));
list.add(p);
}
return list;
}
}