idea Java 完成对用户表 的增删改查 用户表字段 ID username password 详细过程
时间: 2024-03-21 11:38:01 浏览: 79
ssm-crud.rar_DEMO_forestql4_java_ssm增删改查demo_框架
好的,我可以帮你解决这个问题。
首先,我们需要创建一个用户类,其中包含 ID、username 和 password 字段,并定义相应的 getter 和 setter 方法。
```java
public class User {
private int ID;
private String username;
private String password;
public int getID() {
return ID;
}
public void setID(int ID) {
this.ID = ID;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
```
接下来,我们需要创建一个用户 DAO 类,它负责处理对用户表的增删改查操作。这里我们使用 JDBC 技术来访问数据库。
```java
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class UserDao {
private String jdbcUrl;
private String username;
private String password;
private Connection connection;
public UserDao(String jdbcUrl, String username, String password) {
this.jdbcUrl = jdbcUrl;
this.username = username;
this.password = password;
}
public void connect() throws SQLException {
connection = DriverManager.getConnection(jdbcUrl, username, password);
}
public void disconnect() throws SQLException {
if (connection != null && !connection.isClosed()) {
connection.close();
}
}
public void addUser(User user) throws SQLException {
String sql = "INSERT INTO users (username, password) VALUES (?, ?)";
PreparedStatement statement = connection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
statement.setString(1, user.getUsername());
statement.setString(2, user.getPassword());
int affectedRows = statement.executeUpdate();
if (affectedRows == 0) {
throw new SQLException("Creating user failed, no rows affected.");
}
try (ResultSet generatedKeys = statement.getGeneratedKeys()) {
if (generatedKeys.next()) {
user.setID(generatedKeys.getInt(1));
} else {
throw new SQLException("Creating user failed, no ID obtained.");
}
}
}
public void updateUser(User user) throws SQLException {
String sql = "UPDATE users SET username=?, password=? WHERE ID=?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, user.getUsername());
statement.setString(2, user.getPassword());
statement.setInt(3, user.getID());
statement.executeUpdate();
}
public void deleteUser(int ID) throws SQLException {
String sql = "DELETE FROM users WHERE ID=?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1, ID);
statement.executeUpdate();
}
public User getUserByID(int ID) throws SQLException {
String sql = "SELECT username, password FROM users WHERE ID=?";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setInt(1, ID);
ResultSet resultSet = statement.executeQuery();
if (resultSet.next()) {
User user = new User();
user.setID(ID);
user.setUsername(resultSet.getString("username"));
user.setPassword(resultSet.getString("password"));
return user;
} else {
return null;
}
}
public List<User> getAllUsers() throws SQLException {
List<User> userList = new ArrayList<>();
String sql = "SELECT ID, username, password FROM users";
Statement statement = connection.createStatement();
ResultSet resultSet = statement.executeQuery(sql);
while (resultSet.next()) {
User user = new User();
user.setID(resultSet.getInt("ID"));
user.setUsername(resultSet.getString("username"));
user.setPassword(resultSet.getString("password"));
userList.add(user);
}
return userList;
}
}
```
以上就是用户 DAO 类的详细实现过程。通过调用 addUser、updateUser、deleteUser、getUserByID 和 getAllUsers 方法,我们可以完成对用户表的增删改查操作。其中,connect 和 disconnect 方法用于连接和断开数据库连接。
同时,我们还需要在数据库中创建一个名为 users 的表,该表包含 ID、username 和 password 字段。可以使用以下 SQL 语句来创建该表:
```sql
CREATE TABLE users (
ID INT PRIMARY KEY AUTO_INCREMENT,
username VARCHAR(50) NOT NULL,
password VARCHAR(50) NOT NULL
);
```
以上就是使用 Java 和 JDBC 完成对用户表的增删改查的详细过程。
阅读全文