java callable语句帮助使用包过程

发布于 2024-10-30 22:20:25 字数 1599 浏览 4 评论 0原文

目前,我在包下有一个程序,当某些人在网页上选择 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;
    }

Currently i have this procedure under package that gets user_name when some selects user_id on web page but now i wanna change the format by adding ssn to the page so when some body selects user_id it loads user name as well as ssn on webpage textbox.. i am using callable statement to load data.. what can i change here to get both user name and 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;

and here is my load function

    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;
    }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

×纯※雪 2024-11-06 22:20:25

因此,这就是按照 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 来处理错误。

So that is change your procedure as Rajesh suggested (though it does depend on which database server you are using):

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;

AND change the Java code as follows:

        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);

Your existing error code would never work - you must handle errors by catching an SQLException somewhere.

旧伤还要旧人安 2024-11-06 22:20:25

该过程需要更改为...

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 并采取必要的操作。

The procedure would need to be changed to...

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;

From your code, it looks like you want to return the error code and error message out from the procedure. Instead, use the try catch block to catch the sqlException in Java and take the necessary action.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文