50,639
社区成员
![](https://csdnimg.cn/release/cmsfe/public/img/topic.427195d5.png)
![](https://csdnimg.cn/release/cmsfe/public/img/me.40a70ab0.png)
![](https://csdnimg.cn/release/cmsfe/public/img/task.87b52881.png)
![](https://csdnimg.cn/release/cmsfe/public/img/share-circle.3e0b7822.png)
public boolean procedureExecute(final String procedureName, final List params) throws AppException {
boolean bResult= false;
try {
//获取数据库连接池
conn = getConnection();
try {
cs = conn.prepareCall("{call " + procedureName + "}");
logger.debug("准备存储过程成功:" + procedureName);
} catch (SQLException se) {
throw new AppException(this.getClass().getName() + ":_procedureExecute(final String procedureName, final List params):conn.prepareCall('{call " + procedureName + "()}):准备存储过程失败,SQLException 错误信息:" + se.getMessage());
}
//参数设置
Iterator inputParamsIte = params.iterator();
int index = 1;
try {
while (inputParamsIte.hasNext()) {
cs.setObject(index, inputParamsIte.next());
index++;
}
} catch (SQLException se) {
throw new AppException(this.getClass().getName() + ":_procedureExecute(final String procedureName, final List params):cs.setObject(index, inputParamsIte.next())失败。object:" + inputParamsIte.toString() + "。SQLException错误信息:" + se.getMessage());
}
//执行存储过程
try {
cs.execute();
bResult=true;
logger.debug("执行存储过程成功:" + procedureName);
} catch (SQLException se) {
throw new AppException(this.getClass().getName() + ":_procedureExecute(final String procedureName, final List params):cs.execute():执行存储过程失败,SQLException 错误信息:" + se.getMessage());
}
} finally {
closeConnection();
}
return bResult;
}
这个是没有返回值的存储过程调用,有返回的还不会,请其他高手补充下吧。
public String callSP(){
String pkey = "";
CallableStatement cs;
try {
// 設定 CallableStatement
cs = getConnection().prepareCall("{call sp_name(?,?)}");
//传给 in
cs.setString(1, "1");
cs.registerOutParameter(2, java.sql.Types.VARCHAR);
// 运行 CallableStatement
cs.execute();
//获取OUT
pkey = cs.getString(2);
} catch (SQLException e) {
e.printStackTrace();
pkey=e.getMessage();
}
return pkey;
}