User.java
public class User {
private int bookid;
private String bookName;
public int getBookid() {
return bookid;
}
public void setBookid(int bookid) {
this.bookid = bookid;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
public float getBookPrice() {
return bookPrice;
}
public void setBookPrice(float bookPrice) {
this.bookPrice = bookPrice;
}
public String getBookAuthor() {
return bookAuthor;
}
public void setBookAuthor(String bookAuthor) {
this.bookAuthor = bookAuthor;
}
public String getBookPuhlisher() {
return bookPuhlisher;
}
public void setBookPuhlisher(String bookPuhlisher) {
this.bookPuhlisher = bookPuhlisher;
}
private float bookPrice;
private String bookAuthor;
private String bookPuhlisher;
}
Servlet01.java
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;
import java.util.List;
@WebServlet("/Servlet01")
public class Servlet01 extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//3.连接数据库,查询信息
QueryRunner qr = new QueryRunner(DruidUtils.getDataSource());
String sql = "select * from t_book";
List<User> bookList = null;
try {
bookList = qr.query(sql, new BeanListHandler<User>(User.class));
} catch (SQLException e) {
e.printStackTrace();
}
//把信息展示在页面上
response.setContentType("text/html;charset= UTF-8");
PrintWriter out = response.getWriter();
out.println("<table border=1 cellspacing=0 cellspadding=0>");
out.println("<tr>");
out.println("<td>图书编号</td>");
out.println("<td>图书名称</td>");
out.println("<td>图书价格</td>");
out.println("<td>图书作者</td>");
out.println("<td>图书出版社</td>");
out.println("</tr>");
//遍历集合
for(User book : bookList){
out.println("<tr>");
out.println("<td>"+book.getBookid()+"</td>");
out.println("<td>"+book.getBookName()+"</td>");
out.println("<td>"+book.getBookPrice()+"</td>");
out.println("<td>"+book.getBookAuthor()+"</td>");
out.println("<td>"+book.getBookPuhlisher()+"</td>");
out.println("<tr>");
}
out.println("</table>");
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request, response);
}
}
druid.properties
#连接设置
driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/db_book
username=root
password=root
#<!-- 初始化连接 -->
initialSize=10
#最大连接数量
maxActive=50
#<!-- 最小空闲连接 -->
minIdle=5
#<!-- 超时等待时间以毫秒为单位 6000毫秒/1000等于60秒 -->
maxWait=6000
web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1">
</web-app>