java callable语句帮助使用包过程

时间:2011-04-07 20:06:32

标签: java procedure callable-statement

目前我的包下有这个程序,当有些人在网页上选择user_id时会获取user_name但现在我想通过在页面中添加ssn来改变格式,所以当某个主体选择user_id时,它会在网页文本框中加载用户名和ssn ..我正在使用可调用语句来加载数据..我可以在这里更改以获取用户名和ssn

    PROCEDURE get_User_name
  (
    return_code_out OUT VARCHAR2,
    return_msg_out OUT VARCHAR2,
    user_id_in IN user.user_id%TYPE,
    user_name_out OUT user.user_name%TYPE
  )
  IS

  BEGIN


    return_code_out := '0000';
    return_msg_out := 'OK';

    SELECT c.user_name INTO user_name_out 
      FROM user c
     WHERE c.user_id = user_in;

 END get_user_name;

这是我的加载函数

    public String load() {

        String retMsg = null;
        DbUtil db = null;
        java.sql.CallableStatement cstmt = null;

            try {
            db = new DbUtil();
            cstmt = db.prepareCall("{ call sample.PACK_USER.get_user_name(?, ?, ?, ?) }");

            cstmt.registerOutParameter(1,OracleTypes.VARCHAR);
            cstmt.registerOutParameter(2,OracleTypes.VARCHAR);
            cstmt.setString(3,this.getuserid());
            cstmt.registerOutParameter(4,OracleTypes.VARCHAR);

                cstmt.execute();

            if (cstmt.getString(1).equals(Constants.dbSuccess))
                myLog.log(myLog.INFORMATION, "UserBean.load() DB return code and message = " + retMsg);
            else {
                retMsg =  cstmt.getString(1) + " - " + cstmt.getString(2);
                myLog.log(myLog.ERROR, "UserBean.load() DB return code and message = " + retMsg);
            }

            this.setUserName(cstmt.getString(4));
return retMsg;
    }

2 个答案:

答案 0 :(得分:0)

该程序需要更改为......

PROCEDURE get_User_name

( user_id_in IN user.user_id%TYPE, 
  user_name_out OUT user.user_name%TYPE,
  user_ssn_out  out user.ssn_type%TYPE ) IS

BEGIN

---return_code_out := '0000';  /*catch sql exception in java instead..*/
---return_msg_out := 'OK';     /*catch sql exception in java instead..*/

SELECT c.user_name, c.ssn 
  INTO user_name_out, ssn_out
  FROM user c
 WHERE c.user_id = user_in;

END get_user_name;

从您的代码中,您似乎希望从过程中返回错误代码和错误消息。相反,使用try catch块捕获Java中的sqlException并采取必要的操作。

答案 1 :(得分:0)

这就像Rajesh建议的那样改变你的程序(虽然它取决于你使用的是哪个数据库服务器):

PROCEDURE get_User_name
( user_id_in IN user.user_id%TYPE, 
  user_name_out OUT user.user_name%TYPE,
  user_ssn_out  out user.ssn_type%TYPE ) IS

BEGIN

SELECT c.user_name, c.ssn 
  INTO user_name_out, ssn_out
  FROM user c
  WHERE c.user_id = user_in;

END get_user_name;

并按如下方式更改Java代码:

        cstmt = db.prepareCall("{ call sample.PACK_USER.get_user_name(?, ?, ?) }");

        cstmt.setString(1,this.getuserid());
        cstmt.registerOutParameter(2,OracleTypes.VARCHAR);
        cstmt.registerOutParameter(3,OracleTypes.VARCHAR);
        cstmt.execute();

        String userName = cstmt.getString(2);
        String ssnType = cstmt.getString(3);

您现有的错误代码永远不会起作用 - 您必须通过在某处捕获SQLException来处理错误。