标签: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() { } }
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显示新闻
标签:java nbsp tab import isp name resultset jdbc 页面
原文地址:https://www.cnblogs.com/qinhesheng/p/13126891.html