public class StatementUtil extends Object
Constructor and Description |
---|
StatementUtil() |
Modifier and Type | Method and Description |
---|---|
static <T> T |
executeQuery(PreparedStatement ps,
RsHandler<T> rsh)
执行查询
|
static int |
executeUpdate(PreparedStatement ps,
Object... params)
用于执行 INSERT、UPDATE 或 DELETE 语句以及 SQL DDL(数据定义语言)语句,例如 CREATE TABLE 和 DROP TABLE。
|
static PreparedStatement |
fillArrayParam(PreparedStatement ps,
Object... params)
填充SQL的参数。
|
static PreparedStatement |
fillParams(PreparedStatement ps,
Iterable<?> params)
填充SQL的参数。
|
static PreparedStatement |
fillParams(PreparedStatement ps,
Iterable<?> params,
Map<Integer,Integer> nullTypeCache)
填充SQL的参数。
|
static Long |
getGeneratedKeyOfLong(Statement ps)
获得自增键的值
此方法对于Oracle无效(返回null) |
static List<Object> |
getGeneratedKeys(Statement ps)
获得所有主键
|
static <T> T |
getGeneratedKeys(Statement statement,
RsHandler<T> rsHandler)
获取主键,并使用
RsHandler 处理后返回 |
static int |
getTypeOfNull(PreparedStatement ps,
int paramIndex)
获取null字段对应位置的数据类型
有些数据库对于null字段必须指定类型,否则会插入报错,此方法用于获取其类型,如果获取失败,使用默认的 Types.VARCHAR |
static CallableStatement |
prepareCall(DbConfig config,
Connection conn,
String sql,
Object... params)
|
static PreparedStatement |
prepareStatement(boolean returnGeneratedKey,
DbConfig config,
Connection conn,
String sql,
Object... params)
|
static PreparedStatement |
prepareStatementForBatch(DbConfig config,
Connection conn,
String sql,
Iterable<Object[]> paramsBatch)
创建批量操作的
PreparedStatement |
static PreparedStatement |
prepareStatementForBatch(DbConfig config,
Connection conn,
String sql,
Object[]... paramsBatch)
创建批量操作的
PreparedStatement |
static void |
setParam(PreparedStatement ps,
int paramIndex,
Object param)
为
PreparedStatement 设置单个参数 |
public static PreparedStatement prepareStatement(boolean returnGeneratedKey, DbConfig config, Connection conn, String sql, Object... params)
returnGeneratedKey
- 当为insert语句时,是否返回主键config
- 数据库配置conn
- 数据库连接sql
- SQL语句,使用"?"做为占位符params
- "?"对应参数列表或者Map表示命名参数PreparedStatement
public static PreparedStatement prepareStatementForBatch(DbConfig config, Connection conn, String sql, Object[]... paramsBatch)
PreparedStatement
config
- 数据库配置conn
- 数据库连接sql
- SQL语句,使用"?"做为占位符paramsBatch
- "?"对应参数批次列表PreparedStatement
public static PreparedStatement prepareStatementForBatch(DbConfig config, Connection conn, String sql, Iterable<Object[]> paramsBatch)
PreparedStatement
config
- 数据库配置conn
- 数据库连接sql
- SQL语句,使用"?"做为占位符paramsBatch
- "?"对应参数批次列表PreparedStatement
public static CallableStatement prepareCall(DbConfig config, Connection conn, String sql, Object... params) throws SQLException
config
- 数据库配置conn
- 数据库连接sql
- SQL语句,使用"?"做为占位符params
- "?"对应参数列表CallableStatement
SQLException
- SQL异常public static Long getGeneratedKeyOfLong(Statement ps) throws SQLException
ps
- PreparedStatementSQLException
- SQL执行异常public static List<Object> getGeneratedKeys(Statement ps) throws SQLException
ps
- PreparedStatementSQLException
- SQL执行异常public static <T> T getGeneratedKeys(Statement statement, RsHandler<T> rsHandler) throws SQLException
RsHandler
处理后返回T
- 自定义主键类型statement
- Statement
rsHandler
- 主键结果集处理器SQLException
- SQL执行异常public static int getTypeOfNull(PreparedStatement ps, int paramIndex)
Types.VARCHAR
ps
- Statement
paramIndex
- 参数位置,第一位从1开始Types.VARCHAR
public static PreparedStatement fillArrayParam(PreparedStatement ps, Object... params) throws SQLException
ps
- PreparedStatementparams
- SQL参数PreparedStatement
SQLException
- SQL执行异常public static PreparedStatement fillParams(PreparedStatement ps, Iterable<?> params) throws SQLException
ps
- PreparedStatementparams
- SQL参数PreparedStatement
SQLException
- SQL执行异常public static PreparedStatement fillParams(PreparedStatement ps, Iterable<?> params, Map<Integer,Integer> nullTypeCache) throws SQLException
ps
- PreparedStatementparams
- SQL参数nullTypeCache
- null参数的类型缓存,避免循环中重复获取类型PreparedStatement
SQLException
- SQL执行异常public static void setParam(PreparedStatement ps, int paramIndex, Object param) throws SQLException
PreparedStatement
设置单个参数ps
- PreparedStatement
paramIndex
- 参数位置,从1开始param
- 参数SQLException
- SQL异常public static <T> T executeQuery(PreparedStatement ps, RsHandler<T> rsh) throws DbException
T
- 结果类型ps
- PreparedStatement
rsh
- 结果集处理对象DbException
- SQL执行异常public static int executeUpdate(PreparedStatement ps, Object... params) throws DbException
ps
- PreparedStatement对象params
- 参数DbException
- SQL执行异常Copyright © 2025. All rights reserved.