weixin_40957379 2017-11-09 01:03 采纳率: 0%
浏览 1085

servlet+jdbc+mysql+jsp实现数据调用,不错数据也不报错

这个是dao文件
public class NewsDao {
private Integer newsid;
private String title;
private String content;
private Data publishDate;
private String author;
private Integer typeld;

public List getNewsList(){
List newsList=new ArrayList();

   Connection connection=null;
   Statement statement=null;
   ResultSet resultset=null;

   try {
     Class.forName("com.mysql.jdbc.Driver");
     connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/","root","123");
     statement=connection.createStatement();
     resultset = statement.executeQuery("select * from t_news ");
     while(resultset.next()){
         int newId = resultset.getInt("newsid");
         String newsTitle= resultset.getString("title");
         String newsContent = resultset.getString("content");
         String newsAuthor = resultset.getString("author");
         int newtypeLd= resultset.getInt("typeld");
         Date newsPublishDate = resultset.getDate("publishDate");
        News news= new News(newsid, title, content, publishDate, author, typeld);
         newsList.add(news);

     }

} catch (Exception e) {
    e.printStackTrace();
}finally {
    if(resultset!=null){
        try {
            resultset.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }if(statement!=null){
        try {
            statement.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }   
    }if(connection!=null){
        try {
            connection.close();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

    }
}



return newsList;

}

}
这个是jdbc文件
public class News {
private Integer newsid;
private String title;
private String content;
private Data publishDate;
private String author;
private Integer typeld;
public Integer getNewsid() {
return newsid;
}
public void setNewsid(Integer newsid) {
this.newsid = newsid;
}
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 Data getPublishDate() {
return publishDate;
}
public void setPublishDate(Data publishDate) {
this.publishDate = publishDate;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Integer getTypeld() {
return typeld;
}
public void setTypeld(Integer typeld) {
this.typeld = typeld;
}
public News(Integer newsid, String title, String content, Data publishDate, String author, Integer typeld) {
super();
this.newsid = newsid;
this.title = title;
this.content = content;
this.publishDate = publishDate;
this.author = author;
this.typeld = typeld;
}
public News() {
super();
}

这个是servlet文件
public class newsServlet extends HttpServlet {
private NewsDao newsDao;
@Override
public void init() throws ServletException {
    newsDao=new NewsDao();


    super.init();
}

protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    // TODO Auto-generated method stub
    doPost(request, response);


}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


    List<News>newsList=newsDao.getNewsList();
    request.setAttribute("newList", newsList);
    request.getRequestDispatcher("newsList.jsp").forward(request, response);
}

}
这个是jsp文件
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8" import="java.util.*"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">



新闻列表

新闻编号 新闻标题 新闻内容 发布时间 作者 新闻id
${news.newsid} ${news.title} ${news.content} ${news.publishDate} ${news.author} ${news.typeld}


  • 写回答

6条回答 默认 最新

  • FEN_TA 2017-11-09 01:11
    关注

    request.setAttribute("newList", newsList); 你request 里面放的是newList 你页面贴一下 看你怎么解析的

    评论

报告相同问题?

悬赏问题

  • ¥15 #MATLAB仿真#车辆换道路径规划
  • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
  • ¥15 数据可视化Python
  • ¥15 要给毕业设计添加扫码登录的功能!!有偿
  • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
  • ¥15 微信公众号自制会员卡没有收款渠道啊
  • ¥100 Jenkins自动化部署—悬赏100元
  • ¥15 关于#python#的问题:求帮写python代码
  • ¥20 MATLAB画图图形出现上下震荡的线条
  • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