简体   繁体   English

如何在JDBC中执行非查询存储过程

[英]How can I execute non-query Stored Procedure in JDBC

I have a stored procedure in a postgres database. 我在postgres数据库中有一个存储过程。 I'm using the postgres JDBC driver to execute a stored procedure, and I do not care about the return type, and can't execute the query. 我正在使用postgres JDBC驱动程序来执行存储过程,并且我不在乎返回类型,并且无法执行查询。 It's indicating that there's a syntax error near the name of the function. 这表明函数名称附近存在语法错误。

In procedures that return rows, I've been able to do this via a PreparedStatement and setting the parameters, like: 在返回行的过程中,我已经可以通过PreparedStatement并设置参数来做到这一点,例如:

PreparedStatement prepared = connection.prepareStatement("SELECT * FROM NonQueryProcedure(?)");
prepared.setInt(1, 999);
// ....
ResulSet resultSet = prepared.executeQuery();

However, I can't seem to get this to work for an "update" stored procedure where I don't care about the return type. 但是,对于似乎不关心返回类型的“更新”存储过程,我似乎无法使用它。 I've tried using connection.prepareStatement() and prepareCall(), and also tried executing it with statement.execute(), .executeUpdate(), and .executeQuery(), without success. 我尝试使用connection.prepareStatement()和prepareCall(),还尝试使用statement.execute()、. executeUpdate()和.executeQuery()来执行它,但没有成功。

How can I execute a stored procedure where I don't care about the return type? 在不关心返回类型的情况下,如何执行存储过程?

Without the actual syntax error, I can't say for sure, but try this: 没有实际的语法错误,我不能肯定地说,但是尝试一下:

"SELECT * FROM \"getData\"(?)"

CamelCase/PascalCase is a BAD idea in any SQL database. CamelCase / PascalCase是任何SQL数据库中的一个坏主意。 Either it folds it to a single case and all you see is AMASSOFUNREADABLELETTERS or it requires quoting and you will have to forevermore type "aMassofLettersAndQuotesAndShiftKeysAndMyFingersHurt" anytime you want to avoid a syntax error. 要么将其折叠为单个大小写,然后您看到的只是AMASSOFUNREADABLELETTERS或需要引用,并且您想避免语法错误的任何时候,都将不得不永远输入“ aMassofLettersAndQuotesAndShiftKeysAndMyFingersHurt”。

As PostgreSQL has no "real" procedures, functions are simply executed using a SELECT statement: 由于PostgreSQL没有“真正的”过程,因此只需使用SELECT语句即可执行函数:

statement.execute("select NonQueryProcedure(?)");

Note that inside a PL/pgSQL function, you can use the perform statement to call such a function. 请注意,在PL / pgSQL函数内部,可以使用perform语句调用此类函数。 But this is not available outside of a PL/pgSQL block. 但这在PL / pgSQL块之外不可用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM