dbUtils工具类

1、dbUtils是什么?

        dbUtils包是Apache下的commons的一个工具包,主要用来对jdbc程序的补充,是一个简单的jdbc小类库。能够简化JDBC应用程序的开发,同时也不影响程序的性能。

2、dbUtils能做什么?

        它能代替我们处理结果集,将结果集封装成我们想要的东西。(实体bean对象)

3.怎么去用呢?

        第一步:导入Commons-dbutils-1.6.jar包

       第二步:就可以在代码中去运用了。

具体代码奉上,了解一哈(增删改查的操作):

    

/**
     * dbUtil工具类封装
     * 获取所有的用户列表
     * @return 返回所有的用户列表
     * 多条查询
     */
   public static List<User>get(){
        //Connection connection = MyUtils.getConnection();
       Connection connection = null;
       try {
           connection = C3p0Util.getConnection();
       } catch (SQLException e) {
           e.printStackTrace();
       }
       //获取执行sql语句的对象
        QueryRunner queryRunner = new QueryRunner();
        String sql = "select * from user";
        /*工具类提供的两种方法*/
        //queryRunner.update();  //对数据库进行更改操作
        //queryRunner.query();   //对数据库进行查询操作
        List<User> list=null;
        try {
           /*将User对象传入进去,并将User的可执行文件反馈给该对象*/
            list = queryRunner.query(connection, sql, new BeanListHandler<User>(User.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
            return list;
}

    /**
     * 单条查询
     * @return 返回所查询的的用户列表
     */
    public static User getone(){
        Connection connection = MyUtils.getConnection();
        QueryRunner queryRunner = new QueryRunner();
        String sql = "select * from user where userId = ?";
        Object [] params = {"123"};
        User user = null;
        try {
            user=queryRunner.query(connection,sql,params,new BeanHandler<User>(User.class));
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return user;
    }

    /**
     * dbUtil工具类封装添加的操作
     * @return 返回受影响的行数,0—表示添加失败,1—表示添加成功
     */
    public static int add(){
        Connection connection = MyUtils.getConnection();
        String sql = "insert into user (userId,password,admin,phone,state,unit,breakfast,lunch,supper) values (?,?,?,?,?,?,?,?,?)";
        //获取执行sql语句的对象
        QueryRunner queryRunner = new QueryRunner();
        Object [] params = {"100","124","124","1234548","0","0","0","1","1"};
        int row = 0;
        try {
            row = queryRunner.update(connection,sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;
    }

    /**
     * 利用dbUntils封装工具类实现删除的操作
     * @return 返回受影响的行数 0—表示删除失败 1—表示删除成功
     */
    public static int delete(){
        QueryRunner queryRunner = new QueryRunner(C3p0Util.getDataSource());
        String sql = "delete from user where userId = ?";
        Object [] params = {"121"};
        int row = 0;
        try {
            row = queryRunner.update(sql,params);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;
    }

    /**
     * 利用dbUntils封装工具类实现修改的操作
     * @return 返回受影响的行数 0—表示修改失败 1—表示修改成功
     */
    public static int update(){
        Connection connection = MyUtils.getConnection();
        QueryRunner queryRunner = new QueryRunner();
        String sql = "update user set password = ?,admin = ?,phone = ?,state = ?,unit =?,breakfast =?,lunch =?,supper=? where userId = ?";
        Object [] params = {"119","119","119",1,"2",12,1,0,"124"};
        int row = 0;
        try {
            row = queryRunner.update(connection,sql,params);
            //DbUtil封装的一个工具类,释放资源
            DbUtils.closeQuietly(connection);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return row;
    }



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值