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>
相关推荐
苹果醋31 小时前
React源码02 - 基础知识 React API 一览
java·运维·spring boot·mysql·nginx
了一li2 小时前
Qt中的QProcess与Boost.Interprocess:实现多进程编程
服务器·数据库·qt
码农君莫笑2 小时前
信管通低代码信息管理系统应用平台
linux·数据库·windows·低代码·c#·.net·visual studio
别致的影分身2 小时前
使用C语言连接MySQL
数据库·mysql
过过过呀Glik2 小时前
在 Ubuntu 上安装 MySQL 的详细指南
mysql·ubuntu
京东零售技术4 小时前
“慢”增长时代的企业数据体系建设:超越数据中台
数据库
sdaxue.com4 小时前
帝国CMS:如何去掉帝国CMS登录界面的认证码登录
数据库·github·网站·帝国cms·认证码
o(╥﹏╥)5 小时前
linux(ubuntu )卡死怎么强制重启
linux·数据库·ubuntu·系统安全
阿里嘎多学长5 小时前
docker怎么部署高斯数据库
运维·数据库·docker·容器
Yuan_o_5 小时前
Linux 基本使用和程序部署
java·linux·运维·服务器·数据库·后端