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>
相关推荐
JH307316 分钟前
Oracle与MySQL中CONCAT()函数的使用差异
数据库·mysql·oracle
蓝染-惣右介18 分钟前
【MyBatisPlus·最新教程】包含多个改造案例,常用注解、条件构造器、代码生成、静态工具、类型处理器、分页插件、自动填充字段
java·数据库·tomcat·mybatis
冷心笑看丽美人19 分钟前
Spring框架特性及包下载(Java EE 学习笔记04)
数据库
武子康1 小时前
Java-07 深入浅出 MyBatis - 一对多模型 SqlMapConfig 与 Mapper 详细讲解测试
java·开发语言·数据库·sql·mybatis·springboot
代码吐槽菌2 小时前
基于SSM的毕业论文管理系统【附源码】
java·开发语言·数据库·后端·ssm
路有瑶台2 小时前
MySQL数据库学习(持续更新ing)
数据库·学习·mysql
数字扫地僧2 小时前
WebLogic 版本升级的注意事项与流程
数据库
lwprain2 小时前
常用docker应用部署,wordpress、mysql、tomcat、nginx、redis
mysql·docker·tomcat
Viktor_Ye3 小时前
高效集成易快报与金蝶应付单的方案
java·前端·数据库
努力算法的小明3 小时前
SQL 复杂查询
数据库·sql