gpt4 book ai didi

java - 通过java执行pl sql block 的问题

转载 作者:行者123 更新时间:2023-12-02 12:07:21 25 4
gpt4 key购买 nike

我用java编写了以下代码。

package demo;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Types;

public class Sample {

private Connection c = null;

public Sample()
{
try {
c = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:XE", "system", "jboss");
String plsql = ""+
"declare"+
"x number;"+
"begin "+
"select HEADCOUNT FROM PROJECT where deptid=1;"+
"end;";
CallableStatement cs = c.prepareCall(plsql);
//cs.registerOutParameter(0, Types.INTEGER);

cs.execute();
System.out.println("Result is "+cs.getInt(1));
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

public static void main(String[] args)
{
Sample z = new Sample();
}
}

我只是想探索通过 java 执行 plsql block 的机制。上面只是简单的查询。在执行上面的java代码时,我得到错误为

java.sql.SQLException: Invalid SQL type: sqlKind = UNINITIALIZED
at oracle.jdbc.driver.T4CCallableStatement.doOall8(T4CCallableStatement.java:75)
at oracle.jdbc.driver.T4CCallableStatement.executeForRows(T4CCallableStatement.java:1043)
at oracle.jdbc.driver.OracleStatement.doExecuteWithTimeout(OracleStatement.java:1336)
at oracle.jdbc.driver.OraclePreparedStatement.executeInternal(OraclePreparedStatement.java:3613)
at oracle.jdbc.driver.OraclePreparedStatement.execute(OraclePreparedStatement.java:3714)
at oracle.jdbc.driver.OracleCallableStatement.execute(OracleCallableStatement.java:4755)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.execute(OraclePreparedStatementWrapper.java:1378)
at demo.Sample.<init>(Sample.java:26)
at demo.Sample.main(Sample.java:36)

让我知道在执行此类 pl/sql block 之前是否需要任何预先设置。

最佳答案

1)第一个问题是其他答案中提到的declarex...
2)第二个问题是select HEADCOUNT FROM PROJECT where deptid=1;在plsql中此查询无效。应该是 select HEADCOUNT into x ...

关于java - 通过java执行pl sql block 的问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46803924/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com