JSP显示新闻
2021-02-05 07:17
阅读:489
YPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
标签: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 ListQueryNews() throws SQLException{ String sql="Select * from News"; ResultSet rs; rs= SqlHelper.executeQuery(sql); List lstNews=new ArrayList (); 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 { ListlsNews=newsService.QueryNews(); request.setAttribute("lsNews",lsNews); request.getRequestDispatcher("showNews.jsp").forward(request,response); for(int i=0;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); } }
编写新闻展示页面
新闻列表
新闻列表
标题 内容 作者 时间
${news.title} ${news.content} ${news.author} ${news.date}
界面没有美化过,比较丑,还请不要介意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
下一篇:js 检查对象是否没有字段
评论
亲,登录后才可以留言!