使用JDBC技术实现数据查询功能。 要求: (1)创建t_users表,字段为id、name、password、email、birthday; (2)创建User类,属性与数据库的t_users表中字段一一对应,用于封装数据; (3)使用JDBC相关接口和类实现数据库中数据的查询; (4)创建Servlet程序用于接收JSP页面请求,并获取数据库中的符合查询条件的t_users表中的数据; (5)创建JSP页面,名称可自定义,用于显示查询结果; (6)创建数据库工具类,实现数据库连接等的获取与释放
时间: 2024-01-21 20:16:36 浏览: 79
使用 JDBC 查询数据库
好的,下面是具体实现步骤:
1. 创建t_users表,字段为id、name、password、email、birthday:
```sql
CREATE TABLE t_users (
id INT PRIMARY KEY AUTO_INCREMENT,
name VARCHAR(50),
password VARCHAR(50),
email VARCHAR(50),
birthday DATE
);
```
2. 创建User类,属性与数据库的t_users表中字段一一对应,用于封装数据:
```java
public class User {
private int id;
private String name;
private String password;
private String email;
private Date birthday;
// 省略getter和setter方法
}
```
3. 使用JDBC相关接口和类实现数据库中数据的查询:
```java
public List<User> queryUsers(String name, String email) {
Connection conn = null;
PreparedStatement stmt = null;
ResultSet rs = null;
List<User> userList = new ArrayList<>();
try {
conn = DBUtil.getConnection();
String sql = "SELECT * FROM t_users WHERE 1 = 1 ";
if (name != null && !name.equals("")) {
sql += "AND name LIKE ?";
}
if (email != null && !email.equals("")) {
sql += "AND email LIKE ?";
}
stmt = conn.prepareStatement(sql);
int index = 1;
if (name != null && !name.equals("")) {
stmt.setString(index++, "%" + name + "%");
}
if (email != null && !email.equals("")) {
stmt.setString(index++, "%" + email + "%");
}
rs = stmt.executeQuery();
while (rs.next()) {
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
user.setEmail(rs.getString("email"));
user.setBirthday(rs.getDate("birthday"));
userList.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DBUtil.close(conn, stmt, rs);
}
return userList;
}
```
4. 创建Servlet程序用于接收JSP页面请求,并获取数据库中的符合查询条件的t_users表中的数据:
```java
public class UserServlet extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
String name = req.getParameter("name");
String email = req.getParameter("email");
UserService userService = new UserService();
List<User> userList = userService.queryUsers(name, email);
req.setAttribute("userList", userList);
req.getRequestDispatcher("/user.jsp").forward(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doGet(req, resp);
}
}
```
5. 创建JSP页面,用于显示查询结果:
```html
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>用户列表</title>
</head>
<body>
<h1>用户列表</h1>
<table border="1">
<tr>
<th>ID</th>
<th>用户名</th>
<th>密码</th>
<th>邮箱</th>
<th>生日</th>
</tr>
<c:forEach items="${userList}" var="user">
<tr>
<td>${user.id}</td>
<td>${user.name}</td>
<td>${user.password}</td>
<td>${user.email}</td>
<td>${user.birthday}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
```
6. 创建数据库工具类,实现数据库连接等的获取与释放:
```java
public class DBUtil {
private static final String URL = "jdbc:mysql://localhost:3306/test";
private static final String USERNAME = "root";
private static final String PASSWORD = "123456";
static {
try {
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
public static Connection getConnection() throws SQLException {
return DriverManager.getConnection(URL, USERNAME, PASSWORD);
}
public static void close(Connection conn, Statement stmt, ResultSet rs) {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
以上就是使用JDBC技术实现数据查询功能的实现步骤。
阅读全文