DBUtils方法详解02
来源:互联网 发布:你凭什么看不起程序员 编辑:程序博客网 时间:2024/06/01 09:40
上篇文章介绍了前五种DBUtils的方法,这篇文章为大家介绍剩下的五种,剩下的这五种是平时比较常用的
6、MapListHandler 每一行都是一个map,把map放入list里表示多行***
public static void test6() throws SQLException{QueryRunner qr = new QueryRunner(C3P0Utils.getDateSource());String sql = "select * from teacher";List<Map<String, Object>> list = qr.query(sql, new MapListHandler());for(int i = 0;i<list.size();i++){for(Map.Entry<String , Object> m:list.get(i).entrySet()){System.out.print(m.getKey()+" "+ m.getValue());}System.out.println();System.out.println("--------------------------------");}}7、ScalarHandler取单行单列***
它的构造方法的参数默认是1,意思是取第一行的第1列,你可以改为其他列,但都是第一行的。*****当查询的内容如果是count,那么实际的返回是Long
public static void test7() throws SQLException{QueryRunner qr = new QueryRunner(C3P0Utils.getDateSource());String sql = "select count(*) from student where ssex=?";Object obj = qr.query(sql, new ScalarHandler(),"男");System.out.println(obj);}8、BeanHandler*****返回一个实体对象,必须要创建了实体类
public static void test8() throws SQLException{QueryRunner qr = new QueryRunner(C3P0Utils.getDateSource());String sql = "select * from User where username=?";User user = qr.query(sql,new BeanHandler<User>(User.class),"zhangsan");System.out.println(user);}9、BeanListHandler*****看名字就能知道,这是取出多个实体对象最后返回的是一个List集合
public static void test9() throws SQLException{QueryRunner qr = new QueryRunner(C3P0Utils.getDateSource());String sql = "select * from user";List<User> list = qr.query(sql, new BeanListHandler<User>(User.class));for(int i =0;i<list.size();i++){System.out.println(list.get(i));}}10、最简单,也是最常用的增删改方法,insert、update、delete
public static void testInsert() throws SQLException{QueryRunner qr = new QueryRunner(C3P0Utils.getDateSource());String sql = "insert into user(username,password) values(?,?)";int i = qr.update(sql,"wangwu","123321");if(i>0){System.out.println("插入成功");}}public static void testUpdate() throws SQLException{QueryRunner qr = new QueryRunner(C3P0Utils.getDateSource());String sql = "update user set password=? where username=?";int i = qr.update(sql,"111111","wangwu");if(i>0){System.out.println("修改成功");}}public static void testDelete() throws SQLException{QueryRunner qr = new QueryRunner(C3P0Utils.getDateSource());String sql = "delete from user where username=?";int i = qr.update(sql,"wangwu");if(i>0){System.out.println("删除成功");}}这五种是平时比较常用的方法,大家需要加强记忆。
阅读全文
0 0
- DBUtils方法详解02
- Dbutils详解
- Dbutils 详解
- Dbutils 详解
- Dbutils详解
- Dbutils 详解
- Dbutils 详解
- Dbutils 详解
- Dbutils详解
- Dbutils 详解
- DBUtils用法详解
- Common Dbutils 详解
- Common Dbutils详解
- DBUtils应用详解
- DBUtils使用详解一
- DBUtils使用详解二
- Common Dbutils详解
- DbUtils API详解
- 自己封装跨浏览器事件添加和删除函数
- JQuery EasyUI-datagrid用法,行编辑模式
- SpringMvc 拦截器介绍
- JRebel for IDEA
- 【爬虫】selenium解析网页
- DBUtils方法详解02
- php,两个数组合并,用哪个函数?
- 模拟实现strstr(c语言)
- 实训笔记1
- 661. Image Smoother
- Listener监听器
- JS获取页面select元素
- hihocoder 1519 : 逃离迷宫II
- 线段树知识点