索引错误时缺少IN或OUT参数

时间:2017-08-23 00:30:49

标签: java sql oracle jdbc

我使用预处理sql 使用JDBC驱动程序版本9.2.0.8.0(classes12.jar)时没有错误但是,这只有在升级到版本9.2.0.8.0之后才会发生。 错误代码是索引

时缺少IN或OUT参数

以下是我的代码。

BEGIN
   SELECT sysdate
   INTO   :today
   FROM   dual;

   BEGIN
      SELECT sysdate
      INTO   :today2
      FROM   dual;
  EXCEPTION
      WHEN OTHERS THEN
         :today2 := NULL;
   END;

   SELECT sysdate
   INTO   :today4
   FROM   dual;
END;

我的问题是,不能使用EXCEPTION,sql?或者是否有其他可用于A的解决方案?

我的JSP文件

<%@ page contentType="text/html; charset=euc-kr"%>
<%@ page import="java.util.*, java.io.*, java.sql.*"%><%!private Connection conn;
    private PreparedStatement stmt;
    private DatabaseMetaData meta;

    //*********************************************************************************
    // 여기에 DB정보를 적어 주십시오.
    public static final String URL = "jdbc:oracle:thin:@192.168.10.38:1521:ora920";
    public static final String DRIVER_NAME = "oracle.jdbc.driver.OracleDriver";
    public static final String ID = "scott";
    public static final String PW = "tiger";

    //*********************************************************************************

    public void jspInit()
    {
        try
        {
            Class.forName(DRIVER_NAME);
            conn = DriverManager.getConnection(URL, ID, PW);
        }
        catch(ClassNotFoundException ce)
        {
            //getServletContext().log( "Error: Database Driver Class : " + DB_CLASS_NMAE );
            ce.printStackTrace();
        }
        catch(SQLException ioe)
        {
            ioe.printStackTrace();
            //getServletContext().log( "Error: DB Connection Error : " + ioe );
        }
    }

    public void jspDestroy()
    {
        try
        {
            if(conn != null)
                conn.close();
        }
        catch(SQLException ioe)
        {
            ioe.printStackTrace();
            //getServletContext().log( "Error: Server Write  Exception : " + ioe);
        }
    }%>
<%
    conn = DriverManager.getConnection(URL, ID, PW);

    ResultSet empCursor = null;
    try
    {
        //pstmt = rdconn.prepareStatement(query);
        //stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_UPDATABLE);
        meta = conn.getMetaData();

        out.println("JDBC Driver Information<br>");
        out.println("---------------------------------<br>");
        out.println("version; " + meta.getDriverVersion() + "<br>");
        out.println("<br>");
        out.println("Database Information<br>");
        out.println("---------------------------------<br>");
        out.println("database name; " + meta.getDatabaseProductName() + "<br>");
        out.println("version; " + meta.getDatabaseProductVersion() + "<br>");
        out.println("<br>");

    }
    catch(Exception e)
    {
        out.println(toHangle("Error occurred<br>") + toHangle(e.toString()));
    }
    finally
    {
    }

    out.println("---------------------------------<br>");
    out.println("Pretreatment test<br>");
    out.println("---------------------------------<br>");
    // 전처리 테스트
    StringBuilder plsql = new StringBuilder();
    plsql.append("BEGIN\n");
    plsql.append("   SELECT sysdate\n");
    plsql.append("      INTO   :today1\n"); // 첫 번째 Parameter
    plsql.append("      FROM   dual;\n");
    plsql.append("   BEGIN\n");
    plsql.append("      SELECT sysdate\n");
    plsql.append("         INTO   :today2\n"); // 두 번째 Parameter
    plsql.append("         FROM   dual;\n");
    // 에러 발생 원인은 EXCEPTION 안 parameter를 사용하는 경우
    plsql.append("   EXCEPTION\n");
    plsql.append("      WHEN OTHERS THEN\n");
    plsql.append("         :today2 := NULL;\n"); // 두 번째 Parameter
    //
    plsql.append("   END;\n");
    plsql.append("      SELECT sysdate\n");
    plsql.append("         INTO   :today3\n"); // 세 번째 Parameter
    plsql.append("         FROM   dual;\n");
    plsql.append("END;\n");

    out.println("<pre>" + plsql.toString() + "</pre>");

    try
    {
        out.println("CallableStatement cstmt = conn.prepareCall()<br>");
        CallableStatement cstmt = conn.prepareCall(plsql.toString());

        // today1~3 갯수에 맞게 3개의 OutParameter를 등록
        for(int j = 1; j <= 3; j++)
        {
            out.println("cstmt.registerOutParameter(" + j + ", " + Types.CHAR + ")<br>");
            cstmt.registerOutParameter(j, Types.CHAR);
        }


        // 최신 Oracle JDBC Driver에서 에러 발생
        out.println("// 최신 Oracle JDBC Driver에서 에러 발생<br>");
        out.println("cstmt.execute()<br>");
        cstmt.execute();
        out.println("<font color=blue>전처리 실행 성공!!</font>");
    }
    catch(Exception e)
    {
        out.println("<font color=red>전처리 실행 실패!!</font>");
        out.println("<br>---------------------------------<br>");
        out.println("Exection<br>");
        out.println("<pre>");
        e.printStackTrace(new PrintWriter(out));
        out.println("</pre>");
    }
%>
<%!private String toHangle(String oldString) throws UnsupportedEncodingException
    {
        if(oldString != null)
        {
            return new String(oldString.getBytes("KSC5601"), "8859_1");
        }
        else
        {
            return oldString;
        }
    }%>

1 个答案:

答案 0 :(得分:0)

尝试将代码更改为以下内容:

cstmt.registerOutParameter("today1", Types.CHAR);
cstmt.registerOutParameter("today2", Types.CHAR);
cstmt.registerOutParameter("today3", Types.CHAR);