使用IDEA 编写一个增删改查
时间: 2023-07-20 09:03:31 浏览: 1070
好的,以下是一个基于Java和MySQL数据库的增删改查示例:
1. 首先,在MySQL数据库中创建一个名为“test”的数据库,并在其中创建一个名为“users”的表,该表包含以下列:id(INT),name(VARCHAR(50)),age(INT)和email(VARCHAR(100))。
2. 在IDEA中创建一个Java项目,并添加MySQL驱动程序和JDBC库。
3. 创建一个名为“User”的Java类,该类具有以下属性:id,name,age和email,并创建相应的Setter和Getter方法。
4. 创建一个名为“UserDAO”的Java类,在该类中实现增删改查的方法。以下是示例代码:
```
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
public class UserDAO {
private Connection connection;
public UserDAO() {
try {
Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection("jdbc:mysql://localhost/test", "root", "");
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
public List<User> getAllUsers() {
List<User> users = new ArrayList<User>();
try {
Statement statement = connection.createStatement();
ResultSet rs = statement.executeQuery("SELECT * FROM users");
while (rs.next()) {
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setAge(rs.getInt("age"));
user.setEmail(rs.getString("email"));
users.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
}
return users;
}
public User getUserById(int id) {
User user = new User();
try {
PreparedStatement preparedStatement = connection.prepareStatement("SELECT * FROM users WHERE id=?");
preparedStatement.setInt(1, id);
ResultSet rs = preparedStatement.executeQuery();
if (rs.next()) {
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setAge(rs.getInt("age"));
user.setEmail(rs.getString("email"));
}
} catch (SQLException e) {
e.printStackTrace();
}
return user;
}
public void addUser(User user) {
try {
PreparedStatement preparedStatement = connection.prepareStatement("INSERT INTO users(name, age, email) VALUES (?, ?, ?)");
preparedStatement.setString(1, user.getName());
preparedStatement.setInt(2, user.getAge());
preparedStatement.setString(3, user.getEmail());
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void updateUser(User user) {
try {
PreparedStatement preparedStatement = connection.prepareStatement("UPDATE users SET name=?, age=?, email=? WHERE id=?");
preparedStatement.setString(1, user.getName());
preparedStatement.setInt(2, user.getAge());
preparedStatement.setString(3, user.getEmail());
preparedStatement.setInt(4, user.getId());
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
public void deleteUser(int id) {
try {
PreparedStatement preparedStatement = connection.prepareStatement("DELETE FROM users WHERE id=?");
preparedStatement.setInt(1, id);
preparedStatement.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
5. 创建一个名为“Main”的Java类,该类包含主函数和用户交互。
```
import java.util.List;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
UserDAO userDAO = new UserDAO();
Scanner scanner = new Scanner(System.in);
boolean running = true;
while (running) {
System.out.println("请选择操作:");
System.out.println("1. 查询所有用户");
System.out.println("2. 根据id查询用户");
System.out.println("3. 添加用户");
System.out.println("4. 更新用户");
System.out.println("5. 删除用户");
System.out.println("0. 退出");
String option = scanner.nextLine();
switch (option) {
case "1":
List<User> users = userDAO.getAllUsers();
for (User user : users) {
System.out.println(user.getId() + "\t" + user.getName() + "\t" + user.getAge() + "\t" + user.getEmail());
}
break;
case "2":
System.out.println("请输入用户id:");
int id = scanner.nextInt();
scanner.nextLine();
User user = userDAO.getUserById(id);
System.out.println(user.getId() + "\t" + user.getName() + "\t" + user.getAge() + "\t" + user.getEmail());
break;
case "3":
System.out.println("请输入用户姓名:");
String name = scanner.nextLine();
System.out.println("请输入用户年龄:");
int age = scanner.nextInt();
scanner.nextLine();
System.out.println("请输入用户邮箱:");
String email = scanner.nextLine();
User newUser = new User();
newUser.setName(name);
newUser.setAge(age);
newUser.setEmail(email);
userDAO.addUser(newUser);
System.out.println("用户添加成功!");
break;
case "4":
System.out.println("请输入用户id:");
int updateId = scanner.nextInt();
scanner.nextLine();
User updateUser = userDAO.getUserById(updateId);
System.out.println("请输入新的姓名:");
String updateName = scanner.nextLine();
System.out.println("请输入新的年龄:");
int updateAge = scanner.nextInt();
scanner.nextLine();
System.out.println("请输入新的邮箱:");
String updateEmail = scanner.nextLine();
updateUser.setName(updateName);
updateUser.setAge(updateAge);
updateUser.setEmail(updateEmail);
userDAO.updateUser(updateUser);
System.out.println("用户更新成功!");
break;
case "5":
System.out.println("请输入用户id:");
int deleteId = scanner.nextInt();
scanner.nextLine();
userDAO.deleteUser(deleteId);
System.out.println("用户删除成功!");
break;
case "0":
running = false;
break;
default:
System.out.println("输入有误,请重新输入!");
break;
}
}
}
}
```
6. 运行Main类,即可进行增删改查操作。
阅读全文