public final class JDBCAgent extends Object implements Closeable
Title: T1开发框架
Description: 基于JDBC的数据库操作工具类。
Copyright: Copyright (c) 2012
Company: seeyon.com
构造器和说明 |
---|
JDBCAgent()
使用Spring管理的数据库连接构造JDBCAgent
|
JDBCAgent(boolean raw)
传入true时将使用原始的数据库连接构造JDBCAgent,false则使用Spring管理的数据库连接
|
JDBCAgent(Connection conn)
使用传入的数据库连接构造JDBCAgent,close方法将不会关闭该连接
|
JDBCAgent(String dsName)
使用Spring管理的指定数据源的数据库连接构造JDBCAgent
|
限定符和类型 | 方法和说明 |
---|---|
void |
batch1Prepare(String sql)
准备需要批量执行更新的Prepared SQL,使用例子如下:
JDBCAgent dba = new JDBCAgent();
dba.batch1Prepare("insert into xxx value(?
|
void |
batch2Add(List params)
添加要执行Prepared SQL的数据参数列表,使用例子如下:
JDBCAgent dba = new JDBCAgent();
dba.batch1Prepare("insert into xxx value(?
|
int |
batch3Execute()
执行批量的Prepared SQL,使用例子如下:
JDBCAgent dba = new JDBCAgent();
dba.batch1Prepare("insert into xxx value(?
|
static String |
clobToString(Clob clob) |
void |
close()
释放数据库资源
|
int |
count(String namedSql,
Map<String,Object> nameParams)
JDBC count方法
|
int |
execute(String sqlString)
执行SQL脚本
|
int |
execute(String sqlString,
List params)
执行SQL脚本
|
int |
execute(String sqlString,
Object param)
执行SQL脚本
|
int |
executeBatch(List batchedSql)
批量执行SQL
|
void |
executeNamedSql(String namedSql,
Map<String,Object> params)
与execut函数执行sql类似,只是sql参数形式类似Hibernate的named query
|
static Object |
executePreparedStatement(String preparedString,
org.springframework.jdbc.core.PreparedStatementCallback action)
使用默认的数据源执行预处理SQL的工具方法
|
static Object |
executePreparedStatement(String dsName,
String preparedString,
org.springframework.jdbc.core.PreparedStatementCallback action)
执行预处理SQL的工具方法
|
static Object |
executeProcedure(String callString,
org.springframework.jdbc.core.CallableStatementCallback action)
使用默认的数据源执行存储过程的工具方法
|
static Object |
executeProcedure(String dsName,
String callString,
org.springframework.jdbc.core.CallableStatementCallback action)
执行存储过程的工具方法
|
void |
executeSqlFile(String sqlKey)
无需参数执行自定义SQL
|
void |
executeSqlFile(String sqlKey,
List params)
执行自定义SQL,指定sqlKey的SQL脚本,SQL文件需要存放于cfgHome/sql目录下,且以数据库类型名称开头,
或者文件名以ALL开头作为各数据库通用sql配置文件
数据库类型名称如(注意区分大小写):DB2、MySQL、Oracle、PostgreSQL、SQLServer
|
void |
executeSqlFile(String sqlKey,
Map<String,Object> params)
执行自定义SQL,指定sqlKey的SQL脚本,SQL文件需要存放于cfgHome/sql目录下,且以数据库类型名称开头,
或者文件名以ALL开头作为各数据库通用sql配置文件
数据库类型名称如(注意区分大小写):DB2、MySQL、Oracle、PostgreSQL、SQLServer
与executeSqlFile(String sqlKey, List params)类似,只是参数形式类似Hibernate的named query
|
FlipInfo |
findByPaging(String sql,
FlipInfo fpi)
SQL翻页查询方法
|
FlipInfo |
findByPaging(String sql,
List params,
FlipInfo fpi)
SQL翻页查询方法
|
FlipInfo |
findNameByPaging(String namedSql,
Map<String,Object> nameParams,
FlipInfo fpi)
SQL翻页命名查询方法
|
static Connection |
getConnection()
获取默认受Spring事务管理控制的数据库连接(spring中配置的名称为dataSource的数据源连接)
|
static DataSource |
getDataSource()
获取在Spring中配置的默认数据源
|
static DataSource |
getDataSource(String dsName)
获取在Spring中配置的指定数据源
|
static String |
getDBType()
获取当前连接的数据库类型标识,oracle、sqlserver、mysql、postgresql
|
String |
getDBUserName()
数据库相关信息开始 *************************
|
org.hibernate.dialect.Dialect |
getDialect()
获取当前Hibernate中的Dialect实例
|
ResultSet |
getQueryResult()
获得查询结果集
|
static Connection |
getRawConnection()
获取默认原始数据库连接(spring中配置的名称为dataSource的数据源连接)
|
static Connection |
getRawConnection(String dsName)
获取在Spring中配置的指定数据源名称的原始数据库连接
|
static String |
getSqlInFile(String sqlKey)
获取自定义SQL,指定sqlKey的SQL脚本,SQL配置文件需要存放于cfgHome/sql目录下,且以数据库类型名称开头,
或者文件名以ALL开头作为各数据库通用sql配置文件
数据库类型名称如(注意区分大小写):DB2、MySQL、Oracle、PostgreSQL、SQLServer
|
protected void |
init() |
static void |
loadSqlInFile() |
static void |
main(String[] args)
特定数据库SQL文件执行结束 ******************
|
static void |
release()
无论是否原生的连接,强制关闭连接,释放资源。
|
List |
resultSetToList()
把查询结果集数据封装为Map的list,同时释放当前查询结果集的数据库游标。
|
List |
resultSetToList(boolean lowercaseKey)
把查询结果集数据封装为Map的list,同时释放当前查询结果集的数据库游标。
|
Map |
resultSetToMap()
把当前查询结果集合结果数据取出第一条封装到Map,如果无数据则返回空的Map实例(不是null)
|
Map |
resultSetToMap(ResultSet rs)
把指定查询结果集合结果数据取出第一条封装到Map,如果无数据则返回空的Map实例(不是null)
|
void |
setMaxSeconds(int maxSeconds) |
public JDBCAgent(Connection conn)
public JDBCAgent()
public JDBCAgent(boolean raw)
raw
- 是否使用原始的数据库连接构造JDBCAgentpublic JDBCAgent(String dsName)
dsName
- Spring数据源配置名称public void setMaxSeconds(int maxSeconds)
maxSeconds
- 最大超时时间(单位:秒)
设置为0或者负数时表示不做连接自动清理的检查protected void init()
public int execute(String sqlString) throws BusinessException, SQLException
sqlString
- 要执行的SQL脚本BusinessException
SQLException
public int execute(String sqlString, Object param) throws BusinessException, SQLException
sqlString
- 要执行的SQL脚本param
- SQL中的单个参数值。当SQL脚本中只有一个'?'出现时采用。BusinessException
SQLException
public int execute(String sqlString, List params) throws BusinessException, SQLException
sqlString
- 要执行的SQL脚本params
- 封装到List中的参数列表。添加参数的顺序需要和SQL脚本中的'?'出现的顺序一致。BusinessException
SQLException
public ResultSet getQueryResult()
public int executeBatch(List batchedSql) throws BusinessException, SQLException
BusinessException
SQLException
public void batch1Prepare(String sql) throws BusinessException, SQLException
sql
- Prepared SQLBusinessException
SQLException
public void batch2Add(List params) throws SQLException
params
- 添加要执行Prepared SQL的数据参数列表BusinessException
SQLException
public int batch3Execute() throws SQLException
BusinessException
SQLException
public FlipInfo findByPaging(String sql, FlipInfo fpi) throws BusinessException, SQLException
sql
- 要进行翻页查询的SQL脚本fpi
- 翻页信息对象(size为-1的时候 取全部数据)BusinessException
SQLException
public FlipInfo findByPaging(String sql, List params, FlipInfo fpi) throws BusinessException, SQLException
sql
- 要进行翻页查询的SQL脚本params
- 封装到List中的参数列表。添加参数的顺序需要和SQL脚本中的'?'出现的顺序一致。fpi
- 翻页信息对象(size为-1的时候 取全部数据)BusinessException
SQLException
public FlipInfo findNameByPaging(String namedSql, Map<String,Object> nameParams, FlipInfo fpi) throws BusinessException, SQLException
sql
- 要进行翻页查询的SQL脚本params
- 查询参数fpi
- 翻页信息对象(size为-1的时候 取全部数据)BusinessException
SQLException
public int count(String namedSql, Map<String,Object> nameParams) throws BusinessException, SQLException
sql
- SQLException
BusinessException
public List resultSetToList() throws SQLException
SQLException
public List resultSetToList(boolean lowercaseKey) throws SQLException
lowercaseKey
- 是否对列名进行lowercase转换SQLException
public Map resultSetToMap() throws BusinessException, SQLException
BusinessException
- 如果一条数据都没有,将抛出此异常SQLException
public Map resultSetToMap(ResultSet rs) throws BusinessException, SQLException
rs
- 要封装到Map的结果集对象BusinessException
- 如果一条数据都没有,将抛出此异常SQLException
public void close()
close
在接口中 Closeable
close
在接口中 AutoCloseable
public static void release()
public String getDBUserName() throws SQLException
SQLException
public org.hibernate.dialect.Dialect getDialect()
public static Object executeProcedure(String callString, org.springframework.jdbc.core.CallableStatementCallback action)
callString
- 存储过程调用语句action
- 存储过程调用回调函数实现public static Object executeProcedure(String dsName, String callString, org.springframework.jdbc.core.CallableStatementCallback action)
dsName
- 执行存储过程的数据源名称callString
- 存储过程调用语句action
- 存储过程调用回调函数实现public static Object executePreparedStatement(String preparedString, org.springframework.jdbc.core.PreparedStatementCallback action)
callString
- 预处理SQL调用语句action
- 预处理SQL调用回调函数实现public static Object executePreparedStatement(String dsName, String preparedString, org.springframework.jdbc.core.PreparedStatementCallback action)
dsName
- 执行预处理SQL的数据源名称callString
- 预处理SQL调用语句action
- 预处理SQL调用回调函数实现public static Connection getConnection()
public static Connection getRawConnection() throws SQLException
SQLException
public static Connection getRawConnection(String dsName) throws SQLException
SQLException
public static DataSource getDataSource()
public static DataSource getDataSource(String dsName)
public static String getDBType()
public static void loadSqlInFile()
public static String getSqlInFile(String sqlKey) throws BusinessException
sqlKey
- SQL配置keyBusinessException
public void executeSqlFile(String sqlKey, List params) throws BusinessException, IOException, SQLException
sqlKey
- SQL配置keyparams
- 自定义SQL执行所需参数ListBusinessException
IOException
SQLException
public void executeSqlFile(String sqlKey, Map<String,Object> params) throws BusinessException, IOException, SQLException
sqlKey
- SQL配置keyparams
- 自定义SQL执行所需参数Map,类似Hibernate的named queryBusinessException
IOException
SQLException
public void executeNamedSql(String namedSql, Map<String,Object> params) throws BusinessException, IOException, SQLException
namedSql
- 类似Hibernate的named query的sqlparams
- 自定义SQL执行所需参数Map,类似Hibernate的named queryBusinessException
IOException
SQLException
public void executeSqlFile(String sqlKey) throws BusinessException, IOException, SQLException
sqlKey
- SQL配置keyBusinessException
IOException
SQLException
public static String clobToString(Clob clob) throws SQLException
SQLException
public static void main(String[] args)
Copyright © 2016–2017. All rights reserved.