---
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>