jsp 中的多个 sql 查询
multiple sql queries in jsp
这可能是一种不好的做法,但我是创建 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>
这行不通!我正在通过 Internet Explorer (http://localhost:8080/filename.jsp) 使用 tomcat localhost 和 运行 jsp。 运行 时出现空白屏幕。我怀疑更新查询有问题。谁能评论一下并告诉我哪里出错了?
如果您不连接到多个数据库,则无需创建两个连接对象,因为您可以通过创建单个对象来实现此要求。
我发现您在更新记录时正在使用 executeQuery() 方法。在执行任何 DML 操作时使用 Connection 对象的 executeUpdate() 方法。它 returns 一个整数值。
下面是有效的 jsp 代码。
<%@ page import="java.util.* , javax.sql.* , java.sql.*" %>
<%
Connection con = null;
java.sql.Statement s = null;
java.sql.ResultSet rs = null;
int var3 = Integer.parseInt(request.getParameter("num1"));
int var4 = Integer.parseInt(request.getParameter("num2"));
String url= "***";
String id= "***";
String pass = "***";
try{
Class.forName("com.mysql.jdbc.Driver");
con = java.sql.DriverManager.getConnection(url, id, pass);
}catch(ClassNotFoundException cnfex){
cnfex.printStackTrace();
}
String sql = "select name from demo where id="+var3;
String sql1 = "update demo set name='XYZ' where id="+var4;
try{ //try start
s = con.createStatement();
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
%>
<%
try{ //try1start
int count = s.executeUpdate(sql1);
%>
<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>
这可能是一种不好的做法,但我是创建 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>
这行不通!我正在通过 Internet Explorer (http://localhost:8080/filename.jsp) 使用 tomcat localhost 和 运行 jsp。 运行 时出现空白屏幕。我怀疑更新查询有问题。谁能评论一下并告诉我哪里出错了?
如果您不连接到多个数据库,则无需创建两个连接对象,因为您可以通过创建单个对象来实现此要求。 我发现您在更新记录时正在使用 executeQuery() 方法。在执行任何 DML 操作时使用 Connection 对象的 executeUpdate() 方法。它 returns 一个整数值。
下面是有效的 jsp 代码。
<%@ page import="java.util.* , javax.sql.* , java.sql.*" %>
<%
Connection con = null;
java.sql.Statement s = null;
java.sql.ResultSet rs = null;
int var3 = Integer.parseInt(request.getParameter("num1"));
int var4 = Integer.parseInt(request.getParameter("num2"));
String url= "***";
String id= "***";
String pass = "***";
try{
Class.forName("com.mysql.jdbc.Driver");
con = java.sql.DriverManager.getConnection(url, id, pass);
}catch(ClassNotFoundException cnfex){
cnfex.printStackTrace();
}
String sql = "select name from demo where id="+var3;
String sql1 = "update demo set name='XYZ' where id="+var4;
try{ //try start
s = con.createStatement();
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
%>
<%
try{ //try1start
int count = s.executeUpdate(sql1);
%>
<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>