java增删改查代码
时间: 2023-07-07 22:17:37 浏览: 33
以下是Java中常见的增删改查(CRUD)操作示例代码:
1. 增加(Create)操作示例:
```
// 创建一个新的用户对象
User user = new User();
user.setName("Tom");
user.setAge(18);
user.setGender("男");
// 将新用户插入到数据库中
Connection conn = null;
PreparedStatement pstmt = null;
try {
// 获取数据库连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "username", "password");
// 创建 PreparedStatement 对象
pstmt = conn.prepareStatement("INSERT INTO user(name, age, gender) VALUES (?, ?, ?)");
// 设置参数值
pstmt.setString(1, user.getName());
pstmt.setInt(2, user.getAge());
pstmt.setString(3, user.getGender());
// 执行 SQL 语句
pstmt.executeUpdate();
System.out.println("添加用户成功!");
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭资源
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
2. 查询(Retrieve)操作示例:
```
// 查询所有用户
List<User> userList = new ArrayList<>();
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
// 获取数据库连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "username", "password");
// 创建 Statement 对象
stmt = conn.createStatement();
// 执行查询语句
rs = stmt.executeQuery("SELECT * FROM user");
// 处理查询结果
while (rs.next()) {
User user = new User();
user.setId(rs.getInt("id"));
user.setName(rs.getString("name"));
user.setAge(rs.getInt("age"));
user.setGender(rs.getString("gender"));
userList.add(user);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭资源
if (rs != null) {
try {
rs.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (stmt != null) {
try {
stmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
// 输出查询结果
for (User user : userList) {
System.out.println(user);
}
```
3. 更新(Update)操作示例:
```
// 修改用户信息
User user = new User();
user.setId(1);
user.setName("Tom");
user.setAge(20);
user.setGender("男");
// 更新用户信息
Connection conn = null;
PreparedStatement pstmt = null;
try {
// 获取数据库连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "username", "password");
// 创建 PreparedStatement 对象
pstmt = conn.prepareStatement("UPDATE user SET name=?, age=?, gender=? WHERE id=?");
// 设置参数值
pstmt.setString(1, user.getName());
pstmt.setInt(2, user.getAge());
pstmt.setString(3, user.getGender());
pstmt.setInt(4, user.getId());
// 执行 SQL 语句
pstmt.executeUpdate();
System.out.println("更新用户成功!");
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭资源
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
4. 删除(Delete)操作示例:
```
// 根据用户 ID 删除用户
int userId = 1;
Connection conn = null;
PreparedStatement pstmt = null;
try {
// 获取数据库连接
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydb", "username", "password");
// 创建 PreparedStatement 对象
pstmt = conn.prepareStatement("DELETE FROM user WHERE id=?");
// 设置参数值
pstmt.setInt(1, userId);
// 执行 SQL 语句
pstmt.executeUpdate();
System.out.println("删除用户成功!");
} catch (SQLException e) {
e.printStackTrace();
} finally {
// 关闭资源
if (pstmt != null) {
try {
pstmt.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
if (conn != null) {
try {
conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![-](https://csdnimg.cn/download_wenku/file_type_column_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)