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>
相关推荐
周全全7 分钟前
MySQL报错解决:The user specified as a definer (‘root‘@‘%‘) does not exist
android·数据库·mysql
白云如幻11 分钟前
MySQL的分组函数
数据库·mysql
荒川之神27 分钟前
ORACLE 闪回技术简介
数据库·oracle
时差9532 小时前
【面试题】Hive 查询:如何查找用户连续三天登录的记录
大数据·数据库·hive·sql·面试·database
让学习成为一种生活方式2 小时前
R包下载太慢安装中止的解决策略-R语言003
java·数据库·r语言
秋意钟2 小时前
MySQL日期类型选择建议
数据库·mysql
Dxy12393102163 小时前
python下载pdf
数据库·python·pdf
ac-er88883 小时前
MySQL如何实现PHP输入安全
mysql·安全·php
桀桀桀桀桀桀4 小时前
数据库中的用户管理和权限管理
数据库·mysql
superman超哥5 小时前
04 深入 Oracle 并发世界:MVCC、锁、闩锁、事务隔离与并发性能优化的探索
数据库·oracle·性能优化·dba