gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-01 14:46:40 26 4
gpt4 key购买 nike

下面是我的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>

最佳答案

首先按如下方式修改您的程序

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>

关于java - 通过jsp调用存储过程时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15382483/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com