“使用QueryRunner查询表中特殊值的操作”的版本间的差异
跳到导航
跳到搜索
Jihongchang(讨论 | 贡献) (建立内容为“https://www.bilibili.com/video/BV1eJ411c7rf/?p=53”的新页面) |
Jihongchang(讨论 | 贡献) |
||
| 第1行: | 第1行: | ||
https://www.bilibili.com/video/BV1eJ411c7rf/?p=53 | https://www.bilibili.com/video/BV1eJ411c7rf/?p=53 | ||
| + | |||
| + | === ScalarHandler === | ||
| + | |||
| + | ==== 示例1 ==== | ||
| + | <syntaxhighlight lang="java"> | ||
| + | /** | ||
| + | * ScalarHandler:是 ResultSetHandler 接口的实现类,对应查询结果中的单个值 | ||
| + | */ | ||
| + | @Test | ||
| + | public void testQueryObjectLong() { | ||
| + | Connection conn = null; | ||
| + | String sql = "select count(*) from customers"; | ||
| + | |||
| + | /* | ||
| + | AbstractKeyedHandler, AbstractListHandler, ArrayHandler, ArrayListHandler, BaseResultSetHandler, | ||
| + | BeanHandler, BeanListHandler, BeanMapHandler, ColumnListHandler, | ||
| + | KeyedHandler, MapHandler, MapListHandler, ScalarHandler | ||
| + | */ | ||
| + | ScalarHandler rsh = new ScalarHandler(); | ||
| + | Long count = null; | ||
| + | QueryRunner runner = new QueryRunner(); | ||
| + | |||
| + | try { | ||
| + | |||
| + | conn = JDBCUtils.getConnectionFromDruid(); | ||
| + | |||
| + | count = (Long) runner.query(conn, sql, rsh); | ||
| + | |||
| + | } catch (SQLException e) { | ||
| + | e.printStackTrace(); | ||
| + | } finally { | ||
| + | JDBCUtils.closeResource(conn, null); | ||
| + | } | ||
| + | |||
| + | System.out.println(count); | ||
| + | |||
| + | } | ||
| + | </syntaxhighlight><syntaxhighlight lang="console"> | ||
| + | 7 | ||
| + | </syntaxhighlight> | ||
| + | |||
| + | |||
| + | |||
| + | ==== 示例2 ==== | ||
| + | <syntaxhighlight lang="java"> | ||
| + | /** | ||
| + | * ScalarHandler:是 ResultSetHandler 接口的实现类,对应查询结果中的单个值 | ||
| + | */ | ||
| + | @Test | ||
| + | public void testQueryObjectDate() { | ||
| + | Connection conn = null; | ||
| + | String sql = "select max(birth) from customers"; | ||
| + | |||
| + | /* | ||
| + | AbstractKeyedHandler, AbstractListHandler, ArrayHandler, ArrayListHandler, BaseResultSetHandler, | ||
| + | BeanHandler, BeanListHandler, BeanMapHandler, ColumnListHandler, | ||
| + | KeyedHandler, MapHandler, MapListHandler, ScalarHandler | ||
| + | */ | ||
| + | ScalarHandler rsh = new ScalarHandler(); | ||
| + | Date d = null; | ||
| + | QueryRunner runner = new QueryRunner(); | ||
| + | |||
| + | try { | ||
| + | |||
| + | conn = JDBCUtils.getConnectionFromDruid(); | ||
| + | |||
| + | d = (Date) runner.query(conn, sql, rsh); | ||
| + | |||
| + | } catch (SQLException e) { | ||
| + | e.printStackTrace(); | ||
| + | } finally { | ||
| + | JDBCUtils.closeResource(conn, null); | ||
| + | } | ||
| + | |||
| + | System.out.println(d); | ||
| + | |||
| + | } | ||
| + | </syntaxhighlight><syntaxhighlight lang="console"> | ||
| + | 2023-01-09 | ||
| + | </syntaxhighlight> | ||
2023年1月14日 (六) 06:58的版本
https://www.bilibili.com/video/BV1eJ411c7rf/?p=53
ScalarHandler
示例1
/**
* ScalarHandler:是 ResultSetHandler 接口的实现类,对应查询结果中的单个值
*/
@Test
public void testQueryObjectLong() {
Connection conn = null;
String sql = "select count(*) from customers";
/*
AbstractKeyedHandler, AbstractListHandler, ArrayHandler, ArrayListHandler, BaseResultSetHandler,
BeanHandler, BeanListHandler, BeanMapHandler, ColumnListHandler,
KeyedHandler, MapHandler, MapListHandler, ScalarHandler
*/
ScalarHandler rsh = new ScalarHandler();
Long count = null;
QueryRunner runner = new QueryRunner();
try {
conn = JDBCUtils.getConnectionFromDruid();
count = (Long) runner.query(conn, sql, rsh);
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, null);
}
System.out.println(count);
}
7
示例2
/**
* ScalarHandler:是 ResultSetHandler 接口的实现类,对应查询结果中的单个值
*/
@Test
public void testQueryObjectDate() {
Connection conn = null;
String sql = "select max(birth) from customers";
/*
AbstractKeyedHandler, AbstractListHandler, ArrayHandler, ArrayListHandler, BaseResultSetHandler,
BeanHandler, BeanListHandler, BeanMapHandler, ColumnListHandler,
KeyedHandler, MapHandler, MapListHandler, ScalarHandler
*/
ScalarHandler rsh = new ScalarHandler();
Date d = null;
QueryRunner runner = new QueryRunner();
try {
conn = JDBCUtils.getConnectionFromDruid();
d = (Date) runner.query(conn, sql, rsh);
} catch (SQLException e) {
e.printStackTrace();
} finally {
JDBCUtils.closeResource(conn, null);
}
System.out.println(d);
}
2023-01-09