On the connection has been finished and the various tools on the database layer of meaning as well as query and delete, modify on now to organize data, modified echo and increased process
1. First, we write about the process of adding
The first is this page, write on the increase in the query page hyperlinks
<%@ page language="java" pageEncoding="utf-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%> <!DOCTYPE html> <html> <head> <Title> News Table </ title> </head> <body> <a href="add.jsp"> add </a> // here hyperlink directly to the page you want to add this <table border="1"> <tr> <Td> ID </ td> <Td> Title </ td> <td>作者</td> <td>操作</td> </tr> <c:forEach items="${list}" var="a" varStatus="status"> <tr> <td>${a.newsId}</td> <td>${a.newsTitle}</td> <td>${a.newsAuthor}</td> <td><a href="DeleteServlet.do?newsId=${a.newsId}">删除</a> </td> </tr> </c:forEach> </table> </body> </html>
To write the contents of the database you want to add some of the content displayed on a page
<%@ page language="java" pageEncoding="utf-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html> <html> <head> <title>Insert title here</title> </head> <body> <H3> Welcome $ {newsTitle} </ h3> <Form action = "AddServlet.do"> // form of action in this sense has the address attribute, so here writing this address servlet processing data reception, processing to add corresponding data <input type = "hidden" name = "newsId"> // because to get the value, so the name attribute in the value of consistency with the field names in the entity class, because id in mysql database is the primary key from the growing need fill value, so hidden headlines: <the INPUT of the type = "text" name = "newsTitle"> <br> News author: <the TextArea cols = "4" rows = "5" name = "newsAuthor"> </ the TextArea> <br> <input type="submit" value="保存"> </form> </body> </html>
Then write dao layer interface
// This method is used to add, to add data to determine whether this one succeeds, the return of a number of rows affected // because the addition of a piece of data, so the argument passed is an entity class int insertNews (NewsBeng newsBeng) ;
Then the implementation class override this method
@Override public int insertNews(NewsBeng newsBeng) { // TODO Auto-generated method stub Connection conn=null; PreparedStatement prestate=null; int reslust=0; conn=BaseDao.getconn(); String sql="insert into news values (null,?,?)"; try { prestate=conn.prepareStatement(sql); prestate.setString ( 1 , newsBeng.getNewsTitle ()); // assignment sql statement to a question mark, because a pass over the entity class, so a direct call from the front desk pass over the data to the assigned question mark prestate.setString ( 2 , newsBeng.getNewsAuthor ()); reslust=prestate.executeUpdate(); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { BaseDao.close(conn, prestate, null); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return reslust; }
Servlet layer followed by a processing of the data
package com.bw.ServletPlml; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; amount com.bw.Beng.NewsBeng; amount com.bw.Dao.plml.NewsDaoPlml; /** * Servlet implementation class AddServlet */ @WebServlet("/AddServlet.do") public class AddServlet extends HttpServlet { private static final long serialVersionUID = 1L; NewsDaoPlml daoPlml=new NewsDaoPlml(); /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); request.setCharacterEncoding("utf-8"); NewsTitle String = request.getParameter ( "newsTitle" ); // This method is used to get the value of the foreground pass over the inside of the field name to be consistent with the field names of the page String newsAuthor = request.getParameter("newsAuthor"); NewsBeng NewsBeng = new NewsBeng (); // Since we have all this data is passed to the background, so direct a new entity to entity class class way backstage pass newsBeng.setNewsTitle (newsTitle); // set with assignment method newsBeng.setNewsAuthor (newsAuthor); int insertNews = daoPlml.insertNews (newsBeng); // call to add the class method implementation dao layer IF (insertNews> 0 ) { request.getRequestDispatcher ( "ShowServlet.do" ) .forward (Request, the Response); // After the addition is complete, the data indicating an increase, so to update the data, re-query } } }
Adding to end here
2. It is on Amending
Or write the query page modify hyperlinks
<%@ page language="java"
pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!DOCTYPE html>
<html>
<head>
<Title> News Table </ title>
</head>
<body>
<a href="add.jsp">添加</a>
<table border="1">
<tr>
<Td> ID </ td>
<Td> Title </ td>
<td>作者</td>
<td>操作</td>
</tr>
<c:forEach items="${list}" var="a" varStatus="status">
<tr>
<td>${a.newsId}</td>
<td>${a.newsTitle}</td>
<td>${a.newsAuthor}</td>
<td><a href="DeleteServlet.do?newsId=${a.newsId}">删除</a>
|
<a href="ShowOneServlet.do?newsId=${a.newsId}"> modify </a> // This is the modification of hyperlinks, modify, and delete only because for a data, which is written in the operation
</td>
</tr>
</c:forEach>
</table>
</body>
</html>
To look at the data before modification is to be modified, so this involves a modification Echo
dao layer interface in the method
// modify a data related to the echo, because you want to modify is to be modified reference data before, so here is based on the method you want to modify this one id data to find the row of data that you want to modify the contents of / / so here is the return of an entity class, the argument passed is an entity class, it can be id, because id just be echoing according to a query this data newsBeng selectnews (newsBeng newsBeng);
Implementation class override this method
@Override public NewsBeng selectnews(NewsBeng newsBeng) { // TODO Auto-generated method stub Connection conn=null; PreparedStatement prestate=null; NewsBeng2 NewsBeng = new NewsBeng (); // returns the entity class, the entity class corresponding to a new ResultSet res=null; conn=BaseDao.getconn(); String sql="select * from news where newsId=?"; try { prestate=conn.prepareStatement(sql); prestate.setInt ( 1 , newsBeng.getNewsId ()); // assign to a question mark RES = prestate.executeQuery (); // get the result sets the while (res.next ()) { newsBeng2.setNewsId (res.getInt ( "newsid" )); // the value of the injected entity class newsBeng2.setNewsTitle(res.getString("newsTitle")); newsBeng2.setNewsAuthor(res.getString("newsAuthor")); } } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { BaseDao.close(conn, prestate, res); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return newsBeng2; // returns the entity class }
servlet layer
package com.bw.ServletPlml; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; amount com.bw.Beng.NewsBeng; amount com.bw.Dao.plml.NewsDaoPlml; /** * Servlet implementation class ShowOneServlet */ @WebServlet("/ShowOneServlet.do") public class ShowOneServlet extends HttpServlet { private static final long serialVersionUID = 1L; NewsDaoPlml daoPlml=new NewsDaoPlml(); /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); Newsid String = request.getParameter ( "newsid" ); // get foreground pass over the value NewsBeng newsBeng = new new NewsBeng (); newsBeng.setNewsId(Integer.parseInt(newsId));//存值 NewsBeng2 NewsBeng = daoPlml.selectnews (newsBeng); // call the method by value IF (newsBeng2 =! Null ) { request.setAttribute ( "News" , newsBeng2); // get the values passed to the page request.getRequestDispatcher ( "update.jsp" ) .forward (Request, the Response); // forward to modify the page } } }
Modify page
<%@ page language="java" pageEncoding="utf-8"%> <%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %> <!DOCTYPE html> <html> <head> <title>Insert title here</title> </head> <body> <H3> Welcome $ {news.newsTitle} </ h3> <form action="updateServlet.do"> <input type = "hidden" name = "newsId" value = "$ {news.newsId}"> // value property is to show the value of the background data transmission over the headlines: <the INPUT of the type = "text" name = "newsTitle "value =" $ {news.newsTitle} "> <br> News author: <the TextArea cols =" 4 "rows =" 5 "name =" newsAuthor "> $ {news.newsAuthor} </ the TextArea> <br> <input type="submit" value="保存"> </form> </body> </html>
Here we have to get Echo
The next step is to write the modified dao layer interface method
// This method is modified, because to confirm the changes affect the number of rows returned so successful, it is of type int // modify the database to modify the row of data, not individual data, so the argument passed is an entity class int updateNews (newsBeng newsBeng);
Implementation class override this method
@Override public int updateNews(NewsBeng newsBeng) { // TODO Auto-generated method stub Connection conn=null; PreparedStatement prestate=null; int reslust=0; conn=BaseDao.getconn(); String sql="update news set newsTitle=? , newsAuthor=? where newsId=?"; try { prestate=conn.prepareStatement(sql); prestate.setString ( 1 , newsBeng.getNewsTitle ()); // assign to a question mark prestate.setString ( 2 , newsBeng.getNewsAuthor ()); prestate.setInt ( 3 , newsBeng.getNewsId ()); reslust = prestate.executeUpdate (); // returns the number of rows Effect } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); }finally { try { BaseDao.close(conn, prestate, null); } catch (SQLException e) { // TODO Auto-generated catch block e.printStackTrace(); } } return reslust; }
servlet layer
package com.bw.ServletPlml; import java.io.IOException; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; amount com.bw.Beng.NewsBeng; amount com.bw.Dao.plml.NewsDaoPlml; /** * Servlet implementation class updateServlet */ @WebServlet("/updateServlet.do") public class updateServlet extends HttpServlet { private static final long serialVersionUID = 1L; NewsDaoPlml daoPlml=new NewsDaoPlml(); /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { doPost(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { request.setCharacterEncoding("utf-8"); response.setCharacterEncoding("utf-8"); Newsid String = request.getParameter ( "newsid" ); // get the value from the front desk String newsTitle = request.getParameter("newsTitle"); String newsAuthor = request.getParameter("newsAuthor"); NewsBeng newsBeng = new NewsBeng (); newsBeng.setNewsId(Integer.parseInt(newsId)); newsBeng.setNewsTitle(newsTitle); newsBeng.setNewsAuthor (newsAuthor); int updateNews = daoPlml.updateNews(newsBeng);//传值 if (updateNews>0) { response.sendRedirect ( "ShowServlet.do" ); // This is redirected, with forwarding achieve a similar effect, here is the updated data } } }