通过 Rest API returns 调用存储过程字符串为空而不是有效字符串

calling Stored Procedure String via Rest API returns empty instead of valid String

作为后端开发人员,我想调用连接Oracle 9i 数据库的存储过程来获取单个输出字符串。

实际上,输出参数的结果为字符串,returns为空。

你能告诉我要修改什么作品吗?

这是我的休息控制器:

@GetMapping(value = { "/third"})
public String getVariable (){
   String  result = "" ;

   Connection conn;
   conn = getConnection() ;

   CallableStatement cstmt = null;

   try {
       String SQL = "{call pkg1234.get_pc_lookup_value_second(?,?)}";
       cstmt = conn.prepareCall (SQL);
       cstmt.setString(1, "TES");         
       cstmt.registerOutParameter(2,  oracle.jdbc.OracleTypes.VARCHAR );
       cstmt.execute();

       String dddresult = cstmt.getString(2);
       System.out.println(" Record :"+dddresult);
       result = dddresult; 
    }
   catch (SQLException e) {
       System.out.println(" go this");
       e.printStackTrace();
  }
   return result ;
}

这是我的存储过程:

PROCEDURE get_pc_lookup_value_second (
    i_lookup_code    IN       VARCHAR2,
    o_lookup_value   OUT      VARCHAR2
)
o_lookup_value := '456';
END get_pc_lookup_value_second;

@raju, 它可能对你有帮助,使用带有 Springboot 注释的 Jpa 模板。

 Controller class
    {
    @Autowired
    B b;
    @GetMapping(value = { "/third"})
        public String    getVariable (){
        return b.getInfo();
    }
    }
    @service
    B
    {
    @Autowired 
    A a;
    public String getInfo()
    {
    return a.getoutput();
    }
    @repository 
    interface A extends JpaRepository<Mention your Entity,String>()
    {
    @procedure(name="Execute your_procedure name")
    //return type
    public String getoutput();
    }
    }

为了简单起见,试试我自己的库:

<dependency>
   <groupId>com.github.buckelieg</groupId>
   <artifactId>db-fn</artifactId>
   <version>0.3.4</version>
</dependency>

然后在代码中:

try(DB db = new DB("your-connection-string")) {
    String result = db.procedure("{call pkg1234.get_pc_lookup_value_second(?,?)}", P.in("TES"), P.out(JDBCType.VARCHAR)).call(cs -> cs.getString(2)).orElse("Unknown");
}

查看更多here

您报告的代码如下:

try(DB db = new DB("jdbc:oracle:thin:username/password@10.8.12.6:1521:dev")) {
    String thisString = db.procedure("{call b_pc_mob_portal_pkg.get_pc_lookup_value_second(?,?)}", 
            P.in("TES" , "i_lookup_code"),
            P.out(JDBCType.LONGNVARCHAR , "o_lookup_value" )).call(cs -> cs.getString(2)).orElse("Unknown");
    System.out.println( "thisString String  :" + thisString );

}

您报告说这给出了:

[Ljava.lang.Object; cannot be cast to [Lbuckelieg.fn.db.P;

您问过:

How can I check on whether this method need to be revised.

这是因为您尝试将命名参数与匿名参数结合使用。要么删除参数名称:

db.procedure("{call b_pc_mob_portal_pkg.get_pc_lookup_value_second(?,?)}", P.in("TES"), P.out(JDBCTtype.VARCHAR))...

或在过程调用语句中添加名称:

db.procedure("{call b_pc_mob_portal_pkg.get_pc_lookup_value_second(:i_lookup_code,:o_lookup_value)}", P.in("i_lookup_code", "TES"), P.out(JDBCType.VARCHAR , "o_lookup_value" ))...

您还报告说您尝试过此代码:

try(DB db = new DB("jdbc:oracle:thin:username/password@10.8.12.6:1521:dev")) {
        String thisString = db.procedure("{call b_pc_mob_portal_pkg.get_pc_lookup_value_second(?,?)}", 
                P.in("TES" ),
                P.out(JDBCType.VARCHAR )).call(cs -> cs.getString(2)).orElse("Unknown");
        System.out.println( "thisString String  :" + thisString );
        }

你说过:

when I do it in this way, it gives:

buckelieg.fn.db.SQLRuntimeException: [registerOutParameter not implemented]

我认为问题出在 JDBC 驱动程序中。您使用哪个版本?可以更新到更新的吗?

您说过这是您的数据库:

还有你已经下载:

OJDBC7.jar

https://www.oracle.com/database/technologies/jdbc-drivers-12c-downloads.html

Here is the Oracle IDE Version fetched by the Oracle SQL Developer

非常有趣。我会调查这个问题。

作为临时解决方法,我建议执行以下操作:

重写程序如下:

CREATE OR REPLACE FUNCTION b_pc_mob_portal_pkg.get_pc_lookup_value_second(IN param1 VARCHAR2 DEFAULT 'TES') 
RETURN VARCHAR2 IS
BEGIN
    RETURN '456';
END b_pc_mob_portal_pkg.get_pc_lookup_value_second;

然后在代码中:

String value = db.select("SELECT b_pc_mob_portal_pkg.get_pc_lookup_value_second(?) AS output FROM DUAL", "TES").single(rs -> rs.getString("output")).orElse("Unknown");

How about procedure?

您是否尝试过最新版本 (0.3.6) 的库?

 <dependency>
  <groupId>com.github.buckelieg</groupId>
  <artifactId>db-fn</artifactId>
  <version>0.3.6</version>
</dependency>