无法从jsp中的存储过程中获取值

时间:2015-10-22 03:27:50

标签: sql-server jsp stored-procedures

我试图从jsp中的存储过程中获取返回值/整数,但它不起作用。我的代码如下:

<%
        if(request.getParameter("siteName") != null)
        {
        session.setAttribute("siteName", request.getParameter("siteName"));

        java.sql.Connection con;                
        java.sql.Statement st;
        java.sql.ResultSet rs;

        con = null;
        st = null;
        rs = null;

        if(session.getAttribute("user") != null)
        {
                UserVO user = (UserVO) session.getAttribute("user");
                int userId = user.getUserId();



            String sql2 = "EXEC [db1].[dbo].[check_site_name] @site_name='"+ session.getAttribute("siteName") +"', @user_id="+ userId +"";

%>
            var sn = "<%=session.getAttribute("siteName")%>";
            var cn = "<%=session.getAttribute("user")%>";

    <%
            try
            {
                con = ConnectUtil.getCon();
                st = con.createStatement();
                rs = st.executeQuery(sql2);


                int result = rs.getInt("result");
                if(result == 0){
    %>
             do something....

我想获得@result值以及存储过程的返回值。 我在存储过程中的代码:

ALTER  PROCEDURE [dbo].[check_site_name]
    @site_name nvarchar(300),
    @user_id int = 0,
    @platform nvarchar(300) = '',
    @acc_no nvarchar(50) = '',
    @result int = 0 OUTPUT
AS
BEGIN
    SET NOCOUNT ON;
IF EXISTS(SELECT 1 FROM siteName_booking WHERE site_name = @site_name AND is_deleted = 0) AND EXISTS(SELECT 1 FROM 
siteName_registration WHERE site_name = @site_name AND invite = 1 and acc_no = @acc_no and prod_platform = @platform)
    BEGIN
        -- PRINT 'available
        RETURN 0
    END
    ELSE
    BEGIN   
        IF EXISTS(SELECT 1 FROM siteName_booking WHERE site_name = @site_name AND is_deleted = 0)
        BEGIN
            -- PRINT not available
            SET @result = 1;
            RAISERROR (N'Sitename is not available', 16, 1);
            RETURN -1
        END

我需要一些帮助......

1 个答案:

答案 0 :(得分:0)

要从jdbc驱动程序运行存储过程,您需要执行以下操作:

CallableStatement callable = con.prepareCall(sql2);

然后在callable上设置各种IN参数,并执行它。