JavaWeb.05.新闻系统功能制作

目录

建表语句

主界面 index.jsp

新闻添加界面 add.jsp 

新闻添加处理页面 doAdd.jsp

新闻修改界面 upd.jsp 

新闻修改处理界面 doUpd.jsp

新闻阅读界面 read.jsp

新闻删除处理界面 doDel.js

建表语句

create table t1_topic(--新闻话题表
    topic_id   number primary key,
    topic_name varchar2(20) not null
);
 
create table t1_news(--新闻表
    news_id        number primary key,
    news_title     varchar2(255) not null,
    news_topic     number        not null,
    news_author    varchar2(255) not null,
    news_publisher varchar2(255) not null,
    news_content   long          not null,
    news_cover     varchar2(255)
);
 
create table t1_user(--用户表
     t_id        number primary key,
     t_name   varchar2(20)  not null,
     t_ped      varchar2(20)  not null,
);
 
create table t_comment(--评论表
    comment_id        number primary key,
    comment_from      number        not null,
    comment_publisher varchar2(20)  not null,
    comment_author    varchar2(20)  not null,
    comment_content   varchar2(255) not null
);

主界面 index.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.Connection" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>bootstrap</title>
    <meta content="width=device-width, initial-scale=1" name="viewport">
    <!--${pageContext.request.contextPath}取项目名称-->
    <link href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css" rel="stylesheet">
    <script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
    <script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
    <style>
        * {
            outline: none !important;
        }
        body,
        html {
            background: #7f8d90;
        }
        nav,
        .breadcrumb {
            border-radius: 0 !important;
            margin-bottom: 0 !important;
        }
 
        .breadcrumb {
            margin-bottom: 20px !important;
            background: #36485c;
            color: white;
        }
        input,
        select,
        textarea,
        .panel-heading {
            border: none !important;
            border-radius: 0 !important;
        }
        .breadcrumb .active {
            color: yellow;
        }
    </style>
</head>
<body>
<nav class="navbar navbar-default hidden-sm hidden-xs">
    <div class="container-fluid">
        <div class="navbar-header">
            <a class="navbar-brand" href="${pageContext.request.contextPath}/index.jsp"
               style="font-size: 25px;">🐖</a>
        </div>
        <ul class="nav navbar-nav">
            <li class="dropdown">
                <a class="dropdown-toggle" data-toggle="dropdown">
                    新闻管理
                    <span class="caret"></span>
                </a>
                <ul class="dropdown-menu">
                    <li><a href="#">新闻发布</a></li>
                    <li class="divider"></li>
                    <li><a href="#">类别管理</a></li>
                </ul>
            </li>
        </ul>
        <ul class="nav navbar-nav navbar-right">
            <li><a>245@qq.com</a></li>
            <li><a href="#">退出<span class="glyphicon glyphicon-off"></span></a></li>
        </ul>
    </div>
</nav>
<ol class="breadcrumb">
    <li>您当前的位置是</li>
    <li>新闻发布系统</li>
    <li class="active">新闻发布</li>
</ol>
<form action="doAdd.jsp" class="container" method="post">
    <div class="panel panel-info">
        <div class="panel-heading">新闻标题</div>
        <input class="form-control" name="title" maxlength="50" placeholder="标题控制在30个字之内哦~~~" required>
        <div class="panel-heading">新闻类别</div>
        <select class=" form-control" name="topic">
            <%
                //加载驱动
                Class.forName("oracle.jdbc.driver.OracleDriver");
                //定义连接字符串
                String url = "jdbc:oracle:thin:@localhost:1521:orcl";
                //获得连接
                Connection con = DriverManager.getConnection(url, "scott", "123");
                //查询所有的新闻数据
                PreparedStatement ps = con.prepareStatement("select * from t1_topic");
                //得到结果集
                ResultSet rs = ps.executeQuery();
                //结果集中有很多数据
                while (rs.next()) {
            %>
            <!-- 下拉框选项说数据库t1_topic表格中的内容,commit!-->
            <option value="<%=rs.getInt(1)%>"><%=rs.getString(2)%></option>
            <%
                }
            %>
        </select>
        <div class="panel-heading">新闻作者</div>
        <input class="form-control" name="author" maxlength="10" placeholder="名字控制在10个字之内哦~~~" required>
        <div class="panel-heading">发布时间</div>
        <input class="form-control" name="publisher" required type="date">
        <div class="panel-heading">新闻内容</div>
        <textarea class="form-control" name="content" placeholder="🙅‍达咩~~~~这是必填的" required rows="10"></textarea>
        <div class="panel-footer">
            <button class="btn btn-primary">增加</button>
            <button class="btn btn-danger">取消</button>
        </div>
    </div>
