jfinal如何调用存储过程?
存储过程用一下 Db.execute(ICallback) 这个方法,在其中用一下:
connection.prepareCall(sql).execute();
就可以调用存储过程了,并且还可以自由控制返回值
例子:
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.SQLException;
import com.jfinal.core.ActionKey;
import com.jfinal.core.Controller;
import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.ICallback;
import oracle.jdbc.OracleTypes;
public class AccessToken extends Controller {
@ActionKey("token/get")
public void applyToken() {
Object o = Db.execute(new ICallback() {
@Override
public Object call(Connection conn) throws SQLException {
CallableStatement proc = conn.prepareCall("{?=call eops_xxx.bbb(?,?)}");
proc.registerOutParameter(1, OracleTypes.INTEGER);
proc.setString(2, "证书");
proc.registerOutParameter(3, OracleTypes.VARCHAR);
proc.execute();
return proc.getObject(3);
}
});
renderJson(o.toString()));
}
}
\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\
public Boolean setVarValue(final String processInstanceId, final String varName, final String varValue) {
Boolean flag = false;
Object o = Db.execute(new ICallback() {
@Override
public Boolean call(Connection conn) throws SQLException {
Boolean flag = false;
try {
CallableStatement proc = conn.prepareCall("{CALL PROCESS_WARN.set_var_value(?,?,?)}");
proc.setString(1, processInstanceId);
proc.setString(2, varName);;
proc.setString(3, varValue);
proc.execute();
//代码来到这里就说明你的存储过程已经调用成功,如果有输出参数,接下来就是取输出参数的一个过程
flag = true;
} catch (Exception e) {
flag = false;
log.error("execute procedure PROCESS_WARN.set_var_value mistakes !!!");
e.printStackTrace();
}
return flag;
}
});
flag = (Boolean) o;
return flag;
}
、、、、、、、、、、、、、、、、、、、、、、、、、、、、
有返回值的
public String activeTaskForUpdate(final String processInstanceId,
final String operationClassKey, final String operationKey, final String taskOrgId,
final String orgFilterId, final String otherTag,final String newType) {
String task_id_list = "";
Object o = Db.execute(new ICallback() {
@Override
public String call(Connection conn) throws SQLException {
String task_id_list = "";
try {
CallableStatement proc = conn.prepareCall("{CALL PROCESS_WARN.active_task_for_update(?,?,?,?,?,?,?,?)}");
proc.setString(1, processInstanceId);
proc.setString(2,operationClassKey);
proc.setString(3, operationKey);
proc.setString(4, taskOrgId);
proc.setString(5, orgFilterId);
proc.setString(6, otherTag);
proc.setString(7, newType);
proc.registerOutParameter(8, oracle.jdbc.OracleTypes.VARCHAR);
proc.execute();
//代码来到这里就说明你的存储过程已经调用成功,如果有输出参数,接下来就是取输出参数的一个过程
task_id_list = proc.getObject(8).toString();
} catch (Exception e) {
log.error("execute procedure PROCESS_WARN.active_task_for_updatemistakes !!!");
e.printStackTrace();
if(e.getMessage().indexOf("ORA-20012") != -1){
String msg = e.getMessage().split("#")[1];
throw new NoAssigneeException(msg);
}else{
throw new SystemUnKnownException(e.getMessage());
}
}
return task_id_list;
}
});
task_id_list = (String) o;
return task_id_list;
}