通过jsp调用存储过程时出错

时间:2013-03-13 10:18:50

标签: java jsp jdbc

下面是我的jsp程序没有正确执行。它直接返回异常错误消息.jdbc连接在include文件中。其中batchcode和ccode是复合键.program应首先检查这些是否已插入数据库或不,然后它应该插入。

procedure:
create or replace procedure udp_addbatch(
p_batchcode  varchar2,
p_ccode  varchar2
)
as 
BEGIN
INSERT INTO BATCHES(BATCHCODE,CCODE) VALUES(p_batchcode,p_ccode);
commit;
END ;
/

**jsp file**

        <html>
<%@page import="java.sql.*, java.lang.String" %>
<head>
<style type='text/css'>
input{
     FONT-FAMILY: Verdana; FONT-SIZE:10pt; color:blue;
}
body{ 
    FONT-FAMILY:Verdana;
    FONT-SIZE:10pt;
    FONT-WEIGHT:Bold;
    }
table{
    FONT-FAMILY:Verdana;
    FONT-SIZE:10pt;
    FONT-WEIGHT:Bold;
     }
H1,H2,H3{
    FONT-FAMILY:Verdana;
    FONT-SIZE:13pt;
    FONT-WEIGHT:Bold;
    COLOR:BLUE;
    }
SELECT {
        font-family : verdana; font-size : 9pt; background-color : #FCFCFC; border: 1px solid #000000; color:blue;
        } 
TEXTAREA { 
    font-family : verdana; font-size : 9pt; background-color : #FCFCFC; border: 1px solid #000000; color:blue;
        }
</style>
</head>
<body bgcolor="white">
<%@include file="jdbcresults.jsp"%>
<%


    ResultSet rs=null;
    int nr;
        CallableStatement cstmt=null;
    try
    {   


        String BatchCode=request.getParameter("BatchCode");
        String CCode=request.getParameter("CCode");

        cstmt=con.prepareCall("{call udp_addbatch(?,?)}");
                cstmt.setString(1,BatchCode);
                cstmt.setString(2,CCode);



                rs=cstmt.executeQuery("select * from Batches where CCode='"+CCode+"' and BatchCode='"+BatchCode+"'");
        if (!rs.next())
        {
            cstmt.executeUpdate();
            out.println("<h2 align='center'> Batch and course Successfully added</h2>");
        }
        else
            {
                            out.println("<h2 align='center'> Batch and Course already Exists</h2>");
               out.println("<center><a href='Addbatch.jsp'>Go Back</a></center>");
                }

         rs.close();
        cstmt.close();
         con.close();
    }



catch(Exception e){ e.printStackTrace();} 
        out.print("<h2 align='center'>course doesn't exist");
                out.println("<center><a href='Addbatch.jsp'>Go Back</a></center>");
                }
finally{
        // The finally clause is always executed - even in error
        // conditions PreparedStatements and Connections will always be closed
        try
        {
                  if (cstmt = null)
                              cstmt.close();
        }
        catch(Exception e) {}

        try
        {
                  if (con = null)
                              con.close();
        }
        catch (Exception e){}
        }
}

    %>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

首先修改您的程序如下

CREATE OR REPLACE
PROCEDURE udp_addbatch(
    p_batchcode IN VARCHAR2,
    p_ccode     IN VARCHAR2,
    o_var OUT NUMBER )
AS
  v_count NUMBER;
BEGIN
  SELECT COUNT(*)
  INTO v_count
  FROM batches
  WHERE BATCHCODE=p_batchcode
  AND CCODE      =p_ccode;
  IF(v_count < 1 ) THEN
    INSERT INTO BATCHES
      (BATCHCODE,CCODE
      ) VALUES
      (p_batchcode,p_ccode
      );
    o_var := 1;
  ELSE
    o_var := 0;
  END IF;
END ;
-- you can add exception handling
/

并且在JSP中执行如下操作,但这并未经过全面测试,将Java代码移出JSP并使用单独的类进行数据库连接总是更好。

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<%@ page contentType="text/html;charset=windows-1252"%>
<html></html>
<html>
        <%@ page import="java.sql.*, java.lang.String"%>
        <head>
                <style type='text/css'>
input{
     FONT-FAMILY: Verdana; FONT-SIZE:10pt; color:blue;
}
body{ 
    FONT-FAMILY:Verdana;
    FONT-SIZE:10pt;
    FONT-WEIGHT:Bold;
    }
table{
    FONT-FAMILY:Verdana;
    FONT-SIZE:10pt;
    FONT-WEIGHT:Bold;
     }
H1,H2,H3{
    FONT-FAMILY:Verdana;
    FONT-SIZE:13pt;
    FONT-WEIGHT:Bold;
    COLOR:BLUE;
    }
SELECT {
        font-family : verdana; font-size : 9pt; background-color : #FCFCFC; border: 1px solid #000000; color:blue;
        } 
TEXTAREA { 
    font-family : verdana; font-size : 9pt; background-color : #FCFCFC; border: 1px solid #000000; color:blue;
        }
</style>
        </head>
        <body bgcolor="white"><%
     // Hope you are getting the connection part  
        Connection con = null; 
        CallableStatement cstmt=null;
        String BatchCode=request.getParameter("BatchCode");
        String CCode=request.getParameter("CCode");

 try {
cstmt=con.prepareCall("{call udp_addbatch(?,?,?)}");
                cstmt.setString(1,BatchCode);
                cstmt.setString(2,CCode);                
                cstmt.registerOutParameter(3, Types.INTEGER);     
                cstmt.executeUpdate(); 
                int val = cstmt.getInt(3);

       if (val == 1) {
       out.println("<h2 align='center'> Batch and course Successfully added</h2>");
       }
       else {
       out.println("<h2 align='center'> Batch and Course already Exists</h2>");
       // and do your stuff
       }
       }
       catch(Exception e){
       e.printStackTrace();
       }
       finally {
       cstmt.close();
       con.close();
       }

%></body>
</html>