jdbc增删改查完整代码
时间: 2024-07-02 11:00:17 浏览: 46
JDBC (Java Database Connectivity) 是 Java 语言中用于与数据库交互的标准 API。以下是一个简单的 JDBC 示例,展示了如何使用 JDBC 进行基本的 CRUD(创建、读取、更新和删除)操作。这里以 MySQL 数据库为例:
首先,确保已添加 MySQL 驱动(如 MySQL Connector/J)到项目中,并导入相应的包:
```java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
```
然后,定义数据库连接信息:
```java
String url = "jdbc:mysql://localhost:3306/your_database";
String username = "your_username";
String password = "your_password";
```
接下来,我们写一个通用的 CRUD 方法:
```java
public void operate(String tableName, String operation, String... args) {
Connection conn = null;
PreparedStatement pstmt = null;
try {
// 1. 获取数据库连接
conn = DriverManager.getConnection(url, username, password);
// 2. 创建 SQL 语句
String sql;
switch (operation) {
case "insert":
sql = "INSERT INTO " + tableName + " (column1, column2, ...) VALUES (?, ?, ...)";
pstmt = conn.prepareStatement(sql);
for (int i = 0; i < args.length; i += 2) {
pstmt.setString(i, args[i]);
pstmt.setString(i + 1, args[i + 1]);
}
break;
case "select":
sql = "SELECT * FROM " + tableName + " WHERE condition = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, args);
break;
case "update":
sql = "UPDATE " + tableName + " SET column1 = ?, column2 = ? WHERE condition = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, args);
pstmt.setString(2, args);
pstmt.setString(3, args);
break;
case "delete":
sql = "DELETE FROM " + tableName + " WHERE condition = ?";
pstmt = conn.prepareStatement(sql);
pstmt.setString(1, args);
break;
default:
throw new IllegalArgumentException("Unsupported operation: " + operation);
}
// 3. 执行 SQL
pstmt.executeUpdate();
// 4. 处理结果(对于 SELECT 操作)
if ("select".equals(operation)) {
ResultSet rs = pstmt.executeQuery();
// ... 处理查询结果
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (pstmt != null) pstmt.close();
if (conn != null) conn.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
```
使用方法示例:
```java
// 插入数据
operate("your_table", "insert", "value1", "value2", ...);
// 查询数据
operate("your_table", "select", "condition_value");
// 更新数据
operate("your_table", "update", "new_value", "other_column", "condition_value");
// 删除数据
operate("your_table", "delete", "condition_value");
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.png)
![-](https://img-home.csdnimg.cn/images/20210720083327.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)