在鏈接數據庫的時候(MySQL)時,會遇到的問題。java
調用QueryRunner的方法是:sql
query(Connection con,String Sql,ResultSetHandler r,Object.params)
將結果集每一行存儲到Map集合中,鍵:列名;值:數據數據庫
public static void mapListHandler()throws SQLException{ QueryRunner qr = new QueryRunner(); String sql = "select * from db"; Lsit<Map<String,Object>> list = qr.query(con,sql,new MapListHandler()); for(Map<String,Object> map:list){ System.out.print(key+'..'+map.get(key)); } System.out.prinln(); }
將結果集的第一行數據,封存到Map集合中數組
public static void mapHandler()throws SQLException{ QueryRunner qr = new QueryRunner(); String sql = "select * from db"; Map map = qr.query(con,sql,new MapHandler()); for(String key:map.keySet()){ System.out.print(key+'..'+map.get(key)); } System.out.prinln(); }
查詢後,只有一個結果scala
Public static void scalarHandler() throws SQLException(){ QueryRunner qr = new QueryRunner(); String sql = "select * from db"; long count = qr.query(con,sql,new ScalarHandler()); System.out.prinln(count); }
結果集,指定列的數據存儲到List集合中,code
List每一個列數據類型不一樣。對象
public static void columnListHandler()throws SQLException(){ QueryRunner qr = new QueryRunner(); String sql = "select * from db"; List<Object> list = qr.query(con,sql,new ColumnListHandler<Object>("name")); for(Object obj:list){ System.out.println(obj); } System.out.prinln(obj); }
結果集每一行數據,封裝JavaBean對象get
多個JavaBean對象,存儲到List集合it
public static void columnListHandler()throws SQLException(){ QueryRunner qr = new QueryRunner(); String sql = "select * from db"; List list = qr.query(con,sql,new BeanListHandler<Sort>(Sort.class)); for(Sort s:list){ System.out.println(s); } }
將結果集的第一行數據,封裝成JavaBean對象io
public static void beanHandler()throws SQLException{ QueryRunner qr = new QueryRunner(); String sql = "SELECT * FROM sort "; Sort s = qr.query(con, sql, new BeanHandler<Sort>(Sort.class)); System.out.println(s); }
將結果集的每一行,封裝到對象數組中
public static void arrayListHandler()throws SQLException{ 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+" "); } System.out.println(); } }
將結果集的第一行存儲到對象數組中 Object[]
public static void arrayHandler()throws SQLException{ QueryRunner qr = new QueryRunner(); String sql = "SELECT * FROM sort"; Object[] result = qr.query(con, sql, new ArrayHandler()); for(Object obj : result){ System.out.print(obj); } } }