/**
* 設置SQL語句中?為任意對象的對應值 <br>
*
* @param index
* int 預製值所在位置
* @param value
* Object 預製值
* @throws SQLException
* SQL異常
*/
public void setObject(int index, Object value) throws SQLException {
preparedStatement.setObject(index, value);
}
/**
* 設置SQL語句中?為Timestamp的變數的對應值 <br>
*
* @param index
* int 預製值所在位置
* @param value
* Timestamp 預製值
* @throws SQLException
* SQL異常
*/
public void setTimestamp(int index, java.sql.Timestamp value) throws SQLException {
preparedStatement.setTimestamp(index, value);
}
/**
* 創建Statement對象
*/
public Statement createStatement() throws SQLException {
statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_UPDATABLE);
return statement;
}
/**
* 得到Statement物件
*
* @return Statement對象
*/
public Statement getStatement() {
return statement;
}
/**
* 創建prepareStatement對象
*
* @param sql
* 預設SQL語句
*/
public void prepareStatement(String sql) throws SQLException {
preparedStatement = connection.prepareStatement(sql);
}
/**
* 返回PreparedStatement對象
*
* @return PreparedStatement對象
*/
public PreparedStatement getPreparedStatement() {
return preparedStatement;
}
/**
* 去掉PREPAREDSTATEMENT中的所有已經綁定的參數 <br>
*
* @throws SQLException
* SQL異常
*/
public void clearParameters() throws SQLException {
preparedStatement.clearParameters();
}
/**
* 資料事務提交 要先將自動提交設置狀態設置為不自動﹐然后才commit
*/
public void commit() throws SQLException {
try {
connection.commit();
} catch (SQLException e) {
LOG.error(e.toString(), e);
throw e;
}finally{
close();
}
}
/**
* 資料事務回滾,在執行SQL的過程中出現異常時執行 要先將自動提交設置狀態設置為不自動
*/
public void rollback() throws SQLException {
try {
connection.rollback();
} catch (SQLException e) {
LOG.error(e.toString(), e);
throw e;
}finally{
close();
}
}
/**
* 將查詢的結果放在一個ResultSet中 使用方法:<br>
* DBDAO dao = new DBDAO(); Result rs = dao.doSQLQueryRS(sql); 解析rs后要顯示關閉
* rs.close(); dao.close();
*
* @param sql
* String 任何一條SQL查詢語句形如:select * from tlb_doc where id=?
* @return ResultSet 查詢結果返回對象集
* @throws SQLException
*/
public ResultSet executeQuery(String sql) throws SQLException {
resultset = null;
try {
statement = connection.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
resultset = statement.executeQuery(sql);
connection.commit();//for DBLink與seq
} catch (SQLException e) {
LOG.error(e.toString(), e);
LOG.error("SQL:" + sql);
throw e;
}
return resultset;
}
/**
* 將查詢的結果放在一個ResultSet中 使用方法:<br>
* DBDAO dao = new DBDAO(); rs = dao.executeQuery(sql,obj); 解析rs后要顯示關閉
* rs.close(); dao.close();
*
* @param sql
* String 任何一條SQL查詢語句形如:select * from tlb_doc where id=?
* @param args
* Object[] 參數對象, 形如︰<p/> Object obj []= new Object[1]; </br>
* obj[0] = new String("2"); </br>
* @return ResultSet 查詢結果返回對象集
* @throws SQLException
*/
public ResultSet executeQuery(String sql, Object[] args) throws SQLException {
resultset = null;
try {
preparedStatement = connection.prepareStatement(sql, ResultSet.TYPE_SCROLL_INSENSITIVE,
ResultSet.CONCUR_READ_ONLY);
if (args != null) {
for (int k = 0; k < args.length; k++) {
preparedStatement.setObject(k + 1, args[k]);
}
}
resultset = preparedStatement.executeQuery();
connection.commit();//for DBLink與seq
} catch (SQLException e) {
LOG.error(e.toString(), e);
LOG.error("SQL:" + sql);
for (int i = 0; i < args.length; i++) {
LOG.error("args[" + i + "]" + args[i]);
}
throw e;
}
return resultset;
}
/**
* 執行SQL更新語句,針對PreparedStatement﹐可以用于事物處理
*
* @return int 執行成功的SQL條數
* @throws SQLException
* SQL異常
*/
public int executeUpdate() throws SQLException {
int num = 0;
System.out.println("aaaaaaa");
try {
if (preparedStatement != null) {
System.out.println("ccccccccc");
num = preparedStatement.executeUpdate();
System.out.println("bbb="+preparedStatement.getFetchSize()+",num="+num);
if (isAutoCommit) {
connection.commit();
}
}
} catch (SQLException e) {
if (isAutoCommit) {
connection.rollback();
}
LOG.error(e.toString(), e);
throw e;
} finally {
if (preparedStatement != null) {
preparedStatement.close();
preparedStatement = null;
}
}
return num;
}
/**
dbdao3.txt
*/ }