标签:apache-dbutils queryrunner resultsethandler dbutils sql
<span style="font-size:14px;"> /** * 测试 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); } }</span>
<span style="font-family:SimSun;font-size:14px;"><span style="font-size:14px;font-weight: normal;">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); } }</span></span>
<span style="font-family:SimSun;font-size:14px;">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); } }</span>
<span style="font-family:SimSun;font-size:14px;"><span style="font-size:14px;font-weight: normal;">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); } }</span></span>
<span style="font-family:SimSun;font-size:14px;font-weight: normal;">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); } }</span>
<span style="font-family:SimSun;">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); } }</span>
Apache-DBUtils包对数据库的操作,布布扣,bubuko.com
标签:apache-dbutils queryrunner resultsethandler dbutils sql
原文地址:http://blog.csdn.net/pengkv/article/details/30774187