java idea中的增删改查
时间: 2023-09-03 19:13:31 浏览: 91
Java IDEA 中的增删改查通常是指对于数据库的增加、删除、修改和查询操作,可以通过 JDBC 来实现。下面是一个简单的示例代码:
1. 增加数据
```
String sql = "INSERT INTO table_name (column1, column2, column3) VALUES (?, ?, ?)";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, value1);
statement.setString(2, value2);
statement.setString(3, value3);
int rowsInserted = statement.executeUpdate();
```
2. 删除数据
```
String sql = "DELETE FROM table_name WHERE condition";
PreparedStatement statement = connection.prepareStatement(sql);
int rowsDeleted = statement.executeUpdate();
```
3. 修改数据
```
String sql = "UPDATE table_name SET column1 = ?, column2 = ? WHERE condition";
PreparedStatement statement = connection.prepareStatement(sql);
statement.setString(1, value1);
statement.setString(2, value2);
int rowsUpdated = statement.executeUpdate();
```
4. 查询数据
```
String sql = "SELECT column1, column2, column3 FROM table_name WHERE condition";
PreparedStatement statement = connection.prepareStatement(sql);
ResultSet result = statement.executeQuery();
while (result.next()) {
String value1 = result.getString("column1");
String value2 = result.getString("column2");
String value3 = result.getString("column3");
}
```
注意:上面的示例代码中,`connection` 是一个已经建立的数据库连接对象,`PreparedStatement` 是一个预编译的 SQL 语句对象,`ResultSet` 是一个结果集对象。在实际应用中,还需要对异常进行处理,并且要根据具体情况对 SQL 语句进行修改。
阅读全文