</div>
/** * 测试 QueryRunner 类的 update 方法 * 该方法可用于 INSERT, UPDATE 和 DELETE */ @Test public void testQueryRunnerUpdate() { //1. 创建 QueryRunner 的实现类 QueryRunner queryRunner = new QueryRunner(); String sql = "DELETE FROM customers " + "WHERE id IN (?,?)"; Connection connection = null; try { connection = JDBCTools.getConnection(); //2. 使用其 update 方法 queryRunner.update(connection, sql, 12, 13); } catch (Exception e) { e.printStackTrace(); } finally{ JDBCTools.releaseDB(null, null, connection); } }
</div>
BeanHandler:把结果集的第一条记录转为创建BeanHandler对象时传入的Class参数对应的对象.
public void testBeanHandler() { QueryRunner queryRunner = new QueryRunner(); Connection conn = null; try { conn = JDBCTools.getConnection(); String sql = "SELECT id, name customerName, email, birth " + "FROM customers WHERE id>=?"; Customer customer = queryRunner.query(conn, sql, new BeanHandler( Customer.class), 5); System.out.println(customer); } catch (Exception e) { e.printStackTrace(); } finally { JDBCTools.releaseDB(null, null, conn); } }
BeanListHandler:把结果集转为一个List,该List不为null,但可能为空集合(即size()方法返回0)
public void testBeanListHandler(){ String sql = "SELECT id, name customerName, email, birth " + "FROM customers"; QueryRunner queryRunner = new QueryRunner(); Connection conn = null; try { conn = JDBCTools.getConnection(); Object object = queryRunner.query(conn, sql, new BeanListHandler<>(Customer.class)); System.out.println(object); } catch (Exception e) { e.printStackTrace(); } finally{ JDBCTools.releaseDB(null, null, conn); } }
MapHandler:返回SQL语句对应的第一条记录对应的Map对象.键:SQL查询的列名(不是列的别名),值:列的值.
public void testMapHandler(){ Connection connection = null; QueryRunner queryRunner = new QueryRunner(); String sql = "SELECT id, name customerName, email, birth " + "FROM customers WHERE id = ?"; try { connection = JDBCTools.getConnection(); Map<String, Object> map = queryRunner.query(connection, sql, new MapHandler(), 4); System.out.println(map); } catch (Exception e) { e.printStackTrace(); } finally{ JDBCTools.releaseDB(null, null, connection); } }
MapLiatHandler:将结果转化为一个Map的List. MapLiatHandler:返回的多条记录对应的Map的集合.
public void testMapListHandler(){ Connection connection = null; QueryRunner queryRunner = new QueryRunner(); String sql = "SELECT id, name, email, birth " + "FROM customers"; try { connection = JDBCTools.getConnection(); List<Map<String, Object>> mapList = queryRunner.query(connection, sql, new MapListHandler()); System.out.println(mapList); } catch (Exception e) { e.printStackTrace(); } finally{ JDBCTools.releaseDB(null, null, connection); } }
ScalarHandler:把结果集转为一个数值(可以是任意类型)返回
</div>
</div>
public void testScalarHandler(){ Connection connection = null; QueryRunner queryRunner = new QueryRunner(); String sql = "SELECT name FROM customers " + "WHERE id = ?"; try { connection = JDBCTools.getConnection(); Object count = queryRunner.query(connection, sql, new ScalarHandler(), 6); System.out.println(count); } catch (Exception e) { e.printStackTrace(); } finally{ JDBCTools.releaseDB(null, null, connection); } }
</div> </div>
本文由用户
jopen 自行上传分享,仅供网友学习交流。所有权归原作者,若您的权利被侵害,请联系管理员。
转载本站原创文章,请注明出处,并保留原始链接、图片水印。
本站是一个以用户分享为主的开源技术平台,欢迎各类分享!