</form>
</body>
</html>

新闻添加界面 add.jsp 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.Connection" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>bootstrap</title>
    <meta content="width=device-width, initial-scale=1" name="viewport">
    <!--${pageContext.request.contextPath}取项目名称-->
    <link href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css" rel="stylesheet">
    <script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
    <script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
    <style>
        * {
            outline: none !important;
        }
        body,
        html {
            background: #7f8d90;
        }
        nav,
        .breadcrumb {
            border-radius: 0 !important;
            margin-bottom: 0 !important;
        }
 
        .breadcrumb {
            margin-bottom: 20px !important;
            background: #36485c;
            color: white;
        }
        input,
        select,
        textarea,
        .panel-heading {
            border: none !important;
            border-radius: 0 !important;
        }
        .breadcrumb .active {
            color: yellow;
        }
    </style>
</head>
<body>
<nav class="navbar navbar-default hidden-sm hidden-xs">
    <div class="container-fluid">
        <div class="navbar-header">
            <a class="navbar-brand" href="${pageContext.request.contextPath}/index.jsp"
               style="font-size: 25px;">🐖</a>
        </div>
        <ul class="nav navbar-nav">
            <li class="dropdown">
                <a class="dropdown-toggle" data-toggle="dropdown">
                    新闻管理
                    <span class="caret"></span>
                </a>
                <ul class="dropdown-menu">
                    <li><a href="#">新闻发布</a></li>
                    <li class="divider"></li>
                    <li><a href="#">类别管理</a></li>
                </ul>
            </li>
        </ul>
        <ul class="nav navbar-nav navbar-right">
            <li><a>245@qq.com</a></li>
            <li><a href="#">退出<span class="glyphicon glyphicon-off"></span></a></li>
        </ul>
    </div>
</nav>
<ol class="breadcrumb">
    <li>您当前的位置是</li>
    <li>新闻发布系统</li>
    <li class="active">新闻发布</li>
</ol>
<form action="doAdd.jsp" class="container" method="post">
    <div class="panel panel-info">
        <div class="panel-heading">新闻标题</div>
        <input class="form-control" name="title" maxlength="50" placeholder="标题控制在30个字之内哦~~~" required>
        <div class="panel-heading">新闻类别</div>
        <select class=" form-control" name="topic">
            <%
                //加载驱动
                Class.forName("oracle.jdbc.driver.OracleDriver");
                //定义连接字符串
                String url = "jdbc:oracle:thin:@localhost:1521:orcl";
                //获得连接
                Connection con = DriverManager.getConnection(url, "scott", "123");
                //查询所有的新闻数据
                PreparedStatement ps = con.prepareStatement("select * from t1_topic");
                //得到结果集
                ResultSet rs = ps.executeQuery();
                //结果集中有很多数据
                while (rs.next()) {
            %>
            <!-- 下拉框选项说数据库t1_topic表格中的内容,commit!-->
            <option value="<%=rs.getInt(1)%>"><%=rs.getString(2)%></option>
            <%
                }
            %>
        </select>
        <div class="panel-heading">新闻作者</div>
        <input class="form-control" name="author" maxlength="10" placeholder="名字控制在10个字之内哦~~~" required>
        <div class="panel-heading">发布时间</div>
        <input class="form-control" name="publisher" required type="date">
        <div class="panel-heading">新闻内容</div>
        <textarea class="form-control" name="content" placeholder="🙅‍达咩~~~~这是必填的" required rows="10"></textarea>
        <div class="panel-footer">
            <button class="btn btn-primary">增加</button>
            <button class="btn btn-danger">取消</button>
        </div>
    </div>
