栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Oracle中获取表作为out参数

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Oracle中获取表作为out参数

您必须定义一个sqldata对象以对此进行映射。

文档:http
:
//docs.oracle.com/cd/E11882_01/java.112/e16548/oraarr.htm#JJDBC28574

例如:

SQL> create or replace TYPE tableoneExample AS OBJECT (  2        somethingOne      VARCHAR2 (4)  3       ,somethingTwo        NUMBER (12)  4  );  5  /Type created.SQL> create or replace TYPE outputoneSQLType IS TABLE OF tableOneExample;  2  /Type created.SQL>SQL> create or replace PROCEDURE myprocedure (  2  inputParam     IN       VARCHAR2,  3  outputOne      OUT outputOneSQLType)  4  as  5  begin  6  outputOne  := outputoneSQLType(tableoneExample('a', 1), tableoneExample('b', 2));  7  end;  8  /Procedure created.

现在我们定义SQLDATA接口:

import java.sql.*;public class TestArr implements SQLData {  private String sql_type;  public String attrOne;  public int    attrTwo;  public TestArr()   {   }  public TestArr (String sql_type, String attrOne, int attrTwo)  {    this.sql_type = sql_type;    this.attrOne = attrOne;    this.attrTwo = attrTwo;   }  // define a get method to return the SQL type of the object  public String getSQLTypeName() throws SQLException  {     return sql_type;   }  // define the required readSQL() method   public void readSQL(SQLInput stream, String typeName)    throws SQLException  {    sql_type = typeName;    attrOne = stream.readString();    attrTwo = stream.readInt();  }    // define the required writeSQL() method   public void writeSQL(SQLOutput stream)    throws SQLException  {     stream.writeString(attrOne);    stream.writeInt(attrTwo);  }}

确保流写入/读取的输入和顺序与您的oracle类型相同,因为任何不一致都会导致内部表示错误。

然后在主类中,将其映射如下:

CallableStatement stmt = conn.prepareCall("begin myprocedure(?,?); end;");stmt.setString(1, "foo");stmt.registerOutParameter(2, java.sql.Types.ARRAY, "OUTPUTONESQLTYPE"); // YOUR ARRAY TYPE (TO MATCH THE API OUTPUT), NOT OBJECTstmt.execute();Array arr = stmt.getArray (2);Map map = conn.getTypeMap();map.put("TABLEONEEXAMPLE", Class.forName("TestArr")); // YOUR OBJECT TYPE, NOT ARRAY.Object[] values = (Object[]) arr.getArray();for (int i=0; i < values.length; i++){  TestArr a = (TestArr)values[i];  System.out.println("somethingOne: " + a.attrOne);  System.out.println("somethingTwo: " + a.attrTwo);}

结果bieng:

M:documentsSample Code1>javac TestArr.javaM:documentsSample Code1>javac ArrayTest.javaNote: ArrayTest.java uses unchecked or unsafe operations.Note: Recompile with -Xlint:unchecked for details.M:documentsSample CodeSQLComplexArray>java ArrayTestOpening Oracle connection...done.somethingOne: asomethingTwo: 1somethingOne: bsomethingTwo: 2


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/612313.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号