2011-11-30 158 views
1

我在Java中的代码调用Oracle DB中的存储过程,并返回带有一些字段的对象。 当我找到对象的属性 - 我有问题的字符串。该字符串变成'???' (3个任务标记),它不应该是那个! (整数返回OK)使用'???'从Oracle存储过程返回给Java的字符串

我在DB上测试了我的存储过程 - 它运行良好。

我使用调用数据库的小本地主程序测试了我的Java代码 - 它运行良好。 (数据库直接连接到DriverManager.getConnection(“jdbc:oracle:thin:@ ......);)

问题出在当我在我的大项目中使用我的存储过程连接到。数据库与WebLogic

你知道如何从数据库得到正确的字符串时,我与WebLogic工作

甲骨文代码:

PROCEDURE SearchOrder (InWoArr IN WoTab, 
        OutWoAccStat OUT WoAccStatTab) as 
    outRec WoAccStatType; 
    wo number(10); 
    acc number(10); 
    stat varchar2(2); 
    begin 
    OutWoAccStat := WoAccStatTab(); 
    for i in InWoArr.FIRST .. InWoArr.LAST loop 
    OutWoAccStat.EXTEND; 
     begin 
     select work_order_number,account_number,' ' 
    into wo,acc,stat 
    from table1 
    where work_order_number=InWoArr(i); 
    .... 
    outRec := WoAccStatType(wo,acc,stat); 
    OutWoAccStat(i) := outRec; 
    exception 
    when no_data_found then 
     outRec := WoAccStatType(InWoArr(i),0,' '); 
     OutWoAccStat(i) := outRec; 
    end; 
end loop; 
end SearchOrder; 

//数组200

create or replace type poldev_dba.WoAccStatTab as VARRAY(200) of WoAccStatType 

//阵列型

create or replace type poldev_dba.WoAccStatType as object (work_order_number number(10), account_number number(10), wo_status varchar2(2)) 

// Java代码:

   //Store Procedure Name 
      CallableStatement cs = (CallableStatement) con.prepareCall("{ call spp.SearchOrder(?, ?)}");                      

      //input: 
      cs.setArray(1,woInput); 

      //Output: 
      cs.registerOutParameter(2,OracleTypes.ARRAY,"WOACCSTATTAB"); 

      //Run the query... 
      cs.execute(); 

      //Retrieve Array: 
      woAccArray = (ARRAY)cs.getArray(2); 
      woAccRecs = (Object[])woAccArray.getArray(); 

      int wo = 0; 
      int acc = 0; 
      String stat; 

      for (int i = 0; i < woAccRecs.length; i++) { 
       /* Since we don't know the type of the record, get it into STRUCT !! */ 
       STRUCT woAccRec = (oracle.sql.STRUCT)woAccRecs[i]; 
       /* Get the attributes - nothing but the columns of the table */ 
       Object[] attributes = woAccRec.getAttributes(); 

       /* attribute 0 - work order */ 
       wo = Integer.parseInt("" + attributes[0]); 

       /* attribute 1 - account number */ 
       acc = Integer.parseInt("" + attributes[1]); 

       /* attribute 2 - status */ 
       stat = (String) attributes[2]; 
       /*PROBLEM!!!! stat returned value '???'*/ 

       System.out.println("wo = " + wo + ",acc = " + acc +", status = "+stat); 
+0

应该是什么字符串? *显示字符串时,我怀疑编码和字体问题。尝试调试你的程序,并检查从存储过程中返回的字符串的内容,以了解它是否真的? –

+0

该字符串是'C'或'O'。没有工作。我将其更改为'0'或'' - 仍然无效。 (通过连接到数据库的小型主服务器,它工作并返回'C'和'O') – user1072865

+0

您是否尝试过使用NVARCHAR2而不是VARCHAR2? – SWeko

回答

0

问题是别处。 Oracle DB和DB驱动程序都不会用???替换结果列。搜索此字符串的产品代码以了解为什么使用该字符串。

+0

我添加了我的Java代码。我不认为代码本身的问题。如果我错了 - 请纠正我。 – user1072865

+0

你的代码看起来正确。尝试使用不同版本的Oracle JDBC驱动程序;也许这是一个错误。如果这不起作用,请用Java代码替换存储过程。我总是尽量避免存储过程,因为它们通常会导致比解决问题更多的问题。使用它们的主要原因是:a)在通用API中隐藏数据库特定代码(当您编写支持许多数据库的产品时);或者b)当您需要JDBC不支持的功能时。 –

+0

“我总是尽量避免存储过程,因为它们通常会导致比解决问题更多的问题” - 这是一个糟糕的评论,事实上不正确,存储过程非常有用,也是排序和限制从数据库返回记录的最佳方法。仅仅因为你对他们有麻烦并不意味着他们要被所有人避免。 – Ollie