JSP中进行SQL操作抛出异常
好奇怪的事情 我记得同样的代码 第一次运行的时候正常运行并且能实现删除操作 然后我重新进入页面进行操作就抛出异常了
这个JSP程序(delete.jsp)是利用从一个页面获取条件,然后从数据库中删除相应条件的数据。
delete.jsp
<%@ page language="java" import="java.util.*" pageEncoding="GBK"%>
<%@ page import="java.sql.*"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<html>
<body>
<%
request.setCharacterEncoding("GBK");
String Tname = request.getParameter("Tname");
Connection conn = null;
Statement stat = null;
ResultSet rs = null;
Class.forName("com.mysql.jdbc.Driver");
String url = "jdbc:mysql://localhost:3306/mldn";
String user = "root";
String password = "root";
conn = DriverManager.getConnection(url,user,password);
stat = conn.createStatement();
stat.executeUpdate("delete from textinfo where Tname = " + Tname + "");
rs = stat.executeQuery("select * from textinfo");
if(rs.next())
{
out.print("<center><br><br><h3>删除成功!</h3></center>");
}
else{
out.print("<center><h3>删除失败!</h3></center>");
}
%>
<br>
<br>
<center> <a href=addStuInfo.jsp>返回添加信息页面</a> <a href=showInfo.jsp>返回信息查询页面</a></center>
<%
if(rs != null)
{
rs.close();
rs = null;
}
if(stat != null)
{
stat.close();
stat = null;
}
if(conn != null)
{
conn.close();
conn = null;
}
%>
</body>
</html>
<a href="delete.jsp?Tname=<%=rs.getString("Tname") %>">删除</a>
19: String password = "root";
20: conn = DriverManager.getConnection(url,user,password);
21: stat = conn.createStatement();
22: stat.executeUpdate("delete from textinfo where Tname = " + Tname + "");
23:
24:
25: rs = stat.executeQuery("select * from textinfo");
[解决办法]
检查20行里面的user,url写的对不?
[解决办法]
..... ...有同样经历
[解决办法]
常有的事,因为一点小情况出异常,然后半天找不到原因……
[解决办法]
要细心哦
delete from textinfo where Tname = " + Tname + ""
[解决办法]
多检查,多注意。
[解决办法]
"delete from textinfo where Tname = " + Tname + "" ------->
"delete from textinfo where Tname = '" + Tname + "'"
[解决办法]
正常的,写在jsp里经常会发生莫名的问题。
[解决办法]
加单引号的问题,什么变量是字符串。。。
[解决办法]
SQL语句错了
[解决办法]
delete from textinfo where Tname = " + Tname 把后面的+“”去掉