dbUtils工具的使用
来源:互联网 发布:python sys.exit 2 编辑:程序博客网 时间:2024/06/11 19:37
使用dbUtils需要倒入第三方包:commons-dbutils-1.4.jar(基本包)和commons-dbutils-ext.jar(扩展包)
public class DbUtilsDemo { @Test//普通方式---不用dbUtils的查询 public void jdbcQuery() throws SQLException{ Connection con = C3p0Pool.getConnection(); String sql = "select * from student"; Statement st = con.createStatement(); ResultSet rs = st.executeQuery(sql); List<Student>students = new ArrayList<Student>(); while(rs.next()){ Student student = new Student(); student.setId(rs.getString("id")); student.setSname(rs.getString("sname")); student.setSex(rs.getString("sex")); student.setAge(rs.getInt("age")); student.setCls(rs.getString("cls")); students.add(student); } System.out.println(students); } @Test public void dbUtilsQuery() throws SQLException{ QueryRunner run = new QueryRunner(C3p0Pool.getDataSource()); String sql = "select * from student"; List<Student>students = run.query(sql, new BeanListHandler<Student>(Student.class)); System.out.println(students); } @Test public void dbUtilsQuery2() throws SQLException{ QueryRunner run = new QueryRunner(C3p0Pool.getDataSource()); String sql = "select * from student"; List<Map<String,Object>>students = run.query(sql, new MapListHandler()); System.out.println(students); } @Test public void save() throws SQLException{ QueryRunner run = new QueryRunner(C3p0Pool.getDataSource()); //String sql = "insert into person2(id,name,age,address) values('A001','张三',22,'湖南长沙')"; String sql = "insert into person2(id,name,age,address) values(?,?,?,?)"; run.update(sql,"A002","李四",23,"湖北武汉"); } @Test public void saveTx() throws SQLException{ QueryRunner run = new QueryRunner(); Connection con = C3p0Pool.getConnection(); try{ con.setAutoCommit(false); String sql1 = "insert into person2(id,name,age,address) values(?,?,?,?)"; run.update(con,sql1,"A005","钱七",27,"广东广州"); String sql2 = "insert into person2(id,name,age,address) values('A004','赵六',25,'湖南宁乡')"; run.update(con,sql2); System.out.println("事务已提交..."); }catch (Exception e) { con.rollback(); System.out.println("事务已回滚..."); }finally{ con.setAutoCommit(true); con.close(); } } @Test//演示批处理功能 public void batch() throws Exception{ QueryRunner run = new QueryRunner(C3p0Pool.getDataSource()); for(int i=1;i<=300;i++){ String sql = "insert into person2(id,name,age,address) values(?,?,?,?) "; String str= "000"+i; str = str.substring(str.length()-3, str.length()); String id1 = "B"+ str; String id2 = "C"+ str; Object params[][] = new Object[][]{{id1,"Tom"+str,(i+10)%100,"湖南"+str},{id2,"Jack"+str,(i+10)%100,"湖北"+str}}; run.batch(sql, params); } } @Test public void extQuery(){ ExtQueryRunner run = new ExtQueryRunner(C3p0Pool.getDataSource()); List<Person>persons = run.query(Person.class); System.out.println(persons); } @Test public void extSave(){ ExtQueryRunner run = new ExtQueryRunner(C3p0Pool.getDataSource()); Person p = new Person(); p.setId("C301"); p.setName("Jack301"); p.setAge(21); p.setAddress("湖北301"); run.update(p); }}
0 0
- DbUtils工具的使用
- dbUtils工具的使用
- DBUtils工具的使用
- DButils工具类库的使用
- Dbutils工具基本使用
- commons-dbutils 工具使用
- JDBC : 使用DBUtils 工具类
- 关于Apache Commons DbUtils工具类的使用记录
- JDBC相关(三):DButils工具类的使用详解
- Java的DBUtils工具演示
- dbutils的使用
- DBUtils框架的使用
- DButils的使用感受
- DbUtils 的常规使用
- common-dbutils的使用
- Dbutils 的使用
- DButils 的使用
- apache--dbutils的使用
- iOS使用AFNetWorking第三库检测网络连接状态
- Cucumber中涉及到的类型转换
- weblogic控制台配置修改
- Rails权限验证工具Pundit
- ABBYY 内置自动化任务的步骤有哪些
- dbUtils工具的使用
- #202 Segment Tree Query
- RxJava Retrofit2 网络请求,返回数据格式统一的数据报文处理方法
- JS中的立即执行函数
- C#中那些[举手之劳]的性能优化
- 使用Guava+Spring实现异步回调操作,提高程序性能
- TCP回射1-----线程
- mysql 双主配置 基于docker 测试 及数据恢复测试
- C\C++中结构体变量与结构体指针内存分配问题