表的准备:
-- MySQL Administrator dump 1.4
--
-- ------------------------------------------------------
-- Server version 5.1.40-community
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */;
/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;
/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */;
--
-- Create schema itnews
--CREATE DATABASE IF NOT EXISTS itnews;
USE itnews;--
-- Definition of table `news`
--DROP TABLE IF EXISTS `news`;
CREATE TABLE `news` (
`id` int(10) unsigned NOT NULL AUTO_INCREMENT,
`title` varchar(50) NOT NULL,
`content` text NOT NULL,
`begintime` datetime NOT NULL,
`username` varchar(45) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=7 DEFAULT CHARSET=gbk;--
-- Dumping data for table `news`
--/*!40000 ALTER TABLE `news` DISABLE KEYS */;
INSERT INTO `news` (`id`,`title`,`content`,`begintime`,`username`) VALUES
(2,'奔迈pre','新宠','2012-01-10 00:00:00','dmy'),
(6,'NokiaE66','女白领最爱','2012-09-07 00:00:00','abc');
/*!40000 ALTER TABLE `news` ENABLE KEYS */;
/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK" import="java.sql.*,javax.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=GBK">
<title>IT资讯新闻系统</title>
<script type="text/javascript">
function checkdel(){
var allCheckBoxs=document.getElementsByName("newsid");
var flag=false;
for(i=0;i<allCheckBoxs.length;i++){
if(allCheckBoxs[i].type=="checkbox"){
if(allCheckBoxs[i].checked){
flag=true;
break;
}
}
}
if(!flag){
alert("请选择要删除的记录!");
return false;
}
else{
if(confirm("确定要删除吗?")) frm.submit();
}
}
</script>
</head>
<body>
<div align="center">
<h1>IT新闻资讯</h1>
<form action="dodel.jsp" method="post" name="frm">
<table border="1">
<tr><td>序号</td><td>删除/批量</td> <td>新闻标题</td><td> 新闻内容</td><td> 作者</td><td> 发布时间</td>
</tr>
<%
Connection conn = null; // 数据库连接
PreparedStatement pstmt=null;
ResultSet rs=null;//结果集对象
int i=1;
String url="jdbc:mysql://mysql.sqlpub.com:3306/huangjin";
Class.forName("com.mysql.cj.jdbc.Driver");
conn=DriverManager .getConnection(url,"laocooon","fc12f7a5215e8e0a");
String sql="select * from news";
pstmt=conn.prepareStatement(sql);
rs=pstmt.executeQuery();
while (rs.next()){ %>
<tr>
<td><%=i %></td><td><input type="checkbox" name="newsid" value="<%=rs.getInt(1)%>"></td>
<td><%=rs.getString(2) %></td>
<td><%=rs.getString(3) %></td>
<td><%=rs.getString(5) %></td>
<td><%=rs.getDate(4) %></td>
</tr>
<% i++;
}
rs.close();
pstmt.close();
conn.close();
%>
<tr><td colspan="6"><input type="button" value="删除" onClick="checkdel()"></td></tr>
</table>
</form>
</div>
</body>
</html>
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK" import="java.sql.*,javax.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<% String delid[]=request.getParameterValues("newsid" );
Connection conn = null; // 数据库连接
PreparedStatement pstmt = null; // 创建Statement对象
int row=0;//受影响的记录行数
for(int i=0;i<delid.length;i++){
int id=Integer.parseInt(delid[i]);
try{
String url="jdbc:mysql://mysql.sqlpub.com:3306/huangjin";
Class.forName("com.mysql.cj.jdbc.Driver");
conn=DriverManager .getConnection(url,"laocooon","fc12f7a5215e8e0a");
String sql="delete from news where id=?";
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1,id);
row=pstmt.executeUpdate();
}catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally{
if(pstmt != null){
try { pstmt.close();} catch (SQLException e) {e.printStackTrace();}
}
if(conn != null){
try { conn.close();} catch (SQLException e) {e.printStackTrace();}
}
}
}
if(row>0) {
out.println("<script type='text/javascript'>alert('成功删除。');</script>");
response.sendRedirect("index.jsp");
}
else out.println("<script type='text/javascript'>alert('删除失败。');</script>");
%>
</body>
</html>
dodel.jsp
<%@ page language="java" contentType="text/html; charset=GBK"
pageEncoding="GBK" import="java.sql.*,javax.sql.*"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<% String delid[]=request.getParameterValues("newsid" );
if(delid==null){
out.println("<script type='text/javascript'>alert('删除失败。');</script>");
return ;
}
Connection conn = null; // 数据库连接
PreparedStatement pstmt = null; // 创建Statement对象
String url="jdbc:mysql://mysql.sqlpub.com:3306/huangjin";
Class.forName("com.mysql.cj.jdbc.Driver");
conn=DriverManager .getConnection(url,"laocooon","fc12f7a5215e8e0a");
int row=0;//受影响的记录行数
for(int i=0;i<delid.length;i++){
int id=Integer.parseInt(delid[i]);
String sql="delete from news where id=?";
pstmt=conn.prepareStatement(sql);
pstmt.setInt(1,id);
row=pstmt.executeUpdate();
}
if(row>0) {
out.println("<script type='text/javascript'>alert('成功删除。');</script>");
response.sendRedirect("index.jsp");
}
else
out.println("<script type='text/javascript'>alert('删除失败。');</script>");
%>
</body>
</html>