码迷,mamicode.com
首页 > Web开发 > 详细

JSP显示新闻

时间:2020-06-15 12:14:05      阅读:80      评论:0      收藏:0      [点我收藏+]

标签:java   nbsp   tab   import   isp   name   resultset   jdbc   页面   

创建数据库

在数据库中创建用户表,并插入数据

技术图片

创建新闻表并插入数据

 

 技术图片

编写表所对应的实体类

 

 编写News类

package Entity;

public class News {
    private String title;
    private String content;
    private String author;
    private String date;

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getContent() {
        return content;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getDate() {
        return date;
    }

    public void setDate(String date) {
        this.date = date;
    }

    @Override
    public String toString() {
        return "News{" +
                "title=‘" + title + ‘\‘‘ +
                ", content=‘" + content + ‘\‘‘ +
                ", author=‘" + author + ‘\‘‘ +
                ", date=‘" + date + ‘\‘‘ +
                ‘}‘;
    }

    public News() {
    }

    public News(String title, String content, String author, String date) {
        this.title = title;
        this.content = content;
        this.author = author;
        this.date = date;
    }
}

 

编写数据访问层的类

package Dal;

import com.mysql.jdbc.Connection;
import com.mysql.jdbc.StatementImpl;

import javax.print.attribute.HashPrintRequestAttributeSet;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;

public class SqlHelper {
    static {
        try {
            Class.forName("com.mysql.jdbc.Driver");
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
    public static Connection getConnection() throws SQLException {
        return (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/new_database", "root", "root");
    }

    public static ResultSet executeQuery(String sql)
    {
        try {
            Connection  conn = getConnection();
            ResultSet rs = conn.createStatement().executeQuery(sql);
            return rs;
        } catch (SQLException e) {
            e.printStackTrace();
            return null;
        }
    }
    public static void close(Connection conn) {

        if (conn != null) {
            try {
                conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
}

 

编写提供查询服务的类

编写查询News的类

package Service;

import Dal.SqlHelper;
import Entity.News;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class newsService {

    public List<News> QueryNews() throws SQLException{
        String sql="Select * from News";
        ResultSet rs;
        rs= SqlHelper.executeQuery(sql);
        List<News> lstNews=new ArrayList<News>();
        while (rs.next()){
            News news=new News();
            news.setTitle(rs.getString("title"));
            news.setContent(rs.getString("content"));
            news.setAuthor(rs.getString("author"));
            news.setDate(rs.getString("date"));
            lstNews.add(news);
        }
        return lstNews;
    }

    public newsService() {
    }
}

编写控制信息显示的Servlet

package Controller;

import Entity.News;
import Service.newsService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.swing.plaf.synth.SynthEditorPaneUI;
import java.io.IOException;
import java.sql.SQLException;
import java.util.List;

@WebServlet( "/Controller.ShowNewsServlet")
public class ShowNewsServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        newsService newsService=new newsService();
        try {
            List<News> lsNews=newsService.QueryNews();
            request.setAttribute("lsNews",lsNews);
            request.getRequestDispatcher("showNews.jsp").forward(request,response);
            for(int i=0;i< lsNews.size();i++)
            {
                System.out.println(lsNews.get(i).toString());
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}

编写新闻展示页面

<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>新闻列表</title>
</head>
<body>
<span>新闻列表</span>
<div>
<table>
<tr >
<td width="10%">标题</td>
<td width="20%">内容</td>
<td width="10%">作者</td>
<td width="20%">时间</td>
</tr>
<c:forEach var="news" items="${lsNews}" >
<tr>
<td width="10%">${news.title} </td>
<td width="20%">${news.content}</td>
<td width="10%">${news.author}</td>
<td width="20%">${news.date}</td>
</tr>
</c:forEach>
</table>
</div>
</body>
</html>

 界面没有美化过,比较丑,还请不要介意orz

技术图片

 

 

 代码地址

https://gitee.com/ioklkiol/javaee_second_job/tree/master/JSP显示新闻



 

 

 

 



JSP显示新闻

标签:java   nbsp   tab   import   isp   name   resultset   jdbc   页面   

原文地址:https://www.cnblogs.com/qinhesheng/p/13126891.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!