how to call stored function of IN and OUT paramete

2019-09-08 08:45发布

问题:

public String getObjectDetails(String buffer) {
        dbcon con = new dbcon();        
        ResultSet rs = null;
        String name_msg=null;
        String type_msg=null;
        int type_code=0;
        int name_code=0;
        try {
            rs = con.dbstate().createStatement().executeQuery("SELECT pkg_test1.get_object_type('"+buffer+"',"+type_code+",'"+type_msg+"'),pkg_test1.get_object_name('"+buffer+"',"+name_code+",'"+name_msg+"') from dual");
            if (rs.next()) {
                return rs.getString(1) + "," + rs.getString(2);
            }
        } catch (SQLException ex) {
            Logger.getLogger(username.class.getName()).log(Level.SEVERE, null, ex);
        }
        return "";
    }

And, I'm trying to get information from these two function declared inside PKG_TEST1 package

FUNCTION get_object_type(i_buffer IN VARCHAR2, o_errcode OUT NUMBER,
                           o_errmsg OUT VARCHAR2) RETURN VARCHAR2;

  FUNCTION get_object_name(i_buffer IN VARCHAR2, o_errcode OUT NUMBER,
                           o_errmsg OUT VARCHAR2) RETURN VARCHAR2;

But, I'm getting error

java.sql.SQLException: ORA-06572: Function GET_OBJECT_NAME has out arguments

Please help me to solve this problem.

回答1:

try this

    CallableStatement c = conn.prepareCall("{? = call get_object_type(?, ?)}");
    c.registerOutParameter(1, Types.VARCHAR);
    c.registerOutParameter(3, Types.VARCHAR);
    c.setString(2, buffer);
    c.execute();
    String res = c.getString(1);
    String err = c.getString(3);


回答2:

This works for me, thanks @home to suggesting me good link.

String call = "{ ? = call pkg_test1.get_object_type(?,?,?)}";
CallableStatement cstmt = con.dbstate().prepareCall(call);
 cstmt.registerOutParameter(1, oracle.jdbc.OracleTypes.VARCHAR);
cstmt.setString(2, buffer);
cstmt.registerOutParameter(3, oracle.jdbc.OracleTypes.NUMBER);
cstmt.registerOutParameter(4, oracle.jdbc.OracleTypes.VARCHAR);
cstmt.executeQuery();
String type = cstmt.getString(1);

Well Explained Here : How to Call Strored Procedure and Function