DBUtil工具类

1. 添加jar包

2. 获取数据源对象;

DataSource ds = DataSourceUtil.getDataSource();//自己写的工具类

3. 获取QueryRunner对象

QueryRunner qr = new QueryRunner(ds);

修改:runner.update(sql,parmates);

        String sql = "insert into student(s_name,phone,course) values(?,?,?)";
        int i = qr.update(sql, new Object[]{"夏利", "13298280685", 86.9});
        String sql = "update student set course=? where id=?";
        int i = qr.update(sql,90,1);  //返回的是受影响的行数

添加:runner.update(sql,parmates);
删除:runner.update(sql,parmates);
查询:
  查询所有:runner.query(sql,new BeanListHandle<>(类名.class));

String sql = "select * from student";
        List<Student> i = qr.query(sql, new BeanListHandler<Student>(Student.class));
        for (Student student : i) {
            System.out.println(student.toString());
        }

  根据id查询单个对象:runner.query(sql,new BeanHandle<>(类名.class),参数);

String sql = "select * from student where id=?";
        Student query = qr.query(sql, 1, new BeanHandler<Student>(Student.class));
       System.out.println(query.toString());

  返回单个值:runner.query(sql,new ScalarHandle<>(类名.class));

String sql ="select count(id) from student";
        long query = qr.query(sql, new ScalarHandler<>());
        System.out.println(query);

 

posted on 2020-11-21 21:30  Difcipo  阅读(99)  评论(0编辑  收藏  举报

导航