|
/**
* 广州城市信息研究所 Oracle Script的实现 用于弥补 OraclePreparedStatement的不足 主要用于修改
* setStringAtName等错误情况 ver 1.0
*/
package com.chinadci.datamodule;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import oracle.jdbc.OraclePreparedStatement;
import oracle.sql.BLOB;
import oracle.sql.CLOB;
import oracle.sql.DATE;
public class OracleScript
{
private OraclePreparedStatement oraStmt = null;
private HashMap paramsMap = new HashMap();
/**
* OracleScript 构造函数用于创建一个 OracleScript
*
* @param con
* 数据库连接
* @param SQL
* SQL语句
* @throws SQLException
*/
public OracleScript(Connection con, String SQL) throws SQLException
{
super();
oraStmt = (OraclePreparedStatement) con.prepareStatement(SQLUtils.TransNamedParam(SQL));
SQLUtils.ParserSQLParam(SQL, paramsMap);
}
/**
* Execute 执行Script命令
*
* @return 成功返回true 否则返回 false
* @throws SQLException
*/
public boolean Execute() throws SQLException
{
return oraStmt.execute();
}
/**
* SetStringAtName 实现
* @param ParamName
* @param Value
* @throws SQLException
*/
public void setStringAtName(String paramName, String value) throws SQLException
{
ArrayList paramList = (ArrayList) paramsMap.get(paramName);
if (paramList != null)
{
for(int i=0;i<paramList.size();i++)
{
int ID = (new Integer((String)paramList.get(i))).intValue();
oraStmt.setString(ID,value);
}
}
}
/**
* setIntAtName 实现
* @param ParamName
* @param Value
* @throws SQLException
*/
public void setIntAtName(String paramName, int value) throws SQLException
{
ArrayList paramList = (ArrayList) paramsMap.get(paramName);
if (paramList != null)
{
for(int i=0;i<paramList.size();i++)
{
int ID = (new Integer((String)paramList.get(i))).intValue();
oraStmt.setInt(ID,value);
}
}
}
/**
* setDateAtName 实现
* @param ParamName
* @param Value
* @throws SQLException
*/
public void setDateAtName(String paramName,DATE value) throws SQLException
{
ArrayList paramList = (ArrayList) paramsMap.get(paramName);
if (paramList != null)
{
for(int i=0;i<paramList.size();i++)
{
int ID = (new Integer((String)paramList.get(i))).intValue();
oraStmt.setDATE(ID,value);
}
}
}
/**
* setBOLBAtName 实现
* @param ParamName
* @param Value
* @throws SQLException
*/
public void setBLOBAtName(String paramName,BLOB value) throws SQLException
{
ArrayList paramList = (ArrayList) paramsMap.get(paramName);
if (paramList != null)
{
for(int i=0;i<paramList.size();i++)
{
int ID = (new Integer((String)paramList.get(i))).intValue();
oraStmt.setBLOB(ID,value);
}
}
}
/**
* setCLOBAtName 实现
* @param ParamName
* @param Value
* @throws SQLException
*/
public void setCLOBAtName(String paramName,CLOB value) throws SQLException
{
ArrayList paramList = (ArrayList) paramsMap.get(paramName);
if (paramList != null)
{
for(int i=0;i<paramList.size();i++)
{
int ID = (new Integer((String)paramList.get(i))).intValue();
oraStmt.setCLOB(ID,value);
}
}
}
/**
* Commit 提交
* @throws SQLException
*/
public void Commit() throws SQLException
{
oraStmt.getConnection().commit();
}
}
//测试代码段
public static void main(String[] args) throws Exception
{
Connection con = OraDB.getConnection();
String SQL = "begin update blobtest set id=:id1 where id<>:id1;end;";
OracleScript oraScript = new OracleScript(con,SQL);
oraScript.setIntAtName("id1",1);
oraScript.Execute();
}
|