使用Java代码

时间:2016-04-17 11:51:00

标签: java sql jdbc plsql

我想使用Java代码执行PL / SQL过程。到目前为止我试过这个:

Statement myStmt = null;

myStmt = conn.createStatement();
myStmt.executeQuery("EXECUTE ProjetIRSTEA.detectionX");

我收到以下错误消息:

java.sql.SQLSyntaxErrorException: ORA-00900: invalid SQL statement

3 个答案:

答案 0 :(得分:2)

您必须使用CallableStatement类来执行存储过程。

请检查Oracle发布的此示例以了解如何使用此类: https://docs.oracle.com/cd/A84870_01/doc/java.816/a81354/samapp2.htm

答案 1 :(得分:1)

尝试:

myStmt.executeUpdate("BEGIN ProjetIRSTEA.detectionX; END");

您还可以使用一种方法,使用CallableStatement接口调用JDBC标准定义的存储过程:

CallableStatement myCall = connection.prepareCall("{call ProjetIRSTEA.detectionX()}")
myCall.executeUpdate();

答案 2 :(得分:1)

在Oracle上,您可以使用CallableStatement(如上所述)或仅使用Statement或PreparedStatement发出正常的sql查询(但前者是首选方法)。

String sql = " select ProjetIRSTEA.detectionX() from dual";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.execute();

请注意,您必须在select语句中引用双重系统表。