package com.oracle.dao;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.ArrayHandler;
import org.apache.commons.dbutils.handlers.ArrayListHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ColumnListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;
import com.oracle.fomain.Student;
import com.oracle.toos.JDBCUtilsToos;
import com.oracle.toos.MyDBUtis;
public class Studentdao {
public int add(Student student) throws SQLException{
Connection conn=JDBCUtilsToos.getconn();
QueryRunner qr=new QueryRunner();
String sql="insert into student values(?,?,?,?)";
int row=qr.update(conn,sql,student.getSid(),student.getSname(),student.getSsex(),student.getMoney());
return row;
}
//ArrayHandler只查一条
public void get1() throws SQLException{
Connection conn=JDBCUtilsToos.getconn();
QueryRunner qr=new QueryRunner();
String sql="select * from student";
Object [] obj=qr.query(conn,sql, new ArrayHandler());
for(Object o:obj){
System.out.println(o);
}
}
// ArrayListHandler 多条
public void get2() throws SQLException{
Connection conn=JDBCUtilsToos.getconn();
QueryRunner qr=new QueryRunner();
String sql="select * from student";
List<Object[]> list=qr.query(conn, sql,new ArrayListHandler());
for(Object[] objs:list){
for(Object o:objs){
System.out.print(o+"");
}
System.out.println();
}
}
// BeanHandler 只用于查一条记录
public void get3() throws SQLException{
Connection conn=JDBCUtilsToos.getconn();
QueryRunner qr=new QueryRunner();
String sql="select * from student";
Student student=qr.query(conn, sql,new BeanHandler<Student>(Student.class));
System.out.println(student);
}
// BeanLictHandler 查询多条
public void get4() throws SQLException{
Connection conn=JDBCUtilsToos.getconn();
QueryRunner qr=new QueryRunner();
String sql="select * from student";
List<Student> list=qr.query(conn, sql,new BeanListHandler<Student>(Student.class));
System.out.println(list);
}
// ColumnListHandler 查询有几列
public void get5() throws SQLException{
Connection conn=JDBCUtilsToos.getconn();
QueryRunner qr=new QueryRunner();
String sql="select * from student";
List<String> list=qr.query(conn, sql, new ColumnListHandler<String>());
System.out.println(list);
}
// ScalarHandler查询count
public void get6() throws SQLException{
QueryRunner qr=new QueryRunner(MyDBUtis.getDataSource());
String sql="select count(*) from student";
Long count=qr.query( sql, new ScalarHandler<Long>());
System.out.println(count.intValue());
}
}
package com.oracle.test;
import java.sql.SQLException;
import com.oracle.dao.Studentdao;
import com.oracle.fomain.Student;
public class Demo01 {
public static void main(String[] args) throws SQLException {
// Student student=new Student();
// student.setSid(12);
// student.setSname("海绵包阿伯");
// student.setSsex("男");
// student.setMoney(1000);
// int row=new Studentdao().add(student);
//System.out.println(row);
new Studentdao().get6();
}
}