0

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

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>
4

1 回答 1

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>
于 2013-03-13T19:00:21.033 回答