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.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 Student1 {
// 存值
public int add(Student student) throws SQLException{
QueryRunner qr=new QueryRunner(MyDBUtis.getDataSource());
String sql="insert into student values(?,?,?,?) ";
int row=qr.update(sql,student.getSid(),student.getSname(),student.getSsex(),student.getMoney());
return row;
}
//BeanHandler 只用于查一条记录
public void get1() throws SQLException{
QueryRunner qr=new QueryRunner(MyDBUtis.getDataSource());
String sql="select * from student";
Student student=qr.query(sql, new BeanHandler<Student>(Student.class) );
System.out.println(student);
}
//BeanLictHandler 查询多条
public void get2() throws SQLException{
QueryRunner qr=new QueryRunner(MyDBUtis.getDataSource());
String sql="select * from student";
List<Student> list=qr.query(sql, new BeanListHandler<Student>(Student.class));
System.out.println(list);
}
//ColumnListHandler 查询有几列
public void get3() throws SQLException{
QueryRunner qr=new QueryRunner(MyDBUtis.getDataSource());
String sql="select sname from student";
List<String> list=qr.query(sql, new ColumnListHandler<String>());
System.out.println(list);
}
//ScalarHandler查询count
public void get4() 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);
}
}
package com.oracle.test;
import java.sql.SQLException;
import com.oracle.dao.Student1;
import com.oracle.fomain.Student;
public class Demo02 {
public static void main(String[] args) throws SQLException {
// Student student=new Student();
// student.setSid(31);
// student.setSname("张三");
// student.setSsex("男");
// student.setMoney(1000);
// int row=new Student1().add(student);
// System.out.println(row);
new Student1().get1();
new Student1().get2();
new Student1().get3();
new Student1().get4();
}
}