public class DBAgent extends Object
Title: 常用代码打包
Description:
这个类作为进行数据库操作的代理,它能进行的数据库操作有select、update、insert和delete。
具体操作步骤如下:
1.实例化一个DBAgent
DBAgent dba = new DBAgent();
2.执行sql脚本
int status = dba.execute("select * from ...");
其中status返回值为-1表示执行失败,如果为select查询,返回1表示成功.如果为其他类型数据库操作,则返回发生影响的结果数.
以上所有的数据库操作任何时候都不会抛出异常,因此在调用时不必进行异常捕捉。是否发生错误可以 通过execute()方法的返回值进行判断。
如果SQL语句为PrepareStatement类型,则需要通过调用setParamVector()方法初始化输入参数。
该类还支持事务处理。在实例化DBAgent时传入是否自动提交参数,true为自动提交,此时不能手工控制事务;为false时需要手工控制 数据库事务,此时可以调用的事务控制方法有rollback(),commit()
Copyright: Copyright (c) 2006
Company: www.justdos.net
| 限定符和类型 | 字段和说明 |
|---|---|
protected List |
batchedSql |
protected String |
configKey |
protected Connection |
conn |
protected List |
params |
protected HashSet |
psSet |
protected ResultSet |
query_result |
protected int |
rows |
protected String |
SQLString |
protected int |
update_result |
| 限定符 | 构造器和说明 |
|---|---|
protected |
DBAgent() |
|
DBAgent(String configKey)
默认构造函数,启用自动提交。
|
|
DBAgent(String configKey,
boolean isAutoCommit)
构造函数,接收参数为是否进行自动提交。
|
|
DBAgent(String configKey,
Connection conn)
构造函数,启用自动提交。
|
|
DBAgent(String configKey,
Connection conn,
boolean isAutoCommit)
构造函数,接收参数为是否进行自动提交。
|
| 限定符和类型 | 方法和说明 |
|---|---|
void |
addBatch(String sql)
添加Batch
|
void |
clearBatch()
清除Batch
|
void |
close()
释放数据库连接
|
void |
commit()
事务提交。
|
int |
execute()
执行sql脚本
|
int |
execute(String sqlString)
执行sql脚本
|
int |
executeBatch()
执行Batch
|
Connection |
getConn()
取得DBBean内的连接,可能是null
|
List |
getParams() |
ResultSet |
getQueryResult()
获得查询结果集
|
int |
getRows()
得到结果集的记录数
|
String |
getSQLString() |
int |
getUpdateResult()
获得更改的记录数
|
void |
rollback()
事务回滚。
|
void |
setParams(List params) |
void |
setSQLString(String SQLString) |
protected Connection conn
protected ResultSet query_result
protected int update_result
protected String SQLString
protected List params
protected int rows
protected HashSet psSet
protected List batchedSql
protected String configKey
protected DBAgent()
public DBAgent(String configKey) throws SQLException, com.ds.common.JDSException
SQLExceptioncom.ds.common.JDSExceptionpublic DBAgent(String configKey, Connection conn) throws SQLException, com.ds.common.JDSException
conn - - 数据库连接SQLExceptioncom.ds.common.JDSExceptionpublic DBAgent(String configKey, boolean isAutoCommit) throws SQLException, com.ds.common.JDSException
isAutoCommit - - 是否使用自动提交SQLExceptioncom.ds.common.JDSExceptionpublic DBAgent(String configKey, Connection conn, boolean isAutoCommit) throws SQLException, com.ds.common.JDSException
conn - - 数据库连接isAutoCommit - - 是否使用自动提交SQLExceptioncom.ds.common.JDSExceptionpublic int execute(String sqlString)
public int execute()
public String getSQLString()
public List getParams()
public void setSQLString(String SQLString)
SQLString - 设置准备执行的sql语句public void setParams(List params)
paramVector - 封装到Vector中的参数列表。添加参数的顺序需要和sql语句中的'?'出现的顺序一致。public ResultSet getQueryResult()
public int getUpdateResult()
public void close()
public int getRows()
public void commit()
public void rollback()
public void clearBatch()
public void addBatch(String sql)
sql - 要添加到Batch中的sqlpublic int executeBatch()
public Connection getConn()
Copyright © 2023 onecode. All rights reserved.