使用idea中的javaweb对数据库进行增删改查操作
时间: 2024-05-07 14:18:36 浏览: 111
首先,需要在项目中添加数据库驱动jar包。然后,创建一个数据库连接。在创建连接之前,需要先在数据库中创建相应的表格。
下面是一个简单的示例代码,用于对MySQL数据库进行增删改查操作:
```java
import java.sql.*;
public class DatabaseManager {
private String url = "jdbc:mysql://localhost:3306/test";
private String username = "root";
private String password = "root";
public Connection getConnection() throws SQLException {
return DriverManager.getConnection(url, username, password);
}
public void insert(String name, int age) throws SQLException {
String sql = "INSERT INTO user (name, age) VALUES (?, ?)";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, name);
pstmt.setInt(2, age);
pstmt.executeUpdate();
}
}
public void update(int id, String name, int age) throws SQLException {
String sql = "UPDATE user SET name = ?, age = ? WHERE id = ?";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setString(1, name);
pstmt.setInt(2, age);
pstmt.setInt(3, id);
pstmt.executeUpdate();
}
}
public void delete(int id) throws SQLException {
String sql = "DELETE FROM user WHERE id = ?";
try (Connection conn = getConnection();
PreparedStatement pstmt = conn.prepareStatement(sql)) {
pstmt.setInt(1, id);
pstmt.executeUpdate();
}
}
public void select() throws SQLException {
String sql = "SELECT * FROM user";
try (Connection conn = getConnection();
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery(sql)) {
while (rs.next()) {
int id = rs.getInt("id");
String name = rs.getString("name");
int age = rs.getInt("age");
System.out.println("id: " + id + ", name: " + name + ", age: " + age);
}
}
}
}
```
在这个示例代码中,我们定义了一个 `DatabaseManager` 类,用于创建数据库连接和执行增删改查操作。其中,`getConnection()` 方法用于创建数据库连接,`insert()` 方法用于插入新的数据,`update()` 方法用于修改数据,`delete()` 方法用于删除数据,`select()` 方法用于查询数据。
在实际使用中,我们可以根据自己的需求进行相应的修改和调整。
阅读全文
相关推荐
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231044901.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.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)
![docx](https://img-home.csdnimg.cn/images/20241231044901.png)
![rar](https://img-home.csdnimg.cn/images/20241231044955.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)