1 // 这个是在添加数据 2 @Test 3 public void demo1() { 4 QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource()); 5 String sql = "insert into Book values(?,?)"; 6 try { 7 int num = qr.update(sql, 26, "明朝那些事"); 8 if (num > 0) { 9 System.out.println("添加成功"); 10 } else { 11 System.out.println("添加失败"); 12 } 13 } catch (SQLException e) { 14 // TODO Auto-generated catch block 15 e.printStackTrace(); 16 } 17 } 18 19 // 修改数据 20 @Test 21 public void demo2() { 22 QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource()); 23 try { 24 String sql = "update Book set bname=? where id=?"; 25 int num = queryRunner.update(sql, "史记", 29); 26 if (num > 0) { 27 System.out.println("添加成功"); 28 } else { 29 System.out.println("添加失败"); 30 } 31 } catch (Exception e) { 32 e.printStackTrace(); 33 } 34 } 35 36 // 下面的是查询语句 37 // 将数据表中的第一条数据封装成object 38 @Test 39 public void demo3() { 40 QueryRunner queryRunner = new QueryRunner(JDBCUtils.getDataSource()); 41 try { 42 String sql = "select * from Book"; 43 Object[] objects = queryRunner.query(sql, new ArrayHandler()); 44 45 System.out.println(Arrays.toString(objects)); 46 } catch (Exception e) { 47 e.printStackTrace(); 48 } 49 } 50 51 //将数据表中的每一条数据封装成object new ArrayListHandler() 52 @Test 53 public void demo4(){ 54 QueryRunner queryRunner=new QueryRunner(JDBCUtils.getDataSource()); 55 try { 56 String sql="select * from Book"; 57 List<Object[]> list=queryRunner.query(sql,new ArrayListHandler()); 58 59 for(Object[] objects:list){ 60 System.out.println(Arrays.toString(objects)); 61 } 62 63 } catch (Exception e) { 64 e.printStackTrace(); 65 } 66 } 67 68 69 //把数据表中的第一条数据封装成JavaBean实体类对象 BeanHandler 70 @Test 71 public void demo5(){ 72 QueryRunner queryRunner=new QueryRunner(JDBCUtils.getDataSource()); 73 try { 74 String sql="select * from Book"; 75 Book list=queryRunner.query(sql,new BeanHandler<>(Book.class)); 76 77 System.out.println(list.toString()); 78 } catch (Exception e) { 79 e.printStackTrace(); 80 } 81 } 82 83 84 //把数据表中的每一条数据封装成JavaBean实体类对象 BeanListHandler 85 @Test 86 public void demo6(){ 87 QueryRunner queryRunner=new QueryRunner(JDBCUtils.getDataSource()); 88 try { 89 String sql="select * from Book"; 90 List<Book> list=queryRunner.query(sql,new BeanListHandler<>(Book.class)); 91 92 for(Book book:list) 93 System.out.println(book.toString()); 94 } catch (Exception e) { 95 e.printStackTrace(); 96 } 97 } 98 99 //把数据表中的第一条数据封装成Map集合 ,键:列名 值:该列的值 new MapHandler() 100 @Test 101 public void demo7(){ 102 QueryRunner queryRunner=new QueryRunner(JDBCUtils.getDataSource()); 103 try { 104 String sql="select * from Book"; 105 Map<String,Object > map=queryRunner.query(sql,new MapHandler()); 106 107 System.out.println(map); 108 } catch (Exception e) { 109 e.printStackTrace(); 110 } 111 } 112 113 //把数据表中的每一条数据封装成Map集合 ,键:列名 值:该列的值 new MapListHandler() 114 @Test 115 public void demo8(){ 116 QueryRunner queryRunner=new QueryRunner(JDBCUtils.getDataSource()); 117 try { 118 String sql="select * from Book"; 119 List<Map<String,Object >>list=queryRunner.query(sql,new MapListHandler()); 120 for(Map<String,Object>map:list) 121 System.out.println(map); 122 } catch (Exception e) { 123 e.printStackTrace(); 124 } 125 } 126 127 // ColunmListHandler: 把数据表中的没一条数据封装成 Map集合, 键:列名 值:该列的值 , 然后把每一行的Map集合装入到一个List集合中. 128 @Test 129 public void demo9() { 130 // 1. 获取可以执行SQL语句的对象: QueryRunner 131 QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource()); 132 // 2. 编写需要执行的SQL语句 133 String sql = "select * from Book;"; 134 // 3. 执行SQL语句, 获取结果集 135 try { 136 List<Object> list = qr.query(sql, new ColumnListHandler("bname")); 137 System.out.println(list); 138 } catch (SQLException e) { 139 e.printStackTrace(); 140 } 141 } 142 143 // ScalarHandler: 把数据表中的没一条数据封装成 Map集合, 键:列名 值:该列的值 , 然后把每一行的Map集合装入到一个List集合中. 144 @Test 145 public void demo10() { 146 // 1. 获取可以执行SQL语句的对象: QueryRunner 147 QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource()); 148 // 2. 编写需要执行的SQL语句 149 String sql = "select count(*) from Book;"; 150 // 3. 执行SQL语句, 获取结果集 151 try { 152 Object obj = qr.query(sql, new ScalarHandler()); 153 System.out.println(obj); 154 } catch (SQLException e) { 155 e.printStackTrace(); 156 } 157 } 158 159 // KeyedHandler: 把数据表中的没一条数据封装成 Map集合, 键:列名 值:该列的值 , 然后把每一行的Map集合装入到一个List集合中. 160 @Test 161 public void demo11() { 162 // 1. 获取可以执行SQL语句的对象: QueryRunner 163 QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource()); 164 // 2. 编写需要执行的SQL语句 165 String sql = "select * from Book;"; 166 // 3. 执行SQL语句, 获取结果集 167 try { 168 Map<Object, Map<String, Object>> map = qr.query(sql, new KeyedHandler("bname")); 169 for (Object key : map.keySet()) { 170 System.out.println(key + "..." + map.get(key)); 171 } 172 173 } catch (SQLException e) { 174 e.printStackTrace(); 175 } 176 }
时间: 2024-10-10 05:18:12