三大核心类
QueryRunner中提供对sql语句操作的API.
ResultSetHandler接口,用于定义select操作后,怎样封装结果集.
DbUtils类,它就是一个工具类,定义了关闭资源与事务处理的方法
QueryRunner
数据库新增:
//1定义QueryRunner 核心类 QueryRunner query = new QueryRunner(); //2、执行QueryRunner update方法执行sql语句 Connection conn = JdbcUtil.con(); String sql = "insert into stu(no,name,subject,score) values(?,?,?,?)"; Object[] param = {1,"tom1","C#",99}; try { int i = query.update(conn, sql, param); int j = query.update(JdbcUtil.con(), "insert into stu(no,name,subject,score) values(?,?,?,?)", 2,"tom2","C#",100); System.out.println("结果为"+i+" "+j); } catch (SQLException e) { e.printStackTrace(); }finally { //3、释放资源 try { DbUtils.close(conn); } catch (SQLException e) { e.printStackTrace(); } }
数据库更新:
//1定义QueryRunner 核心类 QueryRunner query = new QueryRunner(); //2、执行QueryRunner update方法执行sql语句 try { int i = query.update(JdbcUtil.con(),"update stu set name=? where no=?","mao",1); System.out.println("影响的行数:"+i); } catch (SQLException e) { e.printStackTrace(); }
数据库删除:
//1定义QueryRunner 核心类 QueryRunner query = new QueryRunner(); //2、执行QueryRunner update方法执行sql语句 try { int i = query.update(JdbcUtil.con(),"delete from stu where no=?",1); System.out.println("结果为:"+i); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }
ResultSetHandler(八大类)
一、ArrayHandler()//查询返回值为数据Object[]
QueryRunner query = new QueryRunner(); try { //获取第一行数据存入数组中 Object[] obj = query.query(JdbcUtil.con(),"select * from stu",new ArrayHandler()); System.out.println(obj[0]); } catch (SQLException e) { e.printStackTrace(); }
二、 ArrayListHandler()//获取每一行存入数组中,在将数据存入List集合中
QueryRunner query = new QueryRunner(); try { //获取每一行存入数组中,在将数据存入List集合中 List<Object[]> obj = query.query(JdbcUtil.con(),"select * from stu",new ArrayListHandler()); for(Object[] objects : obj){ for(int i=0;i< objects.length;i++){ System.out.println(objects[i]); } } } catch (SQLException e) { e.printStackTrace(); }
三、BeanHandler()//将数据库表中一条数据封装到指定的javaBean中
//1、定义核心类 QueryRunner query = new QueryRunner(); //2、执行查询语句 try { Stu stu = query.query(JdbcUtil.con(), "select * from stu", new BeanHandler<Stu>(Stu.class)); System.out.println(stu.getNo()+" "+stu.getName()); } catch (SQLException e) { e.printStackTrace(); }
四、BeanListHandler()//得到数据库中每条数据并封装到javaBean集合中
//1、定义核心类 QueryRunner query = new QueryRunner(); //2、执行查询语句 try { List<Stu> list = query.query(JdbcUtil.con(), "select * from stu", new BeanListHandler<Stu>(Stu.class)); for(Stu s : list){ System.out.println(s.getNo()+"====="+s.getName()); } } catch (SQLException e) { e.printStackTrace(); }
五、ColumnListHandler()//将结果集中指定的数据封装到List集合中
//1、定义核心类 QueryRunner query = new QueryRunner(); //2、执行查询语句 try { List<Object> list = query.query(JdbcUtil.con(), "select * from stu", new ColumnListHandler<Object>("name")); for(Object obj : list){ System.out.println(obj); } } catch (SQLException e) { e.printStackTrace(); }
六、ScalarHandler()//用于处理单数据,用于执行select count(*) from stu的操作
//1、定义核心类 QueryRunner query = new QueryRunner(); //2、执行查询语句 try { Object obj = query.query(JdbcUtil.con(), "select count(*) from stu", new ScalarHandler<Object>()); System.out.println(obj); } catch (SQLException e) { e.printStackTrace(); }
七、MapHandler()//以字段名为键,以内容为值,只取第一行。
//1、定义核心类 QueryRunner query = new QueryRunner(); //2、执行查询语句 try { Map<String,Object> map = query.query(JdbcUtil.con(), "select * from stu", new MapHandler()); for(String key : map.keySet()){ System.out.println("键"+key+"值"+map.get(key)); } } catch (SQLException e) { e.printStackTrace(); }
八、MapListHandler()//将数据库中每一条数据存入Map集合中
//1、定义核心类 QueryRunner query = new QueryRunner(); //2、执行查询语句 List<Map<String, Object>> list; try { list = query.query(JdbcUtil.con(), "select * from stu",new MapListHandler()); for(Map<String,Object> map :list){ for(String key : map.keySet()){ System.out.println("键"+key+"值"+map.get(key)); } } } catch (SQLException e) { e.printStackTrace(); }