从 Java 调用 Oracle 存储过程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3674841/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-18 21:29:29  来源:igfitidea点击:

Calling Oracle stored procedure from Java

javaoracle

提问by javaguy

I'm calling an Oracle stored function using JDBCthin driver.
Following is the code.

我正在使用JDBC瘦驱动程序调用 Oracle 存储函数。
以下是代码。

class testSP 
{ 
  public static void main (String args []) 
                     throws SQLException, ClassNotFoundException 
  { 
      String driver_class = "oracle.jdbc.driver.OracleDriver"; 
      String connect_string = "jdbc:oracle:thin:@xxx.xx.xx.xx:1521:xxxx"; 

      Connection conn; 

      Class.forName(driver_class); 
      conn = DriverManager.getConnection(connect_string, "xxxx", "xxxx"); 

      // OracleCallableStatement ocs = 
      //    (OracleCallableStatement)conn.prepareCall(
      //        "{? = call acpks_stmt_gen.fn_stmt_gen(?,?,?,?,?,?)}");
      CallableStatement ocs = 
         conn.prepareCall(
            "{? = call acpks_stmt_gen.fn_stmt_gen(?,?,?,?,?,?)}");

      ocs.registerOutParameter(1, java.sql.Types.ARRAY);

      ocs.setString(2, "144000014");
      ocs.setString(3, "RET");
      ocs.setString(4, "N");
      ocs.setString(5, "3");
      ocs.setNull(6, java.sql.Types.DATE) ;
      ocs.setNull(7, java.sql.Types.DATE);



      ocs.executeUpdate(); 

     // java.sql.ResultSet rs2 = (java.sql.ResultSet) ocs.getResultSet();

  }

}

When I call this I get an exception as following

当我调用它时,我得到如下异常

Exception in thread "main" java.sql.SQLException: ORA-03115: unsupported network datatype or representation

I'm using the thin driver ojdbc6.jarprovided by Oracle.
and I'm using this in my class path.

我正在使用ojdbc6.jarOracle 提供的瘦驱动程序。
我在我的课程路径中使用它。

Thanks.

谢谢。

回答by Erich Kitzmueller

Try

尝试

  ocs.setString(6, "") ;
  ocs.setString(7, "");

instead of

代替

  ocs.setNull(6, java.sql.Types.DATE);
  ocs.setNull(7, java.sql.Types.DATE);

making use of the fact that in Oracle, the empty string is the same as NULL.

利用在 Oracle 中,空字符串与 NULL 相同的事实。

In my current project, I've encountered similar problems with NULLs that could be solved that way. (Yes, that's fugly)

在我当前的项目中,我遇到了可以通过这种方式解决的 NULL 的类似问题。(是的,这很糟糕)