weixin_40957379
weixin_40957379
2017-11-09 01:03

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

  • 数据
  • java
  • servlet
  • mysql
  • jdbc

这个是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条回答