JDBC 调用存储过程返回 null
我在 Oracle 数据库上的存储过程方面遇到了一些问题。
我只想调用一个过程(有 50 个 IN 参数和 2 个 IN OUT 参数)并获取这 2 个 OUT 参数。
我正在尝试执行()关联的CallableStatement,但它返回一个NullPointerException
java.lang.NullPointerException
at oracle.jdbc.driver.T4C8Oall.getNumRows(T4C8Oall.java:977)
at oracle.jdbc.driver.T4CCallableStatement.executeForRows(T4CCallableStatement.java:1363)
...
这是代码(我正在使用Hibernate):
Session session = (Session) HibernateUtil.currentSession();
final Transaction transaction = session.beginTransaction();
try {
session.doWork(new Work() {
public void execute(Connection connection) throws SQLException {
try {
CallableStatement call = connection.prepareCall("{?,? = call geneva_admin.bksgnvcrmps.createorreturnproduct1nc(" +
"?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?," +
"?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?," +
"?,?,?,?,?,?,?,?,?,?,?,?)}");
int i = 1;
call.registerOutParameter(i++, Types.INTEGER);
call.registerOutParameter(i++, Types.VARCHAR);
call.setString(i++, typeofproduct);
call.setString(i++, shiptositenumber);
call.setString(i++, billtositenumber);
call.setString(i++, ordernumber);
call.setInt(i++, orderid);
...
call.setInt(i++, errcode);
call.setString(i++, errmsg);
call.execute();
System.out.println("err_code: " + call.getString(1));
System.out.println("err_msg: " + call.getString(2));
call.close();
transaction.commit();
} catch (SQLException e) {
throw new SQLException(e);
}
}
});
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
return false;
}
如果在execute()之前,我设置了
call.setEscapeProcessing(false);
一个SQLSyntaxErrorException(ORA-00911:无效字符)
你能帮我吗?
Fab
这是解决方案:
该过程的最后 2 个参数是 IN OUT。 我认为我们必须将它们作为 OUT 参数进行管理:
CallableStatement call = connection.prepareCall("{?, ? = call bksgnvcrm.createorreturnproduct1nc(..., ?, ?)}");
但是由于它们是 IN OUT,因此这些参数必须声明为 IN:
CallableStatement call = connection.prepareCall("{call bksgnvcrm.createorreturnproduct1nc(..., ?, ?)}");
最后将它们注册为 OUT,如下所示:
call.registerOutParameter(i, Types.INTEGER);
call.setInt(i++, errcode);
call.registerOutParameter(i, Types.VARCHAR);
call.setString(i++, errmsg);
我希望这可以帮助您:)
I've got some problems with a stored procedure on an Oracle database.
I just want to call a procedure (which has 50 IN parameters and 2 IN OUT parameters) and get these 2 OUT parameters.
I'm trying to execute() the associated CallableStatement but it returns me a NullPointerException
java.lang.NullPointerException
at oracle.jdbc.driver.T4C8Oall.getNumRows(T4C8Oall.java:977)
at oracle.jdbc.driver.T4CCallableStatement.executeForRows(T4CCallableStatement.java:1363)
...
Here is the code (I'm using Hibernate):
Session session = (Session) HibernateUtil.currentSession();
final Transaction transaction = session.beginTransaction();
try {
session.doWork(new Work() {
public void execute(Connection connection) throws SQLException {
try {
CallableStatement call = connection.prepareCall("{?,? = call geneva_admin.bksgnvcrmps.createorreturnproduct1nc(" +
"?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?," +
"?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?," +
"?,?,?,?,?,?,?,?,?,?,?,?)}");
int i = 1;
call.registerOutParameter(i++, Types.INTEGER);
call.registerOutParameter(i++, Types.VARCHAR);
call.setString(i++, typeofproduct);
call.setString(i++, shiptositenumber);
call.setString(i++, billtositenumber);
call.setString(i++, ordernumber);
call.setInt(i++, orderid);
...
call.setInt(i++, errcode);
call.setString(i++, errmsg);
call.execute();
System.out.println("err_code: " + call.getString(1));
System.out.println("err_msg: " + call.getString(2));
call.close();
transaction.commit();
} catch (SQLException e) {
throw new SQLException(e);
}
}
});
} catch (Exception e) {
e.printStackTrace();
transaction.rollback();
return false;
}
If, before the execute(), I set
call.setEscapeProcessing(false);
I've got a SQLSyntaxErrorException (ORA-00911: invalid character)
Could you help me ?
Fab
Here is the solution:
The 2 last parameters of the procedure are IN OUT.
I thought that we had to manage them as OUT parameter :
CallableStatement call = connection.prepareCall("{?, ? = call bksgnvcrm.createorreturnproduct1nc(..., ?, ?)}");
But as they're IN OUT, these parameters have to be declared as IN :
CallableStatement call = connection.prepareCall("{call bksgnvcrm.createorreturnproduct1nc(..., ?, ?)}");
And finally register them as OUT like this :
call.registerOutParameter(i, Types.INTEGER);
call.setInt(i++, errcode);
call.registerOutParameter(i, Types.VARCHAR);
call.setString(i++, errmsg);
I hope this may help you :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我也有同样的问题。就我而言,我意识到在调用存储过程时我忘记了“调用”一词。
有一天这可能会帮助别人,谁知道呢......
I also had the same issue. In my case I realized that I forgot the "call" word when calling the stored procedure.
This may help someone else one day, who knows...