//DBUtils 工具类,简化了JDBC的操作
//QueryRunner 实现增加、删除、修改操作
import day02.JdbcUtils3;
import day03.Student;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

public class CRUD {
    //创建一个全局的QueryRunner对象
   private  static QueryRunner qr=new QueryRunner();
   //学员信息添加的方法
    public static void  insert(){
        //获取Connection对象
        try(Connection  con= JdbcUtils3.getConnection();){
        //准备执行的SQL语句
         String  sql="insert  into student(name,age,Gender,birthday)values(?,?,?,?)";
         Object[]prams={"桑凤娇",22,"女","2000-09-20"};
         int i=qr.update(con,sql,prams);
         if(i>0){
             System.out.println("学员信息添加成功");
         }else{
             System.out.println("学员信息添加失败");
         }
        }catch(SQLException  ce){
            ce.printStackTrace();
        }
    }
    //学员信息删除的方法
    public static  void delete(){
        //获取Connection对象
        try(Connection  con=JdbcUtils3.getConnection()){
         //准备SQL语句
         String  sql="delete  from student  where  id=?";
         Object[]prams={4};
         int  i=qr.update(con,sql,prams);
         if(i>0){
             System.out.println("删除成功");
         }else{
             System.out.println("删除失败");
         }
        }catch(SQLException  ce){
            ce.printStackTrace();
        }
    }
    //学员信息修改的方法
    public static  void update(){
        //获取Connection对象
        try(Connection  con=JdbcUtils3.getConnection()){
         //SQl语句
         String sql="update student set name=?,age=?,Gender=?,birthday=? where id=?";
         Object[]prams={"王智雅",20,"女","1998-01-04",7};
         int i=qr.update(con,sql,prams);
         if(i>0){
             System.out.println("修改成功");
         }else{
             System.out.println("修改失败");
         }
        }catch(SQLException  ce){
            ce.printStackTrace();
        }
    }
    //查询表中所有的信息(new BeanListHandler)
    public static  void  findAll(){
        //获取Connection对象
        try(Connection  con=JdbcUtils3.getConnection()){
          //查询的SQL语句
          String sql="select  *  from  student";
          List<Student>list=qr.query(con,sql,new BeanListHandler<Student>(Student.class));
          for(Student ss:list){
              System.out.println(ss);
          }
        }catch(SQLException  ce){
            ce.printStackTrace();
        }
    }
    //只查询表中的一条记录那么就使用:new BeanHandler
    public  static  void findFidy(int id){
        //获取Connection对象
        try(Connection  con=JdbcUtils3.getConnection()){
         //准备SQl语句,根据id去查询
         String sql="select  *  from  student  where  id=?";
         Object[]prams={1};
         //beanHandler返回的是Student对象
         Student student=qr.query(con,sql,new BeanHandler<Student>(Student.class),prams);
            System.out.println(student);
        }catch (SQLException  ce){
            ce.printStackTrace();
        }
    }
    public static void main(String[] args) {
        CRUD.findFidy(1);
    }
}
12-04 08:48