IT新闻资讯系统,使用mysql作为后台数据库,此系统具有显示数据库中的所有信息和删除两大功能。

表的准备:

-- 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 */;

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=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>
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

java 复制代码
<%@ 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>
相关推荐
vvvae12345 小时前
分布式数据库
数据库
雪域迷影6 小时前
PostgreSQL Docker Error – 5432: 地址已被占用
数据库·docker·postgresql
bug菌¹6 小时前
滚雪球学Oracle[4.2讲]:PL/SQL基础语法
数据库·oracle
逸巽散人7 小时前
SQL基础教程
数据库·sql·oracle
月空MoonSky7 小时前
Oracle中TRUNC()函数详解
数据库·sql·oracle
momo小菜pa7 小时前
【MySQL 06】表的增删查改
数据库·mysql
向上的车轮8 小时前
Django学习笔记二:数据库操作详解
数据库·django
编程老船长8 小时前
第26章 Java操作Mongodb实现数据持久化
数据库·后端·mongodb
全栈师9 小时前
SQL Server中关于个性化需求批量删除表的做法
数据库·oracle
Data 3179 小时前
Hive数仓操作(十七)
大数据·数据库·数据仓库·hive·hadoop