求助:一个删除语句的问题
index.jsp
<%@ page language="java" import="java.util.*" pageEncoding="gb2312"%>
<%@ page import="com.bean.*" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'index.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<jsp:useBean id="datamanger" class="com.dbc.DataManger"></jsp:useBean>
<body>
<table width=80% border="1" bgcolor="#FFFFF">
<tr>
<td>分院名称</td>
<td>班级名称</td>
<td>班主任</td>
<td>学习委员</td>
<td>修改数据</td>
<td>删除数据</td>
</tr>
<%
Collection temp = datamanger.getAllData();
Iterator it = temp.iterator();
while(it.hasNext()) {
ClassBean cb = (ClassBean)it.next();
out.println("<td>" + cb.getPartmentname() + "</td>");
out.println("<td>" + cb.getClassname() + "</td>");
out.println("<td>" + cb.getClasshead() + "</td>");
out.println("<td>" + cb.getStudenthead() + "</td>");
out.println("<td><a href='#'>修改</a></td>");
out.println("<td><a href='class_delete.jsp?Classid="+cb.getClassid()+"'>删除</a></td>");
}
%>
</table>
</body>
</html>
class_delete.jsp
<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>My JSP 'class_delete.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<jsp:useBean id="datamanger" class="com.dbc.DataManger"></jsp:useBean>
<body>
<%
datamanger.ClassDelete((String)request.getAttribute("Classid"));
response.sendRedirect("index.jsp");
%>
</body>
</html>
下面是一个javabean,对数据库操作
/**
*
*/
package com.dbc;
import java.sql.*;
import java.util.*;
import com.bean.*;
/**
* @author Administrator
*
*/
public class DataManger {
Connection conn = null;
public DataManger() {
conn = DataConnection.getConnection(); //获得数据库的连接
}
public Collection getAllData() throws SQLException {
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select Partmentname,Classname,Classhead,Studenthead " +
"from Classes left join Partment on Classes.Partmentid=Partment.partmentid");
Collection list = new ArrayList();
while(rs.next()) {
ClassBean cb = new ClassBean();
cb.setClassid(rs.getString("Classid"));
cb.setPartmentname(rs.getString("Partmentname"));
cb.setClassname(rs.getString("Classname"));
cb.setClasshead(rs.getString("Classhead"));
cb.setStudenthead(rs.getString("Studenthead"));
list.add(cb);
}
return list;
}
public void ClassDelete(String Classid) throws SQLException {
Statement stmt = conn.createStatement();
stmt.execute("delete from Classes where Classid='" + Classid + "'");
}
}
各位大侠们,为什么不能执行删除功能,用debug调试下,Classid值为空,这是为什么??
[解决办法]
在刚开始的时候先定义一个变量来存储classId的值 然后再在后面用这个变量
原因:在cb.getClassname() 时 要按照顺序获得
[解决办法]
public void ClassDelete(String Classid) throws SQLException {Statement stmt = conn.createStatement();stmt.execute("delete from Classes where Classid='" + Classid + "'");}}