时间:2021-07-01 10:21:17 帮助过:26人阅读
/ArrayListHandler处理结果集 private static void arrayListHandlerM() throws SQLException { Connection con = JDBCUtilsss.getConnection(); //获取QueryRunner对象 QueryRunner qr = new QueryRunner(); String sql = "select * from sort"; //将获取的结果集存入集合,集合中存入每条语句构成的数组 List<Object[]> result = qr.query(con, sql, new ArrayListHandler()); for (Object[] objs : result) { for (Object obj : objs) { System.out.print(obj+"\t"); } System.out.println(); } }
//BeanListHandler private static void beanListHandlerM() throws SQLException { Connection con = JDBCUtilsss.getConnection(); //获取QueryRunner对象 QueryRunner qr = new QueryRunner(); String sql = "select * from sort"; List<Sort> s = qr.query(con, sql, new BeanListHandler<Sort>(Sort.class)); for (Sort ss : s) { System.out.println(ss); } }
//ColumnListHandler private static void columnListHandlerM() throws SQLException { Connection con = JDBCUtilsss.getConnection(); //获取QueryRunner对象 QueryRunner qr = new QueryRunner(); String sql = "select * from sort"; List<Object>list = qr.query(con, sql, new ColumnListHandler<Object>("sname")); for (Object s : list) { System.out.println(s); } }
//ScalarHandler private static void scalarHandlerM() throws SQLException { Connection con = JDBCUtilsss.getConnection(); //获取QueryRunner对象 QueryRunner qr = new QueryRunner(); String sql = "select count(*) from sort"; Object count = qr.query(con, sql, new ScalarHandler<Object>()); System.out.println(count); }
//MapListHandler private static void mapListHandlerM() throws SQLException { Connection con = JDBCUtilsss.getConnection(); //获取QueryRunner对象 QueryRunner qr = new QueryRunner(); String sql ="select * from sort"; List<Map<String,Object>> list = qr.query(con, sql, new MapListHandler()); //获取到每个Map,遍历得到信息 for (Map<String, Object> map : list) { for (Object keys : map.keySet()) { System.out.print(keys+".."+map.get(keys)); } System.out.println(); } }
JDBCUtils
标签:工具 .class load dbutil 字段 date 执行 param 成功