用java和MySQL实现action层的黑名单增删改查功能
时间: 2023-12-06 19:44:33 浏览: 130
java mysql实现增删改查
首先,你需要先创建一个名为“blacklist”的MySQL表,其中包含以下字段:
- id (INT, 主键)
- name (VARCHAR(50), 姓名)
- phone (VARCHAR(20), 手机号码)
- reason (VARCHAR(200), 拉黑原因)
接下来,你需要在Java中创建一个名为“Blacklist”的类,该类包含以下属性:
```java
public class Blacklist {
private int id;
private String name;
private String phone;
private String reason;
// getter和setter方法
}
```
接下来,你可以创建一个名为“BlacklistDao”的类来执行对数据库的相关操作。这个类应该实现以下方法:
- add(Blacklist blacklist):添加一条黑名单记录到数据库中
- delete(int id):从数据库中删除一条指定id的黑名单记录
- update(Blacklist blacklist):更新数据库中的一条黑名单记录
- findById(int id):通过id查找一条黑名单记录
```java
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;
public class BlacklistDao {
Connection conn = null;
PreparedStatement ps = null;
ResultSet rs = null;
// 添加一条黑名单记录到数据库中
public void add(Blacklist blacklist) throws SQLException {
conn = JdbcUtil.getConnection();
String sql = "INSERT INTO blacklist(name, phone, reason) VALUES(?, ?, ?)";
ps = conn.prepareStatement(sql);
ps.setString(1, blacklist.getName());
ps.setString(2, blacklist.getPhone());
ps.setString(3, blacklist.getReason());
ps.executeUpdate();
JdbcUtil.close(conn, ps, rs);
}
// 从数据库中删除一条指定id的黑名单记录
public void delete(int id) throws SQLException {
conn = JdbcUtil.getConnection();
String sql = "DELETE FROM blacklist WHERE id = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
ps.executeUpdate();
JdbcUtil.close(conn, ps, rs);
}
// 更新数据库中的一条黑名单记录
public void update(Blacklist blacklist) throws SQLException {
conn = JdbcUtil.getConnection();
String sql = "UPDATE blacklist SET name = ?, phone = ?, reason = ? WHERE id = ?";
ps = conn.prepareStatement(sql);
ps.setString(1, blacklist.getName());
ps.setString(2, blacklist.getPhone());
ps.setString(3, blacklist.getReason());
ps.setInt(4, blacklist.getId());
ps.executeUpdate();
JdbcUtil.close(conn, ps, rs);
}
// 通过id查找一条黑名单记录
public Blacklist findById(int id) throws SQLException {
conn = JdbcUtil.getConnection();
String sql = "SELECT * FROM blacklist WHERE id = ?";
ps = conn.prepareStatement(sql);
ps.setInt(1, id);
rs = ps.executeQuery();
Blacklist blacklist = null;
while (rs.next()) {
blacklist = new Blacklist();
blacklist.setId(rs.getInt("id"));
blacklist.setName(rs.getString("name"));
blacklist.setPhone(rs.getString("phone"));
blacklist.setReason(rs.getString("reason"));
}
JdbcUtil.close(conn, ps, rs);
return blacklist;
}
// 查询所有黑名单记录
public List<Blacklist> findAll() throws SQLException {
conn = JdbcUtil.getConnection();
String sql = "SELECT * FROM blacklist";
ps = conn.prepareStatement(sql);
rs = ps.executeQuery();
List<Blacklist> list = new ArrayList<>();
while (rs.next()) {
Blacklist blacklist = new Blacklist();
blacklist.setId(rs.getInt("id"));
blacklist.setName(rs.getString("name"));
blacklist.setPhone(rs.getString("phone"));
blacklist.setReason(rs.getString("reason"));
list.add(blacklist);
}
JdbcUtil.close(conn, ps, rs);
return list;
}
}
```
最后,你可以在你的Action层中调用上述方法执行相应的操作。例如:
```java
public class BlacklistAction {
// 添加一条黑名单记录
public void add(Blacklist blacklist) throws SQLException {
BlacklistDao dao = new BlacklistDao();
dao.add(blacklist);
}
// 删除一条黑名单记录
public void delete(int id) throws SQLException {
BlacklistDao dao = new BlacklistDao();
dao.delete(id);
}
// 更新一条黑名单记录
public void update(Blacklist blacklist) throws SQLException {
BlacklistDao dao = new BlacklistDao();
dao.update(blacklist);
}
// 查找一条黑名单记录
public Blacklist findById(int id) throws SQLException {
BlacklistDao dao = new BlacklistDao();
return dao.findById(id);
}
// 查询所有黑名单记录
public List<Blacklist> findAll() throws SQLException {
BlacklistDao dao = new BlacklistDao();
return dao.findAll();
}
}
```
阅读全文