</form>
</body>
</html>

新闻添加处理页面 doAdd.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.nio.charset.StandardCharsets" %>
<%
    try {
        //接收新闻的数据
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String publisher = request.getParameter("publisher");
        String topic = request.getParameter("topic");
        String content = request.getParameter("content");
        //【新闻的添加(连接数据库)】
        //加载驱动
        Class.forName("oracle.jdbc.driver.OracleDriver");
        //定义连接字符串
        String url = "jdbc:oracle:thin:@localhost:1521:orcl";
        //获得连接
        Connection con = DriverManager.getConnection(url, "scott", "123");
        // 主键不能不填
        // 主键没有自增的选项(触发器+序列)
        //获得执行对象【数据插入之前,先把主键查询出来】
        PreparedStatement ps = con.prepareStatement("select nvl(max(NEWS_ID),0) from t1_news");
        ResultSet rs = ps.executeQuery();
        int id = 0; //定义保存主键的变量
        if (rs.next()) {
            id = rs.getInt(1);//查询出来的最大id,加一:【避免主键的重复】
        }
		id++;
		//System.out.println(id);
        //插入新闻的操作
        ps = con.prepareStatement("insert into t1_news(news_id, news_title, news_topic, news_author, news_publisher, news_content) VALUES (?,?,?,?,?,?)");
        //赋值
        ps.setInt(1, id);
        ps.setString(2, title);
        ps.setInt(3, Integer.parseInt(topic));
        ps.setString(4, author);
        ps.setString(5, publisher);
        ps.setString(6, content);
        System.out.println(id);
        //执行结果
        int n = ps.executeUpdate();
        System.out.println(id);
        if (n > 0) {
            out.print("<script>alert('增加成功');location.href='index.jsp'</script>");
        } else {
            out.print("<script>alert('增加失败');location.href='index.jsp'</script>");
        }      
		//关闭连接
        if (!con.isClosed()) {
            con.close();
        }
        ps.close();
        rs.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
%>

新闻修改界面 upd.jsp 

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.Connection" %>
<!DOCTYPE html>
<html lang="zh">
<head>
    <meta charset="UTF-8">
    <title>bootstrap</title>
    <meta content="width=device-width, initial-scale=1" name="viewport">
    <link href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css" rel="stylesheet">
    <script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
    <script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
    <style>
        * {
            outline: none !important;
        }
 
        body,
        html {
            background: #7f8d90;
        }
        nav,
        .breadcrumb {
            border-radius: 0 !important;
            margin-bottom: 0 !important;
        }
        .breadcrumb {
            margin-bottom: 20px !important;
            background: #36485c;
            color: white;
        }
        input,
        select,
        textarea,
        .panel-heading {
            border: none !important;
            border-radius: 0 !important;
        }
        .breadcrumb .active {
            color: yellow;
        }
    </style>
</head>
<body>
<nav class="navbar navbar-default hidden-sm hidden-xs">
    <div class="container-fluid">
        <div class="navbar-header">
            <a class="navbar-brand" href="${pageContext.request.contextPath}/news/index.jsp"
               style="font-size: 25px;">🐖</a>
        </div>
        <ul class="nav navbar-nav">
            <li class="dropdown">
                <a class="dropdown-toggle" data-toggle="dropdown">
                    新闻管理
                    <span class="caret"></span>
                </a>
                <ul class="dropdown-menu">
                    <li><a href="#">新闻发布</a></li>
                    <li class="divider"></li>
                    <li><a href="#">类别管理</a></li>
                </ul>
            </li>
        </ul>
        <ul class="nav navbar-nav navbar-right">
            <li><a>245@qq.com</a></li>
            <li><a href="#">退出<span class="glyphicon glyphicon-off"></span></a></li>
        </ul>
    </div>
</nav>
 
<ol class="breadcrumb">
    <li>您当前的位置是</li>
    <li>新闻发布系统</li>
    <li class="active">新闻修改</li>
</ol>
<%
    // http://localhost:8080/web04/news/upd.jsp?newId=4
    //从read界面获得新闻的id,根据id去数据库做查询操作
    String newId = request.getParameter("newId");
	//加载驱动
    Class.forName("oracle.jdbc.driver.OracleDriver");
    //定义连接字符串
    String url = "jdbc:oracle:thin:@localhost:1521:orcl";
    //获得连接
    Connection con = DriverManager.getConnection(url, "scott", "123");
    //查询所有的新闻数据
    PreparedStatement ps = con.prepareStatement("select * from t1_news where news_id=?");
    //占位符的设置
    ps.setInt(1,Integer.parseInt(newId));
    //得到结果集
    ResultSet rs = ps.executeQuery();
    //定义需要的值
    String title="";
    int topic=0;
    String author="";
    String publisher="";
    String content="";
    if(rs.next()){
        //可以取值
        title=rs.getString(2);
        publisher=rs.getString(5);
        author=rs.getString(4);
        content=rs.getString(6);
        //topic是t1_news表中的第三列news_topic!!!
        topic=rs.getInt(3);
    }
%>
<!-- 修改界面内容的数据了过大,所以要用post,get有长度限制 -->
<form action="doUpd.jsp" class="container" method="post">
    <div class="panel panel-info">
    	<!--将带id的框隐藏type=隐藏框hidden,只用于传给doUpd做修改,不在页面显示,将输入框的值改为数据库的值=newId-->
        <input type="hidden" value="<%=newId%>" name="newId">
        <div class="panel-heading">新闻标题</div>
        <input value="<%=title%>" class="form-control" name="title" maxlength="50" placeholder="标题控制在30个字之内哦~~~" required>
        <div class="panel-heading">新闻类别</div>
        <select class=" form-control" name="topic">
            <%
                //查询对应的类目的信息,直接用连接了数据库后的ps,rs
                ps=con.prepareStatement("select * from T1_TOPIC");
                rs=ps.executeQuery();
                while(rs.next()){
            %>
            <!--每查出一条数据就生成一个下拉框 ? :三元运算符-->
            <!-- topic==rs.getInt(1)如果数据库中话题列和 -->
            <!--topic是t1_news表中的第三列news_topic!!!-->
  <option <%= topic==rs.getInt(1) ? "selected" : ""%> value="<%=rs.getInt(1)%>">
  					<!-- 下拉框内容 -->
                    <%=rs.getString(2)%>
            </option>
            <%
                }
            %>
        </select>
        <div class="panel-heading">新闻作者</div>
        <input value="<%=author%>" class="form-control" name="author" maxlength="10" placeholder="名字控制在10个字之内哦~~~" required>
        <!-- 发布时间不能修改 -->
        <div class="panel-heading">发布时间</div>
        <input value="<%=publisher%>" class="form-control" name="publisher" required type="date">
        <div class="panel-heading">新闻内容</div>
        <textarea class="form-control" name="content" placeholder="🙅‍达咩~~~~这是必填的" required rows="10">
            <%=content%>
        </textarea>
        <div class="panel-footer">
            <button class="btn btn-primary">修改</button>
            <button onclick="history.go(-1)" class="btn btn-danger">取消</button>
        </div>
    </div>
</form>
</body>
</html>

新闻修改处理界面 doUpd.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.*" %>
<%@ page import="java.nio.charset.StandardCharsets" %>
<%
    try {
        //接收新闻的数据
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String publisher = request.getParameter("publisher");
        String topic = request.getParameter("topic");
        String content = request.getParameter("content");
        //【新闻的添加(连接数据库)】
        //加载驱动
        Class.forName("oracle.jdbc.driver.OracleDriver");
        //定义连接字符串
        String url = "jdbc:oracle:thin:@localhost:1521:orcl";
        //获得连接
        Connection con = DriverManager.getConnection(url, "scott", "123");
        // 主键不能不填
        // 主键没有自增的选项(触发器+序列)
        //获得执行对象【数据插入之前,先把主键查询出来】
        PreparedStatement ps = con.prepareStatement("select nvl(max(NEWS_ID),0) from t1_news");
        ResultSet rs = ps.executeQuery();
        int id = 0; //定义保存主键的变量
        if (rs.next()) {
            id = rs.getInt(1);//查询出来的最大id,加一:【避免主键的重复】
        }
		id++;
		//System.out.println(id);
        //插入新闻的操作
        ps = con.prepareStatement("insert into t1_news(news_id, news_title, news_topic, news_author, news_publisher, news_content) VALUES (?,?,?,?,?,?)");
        //赋值
        ps.setInt(1, id);
        ps.setString(2, title);
        ps.setInt(3, Integer.parseInt(topic));
        ps.setString(4, author);
        ps.setString(5, publisher);
        ps.setString(6, content);
        System.out.println(id);
        //执行结果
        int n = ps.executeUpdate();
        System.out.println(id);
        if (n > 0) {
            out.print("<script>alert('增加成功');location.href='index.jsp'</script>");
        } else {
            out.print("<script>alert('增加失败');location.href='index.jsp'</script>");
        }    
		//关闭连接
        if (!con.isClosed()) {
            con.close();
        }
        ps.close();
        rs.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
%>

新闻阅读界面 read.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.ResultSet" %>
<%@ page import="java.sql.Connection" %>
<!DOCTYPE html>
<html lang="zh">
<head>
<!-- read读取新闻内容的界面具有新闻修改,删除按钮 -->
    <meta charset="UTF-8">
    <title>bootstrap</title>
    <meta content="width=device-width, initial-scale=1" name="viewport">
    <link href="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/css/bootstrap.css" rel="stylesheet">
    <script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/jquery-3.5.1.js"></script>
    <script src="${pageContext.request.contextPath}/bootstrap-3.3.7-dist/js/bootstrap.js"></script>
    <style>
        * {
            outline: none !important;
        }
        body,
        html {
            background: #7f8d90;
        }
        nav,
        .breadcrumb {
            border-radius: 0 !important;
            margin-bottom: 0 !important;
        }
        .breadcrumb {
            margin-bottom: 20px !important;
            background: #36485c;
            color: white;
        }
        input,
        select,
        textarea,
        .panel-heading {
            border: none !important;
            border-radius: 0 !important;
        }
        .breadcrumb .active {
            color: yellow;
        }
    </style>
</head>
<body>
<nav class="navbar navbar-default hidden-sm hidden-xs">
    <div class="container-fluid">
        <div class="navbar-header">
            <a class="navbar-brand" href="${pageContext.request.contextPath}/index.jsp"
               style="font-size: 25px;">🐖</a>
        </div>
        <ul class="nav navbar-nav">
            <li class="dropdown">
                <a class="dropdown-toggle" data-toggle="dropdown">
                    新闻管理
                    <span class="caret"></span>
                </a>
                <ul class="dropdown-menu">
                    <li><a href="#">新闻发布</a></li>
                    <li class="divider"></li>
                    <li><a href="#">类别管理</a></li>
                </ul>
            </li>
        </ul>
        <ul class="nav navbar-nav navbar-right">
            <li><a>245@qq.com</a></li>
            <li><a href="#">退出<span class="glyphicon glyphicon-off"></span></a></li>
        </ul>
    </div>
</nav>
<ol class="breadcrumb">
    <li>您当前的位置是</li>
    <li>新闻发布系统</li>
    <li class="active">新闻阅读</li>
</ol>
<%
    //获得新闻的id,根据id去数据库做查询操作
    String newId = request.getParameter("newId");
    //加载驱动
    Class.forName("oracle.jdbc.driver.OracleDriver");
    //定义连接字符串
    String url = "jdbc:oracle:thin:@localhost:1521:orcl";
    //获得连接
    Connection con = DriverManager.getConnection(url, "scott", "123");
    //查询所有的新闻数据
    PreparedStatement ps = con.prepareStatement("select * from t1_news where news_id=?");
    //占位符的设置(页面上的值都是String类型)
    ps.setInt(1,Integer.parseInt(newId));
    //得到结果集
    ResultSet rs = ps.executeQuery();
    //定义需要的值
    String title="";
    int count=0;
    String author="";
    String publisher="";
    String content="";
    if(rs.next()){
        //可以取值
        title=rs.getString(2);
        publisher=rs.getString(5);
        author=rs.getString(4);
        content=rs.getString(6);
        //count=rs.getInt(8);
    }
%>
<div class="container" style="background: rgba(239, 231, 231, 0.9);border-radius:10px;">
    <h1><%=title%></h1>
    <h3 class="text-right">
        <small>
            <span class="glyphicon glyphicon-user"><span class="label label-default"><%=author%></span></span>
            <span class="glyphicon glyphicon-eye-open"><span class="label label-default"><%=count%></span></span>
            <span class="glyphicon glyphicon-time"><span class="label label-info"><%=publisher%></span></span>
        </small>
    </h3>
    <samp><%=content%></samp>
    <div class="btn-group btn-group-justified" style="margin-bottom: 20px;">
        <div class="btn-group">
        <!-- newId是index传过来的! -->
            <a href="${pageContext.request.contextPath}/doDel.jsp?newId=<%=newId%>" class="btn btn-danger" type="button">删除</a>
        </div>
        <div class="btn-group">
        <!--将newId也传给修改界面upd-->
            <a href="${pageContext.request.contextPath}/upd.jsp?newId=<%=newId%>" class="btn btn-info" type="button">修改</a>
        </div>
    </div>
</div>
<div class="container" style="background: rgba(239, 231, 231, 0.9);border-radius:10px;margin-top: 10px;">
    <div class="panel panel-default" style="margin-top: 20px;">
        <div class="panel-heading">
            <span class="glyphicon glyphicon-user"><span class="label label-success">东</span></span>
            <p style="margin-top: 10px;text-indent: 2em;">
                <samp>我是一条非常好看的评论.</samp>
            </p>
            <p class="text-right">
                <span class="glyphicon glyphicon-time"><span class="label label-info">2020/1/1 10:23:04</span></span>
            </p>
        </div>
    </div>
    <div class="panel panel-default" style="margin-top: 20px;">
        <div class="panel-heading">
            <span class="glyphicon glyphicon-user"><span class="label label-success">东</span></span>
            <p style="margin-top: 10px;text-indent: 2em;">
                <samp>我是一条非常好看的评论.</samp>
            </p>
            <p class="text-right">
                <span class="glyphicon glyphicon-time"><span class="label label-info">2020/1/1 10:23:04</span></span>
            </p>
        </div>
    </div>
</div>
<form class="container" style="background: rgba(239, 231, 231, 0.9);border-radius:10px;margin-top: 10px;padding: 30px;">
    <div class="form-group">
        <label for="name">Name</label>
        <input id="name" class="form-control" placeholder="用户名称" required type="text">
    </div>
    <div class="form-group">
        <label for="email">Email</label>
        <input id="email" class="form-control" placeholder="评论内容" required type="email">
    </div>
    <button class="btn btn-default" type="submit">发布评论</button>
</form>
<div style="height: 50px;"></div>
</body>
</html>

新闻删除处理界面 doDel.jsp

<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ page import="java.sql.DriverManager" %>
<%@ page import="java.sql.PreparedStatement" %>
<%@ page import="java.sql.Connection" %>
<%
//删除没有界面del,只有doDel后台,在read界面点击删除按钮就会执行这个后台
    //获得新闻的id,根据id去数据库做删除操作
    String newId = request.getParameter("newId");
    //加载驱动
    Class.forName("oracle.jdbc.driver.OracleDriver");
    //定义连接字符串
    String url = "jdbc:oracle:thin:@localhost:1521:orcl";
    //获得连接
    Connection con = DriverManager.getConnection(url, "scott", "123");
    //查询所有的新闻数据
    PreparedStatement ps = con.prepareStatement("delete from t1_news where news_id=?");
    //占位符的设置
    ps.setInt(1,Integer.parseInt(newId));
    //执行并获得结果 【收到影响的行数】
    int i = ps.executeUpdate();
    if(i>0){ //删除成功
        out.print("<script>alert('删除成功');location.href='index.jsp'</script>");
    }else{ //删除失败
    	//history.go(-1)跳到上一页即doDel
        out.print("<script>alert('删除失败');history.go(-1)</script>");
    }
%>

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值