---第一步 创件实体类 package com.Bookinfo.web.util; public class Book { private int id; private String name; private String author; private String press; private String price; public Book(int id, String name, String author, String press, String price) { super(); this.id = id; this.name = name; this.author = author; this.press = press; this.price = price; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getPress() { return press; } public void setPress(String press) { this.press = press; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } }
--第二部 创建接口 package com.Bookinfo.BFace.util; import java.util.List; import com.Bookinfo.web.util.Book; //接口 返回List集合 public interface BookInter { public List getBook(); }
---第三布 继承(扩展它) package com.Bookinfo.BImple.util; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSet; import java.sql.Statement; import java.util.ArrayList; import java.util.List; import com.Bookinfo.BFace.util.BookInter; import com.Bookinfo.web.util.Book; public class Bimple implements BookInter { public Connection conn=null; public Statement stm=null; public ResultSet rs=null; public List getBook() { // TODO Auto-generated method stub List books=new ArrayList(); try { Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver"); conn=DriverManager.getConnection("jdbc:sqlserver://localhost:1433;database=demodb_info","sa","svse"); stm=conn.createStatement(); rs=stm.executeQuery("select * from [dbo].[t_book]"); while(rs.next()){ int id=rs.getInt(1); String name=rs.getString(2); String author=rs.getString(3); String press=rs.getString(4); String price=rs.getString(5); Book b=new Book(id,name,author,press,price); books.add(b); } } catch (Exception e) { // TODO: handle exception e.printStackTrace(); } return books; } }
---第四部 写servlet package com.Bookinfo.Servlet.util; import java.io.IOException; import javax.servlet.ServletConfig; import javax.servlet.ServletException; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; import com.Bookinfo.BFace.util.BookInter; import com.Bookinfo.BImple.util.Bimple; public class GetServlet extends HttpServlet { //得到 在web里面 得到的值 private ServletConfig config; public void doGet(HttpServletRequest request,HttpServletResponse response)throws ServletException,IOException{ //自己 会在java里面 吧“国资”两个字打印出来 config=this.getServletConfig(); String school=config.getInitParameter("school"); System.out.println(school); response.setContentType("text/html;charset=UTF-8"); HttpSession sess=request.getSession(); BookInter bt=new Bimple(); sess.setAttribute("book",bt.getBook()); request.getRequestDispatcher("BookTable.jsp").forward(request, response); } }
---sql 文件 USE [demodb] GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO SET ANSI_PADDING ON GO CREATE TABLE [dbo].[t_user]( [name] [varchar](20) NOT NULL, [password] [varchar](20) NULL, [email] [varchar](30) NULL, [money_] [money] NULL, CONSTRAINT [PK_t_user] PRIMARY KEY CLUSTERED ( [name] ASC )WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY] ) ON [PRIMARY] GO SET ANSI_PADDING OFF GO
----index 文件里面可以写一句话 <a href="bookTable"> 查看所有图书信息 </a>
--- web.xml <servlet> <servlet-name>booktable</servlet-name> <servlet-class>com.Bookinfo.Servlet.util.GetServlet</servlet-class> <init-param> <param-name>school</param-name> <param-value>国资</param-value> </init-param> </servlet> <servlet-mapping> <servlet-name>booktable</servlet-name> <url-pattern>/bookTable</url-pattern> </servlet-mapping>
-- bookinfo 文件 <body> This is my table page. <br> <hr/> <center> <table> <tr> <td>图书Id</td> <td>图书名称</td> <td>图书作者</td> <td>图书出版社</td> <td>图书价格</td> </tr> <% List<Book> listBook=(ArrayList)session.getAttribute("book"); for(int i=0;i<listBook.size();i++){ Book b=listBook.get(i); %> <tr> <td><%=b.getId() %></td> <td><%=b.getName() %></td> <td><%=b.getAuthor() %></td> <td><%=b.getPress() %></td> <td><%=b.getPrice() %></td> </tr> <% } %> </table> </center> </body>