通过java执行pl sql块的问题

时间:2017-10-18 06:20:33

标签: java oracle jdbc plsql

我在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块的机制。 上面只是一个简单的查询。 在执行上面的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块之前需要先前的设置,请告诉我。

3 个答案:

答案 0 :(得分:1)

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

答案 1 :(得分:0)

一些错误:

  1. 该字符串将连接到 URL url = new URL(mp4url); //Open a connection to that URL. HttpsURLConnection con = (HttpsURLConnection) int response =con.getResponseCode(); InputStream is = con.getInputStream(); BufferedInputStream inStream = new BufferedInputStream(is, 1024 * 5); FileOutputStream outStream = new FileOutputStream(new File(Environment.getExternalStorageDirectory() + "/" + "mnt/sdcard/17.mp4")); byte[] buff = new byte[7 * 1024]; int len; while ((len = inStream.read(buff)) != -1) { outStream.write(buff,0,len); } //clean up outStream.flush(); outStream.close(); inStream.close(); ,您需要在它们之间留一个空格。
  2. 您没有从表declarex PL / SQL变量中选择值。
  3. PL / SQL块没有将值放入绑定变量以将其返回给Java(如果直接使用绑定变量,则不需要在PL / SQL中声明单独的变量)。
  4. 您已注释掉该行以注册输出参数,且参数为1索引(非0索引)。
  5. 这样的事情:

    INTO

答案 2 :(得分:0)

在String中提供适当的空格以形成SQL个查询。这是用必要的空间改变后的样子!

String plsql = "declare x number; "+
                "begin "+
                   "select HEADCOUNT  FROM PROJECT where deptid=1; "+
                "end;";

您需要使用正确的空格更正您的代码,否则您想要传递的SQL将通过Java / JDBC代码出错。

您可以将PL/SQL代码更改为以下内容:

CallableStatement cs = con.prepareCall(
    "BEGIN SELECT HEADCOUNT INTO :1 FROM PROJECT WHERE deptid=1; END;"
);

希望这能很好地回答你的问题!