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 你页面贴一下 看你怎么解析的

    评论

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?