【发布时间】:2015-05-19 09:16:10
【问题描述】:
这可能是一个不好的做法,但我是创建 jsp 的新手。我想执行多个更新 - 使用 if 语句。我想使用大约 6 个查询,但我无法让代码工作。 jsp中是否可以更新多个sql?
这是我的代码:
<html> //dbupdatetam.jsp
<head><title>Update Tamil page</title></head>
<body>
<%@ page import="java.util.* , javax.sql.* , java.sql.*" %>
<%
java.sql.Connection con = null;
java.sql.Statement s = null;
java.sql.ResultSet rs = null;
java.sql.PreparedStatement pst = null;
String var1 = request.getParameter("num1");
String var2 = request.getParameter("num2");
//int var3 = Integer.parseInt(var1);
String url= "jdbc:sqlserver://HOST;databaseName=dbname";
String id= "user";
String pass = "pwd";
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
con = java.sql.DriverManager.getConnection(url, id, pass);
}catch(ClassNotFoundException cnfex){
cnfex.printStackTrace();
}
String sql = "select Genre from tablename where id= '" + var1 + "'";
String sqlFic = "update tablename set StatusID='0', Status= 'Borrowed (" + var2 + ")' where id= '" + var1 + "'";
try{ //try start
s = con.createStatement();
//pst=con.prepareStatement(sql);
rs = s.executeQuery(sql);
%>
<%
String retnValue = null;
if ( rs.next() ){ //while start
retnValue = rs.getString(1);
}
%>
<p>String value is <%=retnValue%></p>
<% if ( retnValue != null) { //ifstart
%>
<p>String value is <%=retnValue%></p>
<%
try{ //try1start
s = con.createStatement();
pst = con.prepareStatement(sqlFic);
int count = s.executeUpdate(sqlFic);
%>
<p>The update is successful.<%=count%> record updated successfully.</p>
<%
} //try1end
catch(Exception e){e.printStackTrace();}
finally{ //finallystart
if(rs!=null) rs.close();
if(s!=null) s.close();
if(con!=null) con.close();
}//finallyend
%>
<% } %>
<%
//} //whileend
%>
<%
} //tryend
catch(Exception e){e.printStackTrace();}
finally { //finallystart
if(rs!=null) rs.close();
if(s!=null) s.close();
if(con!=null) con.close();
} //finallyend
%>
</body>
</html>
这是我得到 var1 和 var2 的地方:
<FORM ACTION="tamupdate.jsp" METHOD="POST">
Enter your Emp ID:
<INPUT TYPE="number" NAME="num1">
<BR>
<b>Please Enter your <b>correct</b> Employee ID as this is where the book you request will be sent.</b>
<br><BR>
Enter the ID of the book you'd like to check the availability:
<INPUT TYPE="number" NAME="num2">
<BR><br>
<INPUT TYPE="SUBMIT" value="Check Availability">
</FORM><br><br>
<jsp:include page="dbupdatetam.jsp">
<jsp:param name="num1" value="bookid"/>
<jsp:param name="num2" value="empid"/>
</jsp:include>
这行不通!我正在使用 tomcat localhost 并通过 Internet Explorer (http://localhost:8080/filename.jsp) 运行 jsp。运行此程序时出现空白屏幕。我怀疑更新查询有问题。谁能回顾一下并告诉我哪里出错了?
【问题讨论】:
标签: java sql sql-server jsp jdbc