考试要用,写一个。
要求:用dbutils查询用户列表,在jsp页面用c:foreach进行显示
servlet
package com.ssj.web;
import com.ssj.domain.User;
import com.ssj.utils.JDBCUtils;
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.sql.SQLException;
import java.util.List;
@WebServlet("/findAllServlet")
public class FindAllServlet extends HttpServlet {
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
QueryRunner qr = new QueryRunner(JDBCUtils.getDataSource());
String sql = "select * from user";
List<User> list = qr.query(sql,new BeanListHandler<User>(User.class));
request.setAttribute("users",list);
request.getRequestDispatcher("/user_list.jsp").forward(request,response);
} catch (SQLException ex) {
System.out.println(ex);
throw new RuntimeException("");
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
this.doPost(request,response);
}
}
User类
package com.ssj.domain;
public class User {
private int id;
private String name;
private int age;
private String gender;
private String address;
private String qq;
private String email;
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 int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public String getQq() {
return qq;
}
public void setQq(String qq) {
this.qq = qq;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
", gender='" + gender + '\'' +
", address='" + address + '\'' +
", qq='" + qq + '\'' +
", email='" + email + '\'' +
'}';
}
}
druid数据库连接池—druid.properties
这里注意要把druid放到最外层包
driverClassName=com.mysql.cj.jdbc.Driver
url=jdbc:mysql://localhost:3306/stduent?characterEncoding=utf8&useSSL=false&serverTimezone=GMT&allowPublicKeyRetrieval=true
username=root
password=root
filters=stat
initialSize=2
maxActive=300
maxWait=60000
timeBetweenEvictionRunsMillis=60000
validationQuery=SELECT 1
testWhileIdle=true
testOnBorrow=false
testOnReturn=false
poolPreparedStatements=false
maxPoolPreparedStatementPerConnectionSize=200
工具类
package com.ssj.utils;
import com.alibaba.druid.pool.DruidDataSourceFactory;
import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Properties;
/**
* 数据库工具类
*/
public class JDBCUtils {
private static DataSource ds;
/**
* 静态代码块
* 加载druid,.properties进入内存
* 获取资源对象
*/
static {
Properties pro = new Properties();
InputStream is = JDBCUtils.class.getClassLoader().getResourceAsStream("druid.properties");
try {
pro.load(is);
ds = DruidDataSourceFactory.createDataSource(pro);
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* 返回资源对象
* @return
*/
public static DataSource getDataSource(){return ds;}
/**
* 返回连接对象
* @return
* @throws SQLException
*/
public static Connection getConnection() throws SQLException {return ds.getConnection();}
}
index.jsp
<%--
Created by IntelliJ IDEA.
User: ssj
Date: 2020/11/24
Time: 20:27
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>$Title$</title>
</head>
<body>
<div>
<a href="/findAllServlet">查询用户列表</a>
</div>
</body>
</html>
user_list.jsp
<%--
Created by IntelliJ IDEA.
User: ssj
Date: 2020/11/26
Time: 14:09
To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<html>
<head>
<title>用户列表</title>
</head>
<body>
<table>
<thead>
<tr>
<th>编号</th>
<th>姓名</th>
<th>年龄</th>
<th>性别</th>
<th>地址</th>
<th>qq</th>
<th>email</th>
</tr>
</thead>
<tbody>
<c:forEach items="${users}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.age}</td>
<td>${user.gender}</td>
<td>${user.address}</td>
<td>${user.qq}</td>
<td>${user.email}</td>
</tr>
</c:forEach>
</tbody>
</table>
</body>
</html>
要导入的jar包有(当然你也可以用Maven导入)
可以在这个网址找jar包:https://mvnrepository.com/