QueryRunner
的update
方法 update(Connection conn, String sql, Object... params)
能够实现对表中数据的增删改,返回int类型的数值,表示对数据表影响的行数。conn
为传入的Connection
连接对象,sql
为要执行的sql
语句,Object... params
为sql
中的占位符添加参数,可以传入一个Object
数组或者直接传入参数。
//QueryRunner的insert()方法
public static void insert() throws SQLException{
//创建queryrunner对象
QueryRunner queryRunner = new QueryRunner();
String sql = "INSERT INTO goods_class (gcname,gcdescribe)VALUES(?,?)";
//创建object数组
Object[] params = {"张三","李四"};
//执行queryrunner的插入方法
int row = queryRunner.update(connection, sql, params);
}
//QueryRunner的delete()方法
public static void delete() throws SQLException {
//创建queryrunner对象
QueryRunner queryRunner = new QueryRunner();
String sql = "delete from goods_class where gcid = ?";
int row = queryRunner.update(connection, sql, 6);
}
//QueryRunner的update()方法
public static void update() throws SQLException {
//创建queryrunner对象
QueryRunner queryRunner = new QueryRunner();
String sql = "UPDATE goods_class SET gcname=? WHERE gcid=?";
//创建object数组
Object[] params = {"王五",6};
int row = queryRunner.update(connection, sql, params);
}
QueryRunner
中的query
方法query(Connection conn, String sql, ResultSetHandler
能够对数据表中的数据进行查询,返回一个结果集,在不同的情况下可以使用不同的结果集。conn
为传入的Connection
连接对象,sql
为要执行的sql
语句,Object... params
为sql
中的占位符添加参数,可以传入一个Object
数组或者直接传入参数,ResultSetHandler
为返回的结果集类型。
ArrayHandler
public static void arrayHandler() throws SQLException {
//1.创建queryrunner对象
QueryRunner queryRunner = new QueryRunner();
String sql = "SELECT * FROM goods_class";
//调用queryrunner的query查询方法
Object[] result = queryRunner.query(connection, sql, new ArrayHandler());
for(Object obj:result) {
System.out.print(obj + " ");
}
}
ArrayListHandler
public static void arrayListHandlder() throws SQLException{
//1.创建queryrunner对象
QueryRunner queryRunner = new QueryRunner();
String sql = "SELECT * FROM goods_class";
//调用queryrunner的query查询方法
List<Object[]> result = queryRunner.query(connection, sql, new ArrayListHandler());
for(Object[] objects:result) {
for(Object object:objects) {
System.out.print(object + "\t");
}
System.out.println();
}
}
BeanHandler
get
和set
方法,必须提供无参构造器。该类实例对象的字段数据为查询数据的第一行数据。传入形参为改类的类加载器。可用于查询某一行数据。public static void beanHandler() throws SQLException {
//1.创建queryrunner对象
QueryRunner queryRunner = new QueryRunner();
String sql = "SELECT * FROM goods_class";
//调用queryrunner的query查询方法
Good_class gc = queryRunner.query(connection, sql, new BeanHandler<Good_class>(Good_class.class));
System.out.println(gc);
}
BeanListHandler
get
和set
方法,必须提供无参构造器。该类实例对象的字段数据为查询数据的一行数据。传入形参为改类的类加载器。public static void beanListHandler() throws SQLException{
//1.创建queryrunner对象
QueryRunner queryRunner = new QueryRunner();
String sql = "SELECT * FROM goods_class";
//调用queryrunner的query查询方法
List<Good_class> result = queryRunner.query(connection, sql, new BeanListHandler<Good_class>(Good_class.class));
for(Good_class good_class:result) {
System.out.println(good_class);
}
}
ColumnListHandler
public static void columnListHandler() throws SQLException {
//1.创建queryrunner对象
QueryRunner queryRunner = new QueryRunner();
String sql = "SELECT * FROM goods_class";
//调用queryrunner的query查询方法
List<Object> result= queryRunner.query(connection, sql, new ColumnListHandler<Object>("gcname"));
for (Object object : result) {
System.out.println(object);
}
}
ScalarHandler
public static void scalarListHandler() throws SQLException{
//1.创建queryrunner对象
QueryRunner queryRunner = new QueryRunner();
String sql = "SELECT SUM(gcid) FROM goods_class";
//调用queryrunner的query查询方法
Object object = queryRunner.query(connection, sql, new ScalarHandler<Object>());
System.out.println(object);
}
MapHandler
public static void mapHandler() throws SQLException{
//1.创建queryrunner对象
QueryRunner queryRunner = new QueryRunner();
String sql = "SELECT * FROM goods_class";
//调用queryrunner的query查询方法
Map<String,Object> result = queryRunner.query(connection, sql, new MapHandler());
for(String key:result.keySet()) {
System.out.println(key + " " + result.get(key));
}
}
MapListHandler
返回所有的对象map,key
为列名,value
为该列数据,并将map
装入列表中。public static void mapListHandler() throws SQLException{
//1.创建queryrunner对象
QueryRunner queryRunner = new QueryRunner();
String sql = "SELECT * FROM goods_class";
//调用queryrunner的query查询方法
List<Map<String, Object>> result = queryRunner.query(connection, sql, new MapListHandler());
for (Map<String, Object> map : result) {
for(String key:map.keySet()) {
System.out.print(key + "\t" + map.get(key) + "\t");
}
System.out.println();
}
}