写出jdbc更新数据库的数据的代码
时间: 2023-12-02 13:03:33 浏览: 25
好的,以下是使用 JDBC 更新数据库数据的示例代码:
```
// 加载 JDBC 驱动
Class.forName("com.mysql.cj.jdbc.Driver");
// 建立数据库连接
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/test?useSSL=false", "root", "password");
// 创建 PreparedStatement 对象
String sql = "UPDATE student SET name = ?, age = ? WHERE id = ?";
PreparedStatement stmt = conn.prepareStatement(sql);
// 设置参数
stmt.setString(1, "张三");
stmt.setInt(2, 18);
stmt.setInt(3, 1);
// 执行更新操作
int result = stmt.executeUpdate();
// 关闭连接和语句对象
stmt.close();
conn.close();
```
希望能够解决您的问题。
相关问题
写出jdbc批量增删改数据库的数据的代码
当我在前面说了什么时, 为什么狗会汪汪叫?
以下是 Java JDBC 批量增删改数据库操作的代码示例:
// 假设有一个名为 "users" 的表
// 数据库连接对象
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "root", "password");
// 要执行的 SQL 语句
String insertSql = "INSERT INTO users (name, age) VALUES (?, ?)";
String updateSql = "UPDATE users SET age = ? WHERE id = ?";
String deleteSql = "DELETE FROM users WHERE id = ?";
// 创建 PreparedStatement 对象
PreparedStatement insertStmt = conn.prepareStatement(insertSql);
PreparedStatement updateStmt = conn.prepareStatement(updateSql);
PreparedStatement deleteStmt = conn.prepareStatement(deleteSql);
// 批量插入多条数据
insertStmt.setString(1, "Alice");
insertStmt.setInt(2, 18);
insertStmt.addBatch();
insertStmt.setString(1, "Bob");
insertStmt.setInt(2, 20);
insertStmt.addBatch();
insertStmt.setString(1, "Charlie");
insertStmt.setInt(2, 22);
insertStmt.addBatch();
// 批量更新多条数据
updateStmt.setInt(1, 25);
updateStmt.setInt(2, 1);
updateStmt.addBatch();
updateStmt.setInt(1, 22);
updateStmt.setInt(2, 2);
updateStmt.addBatch();
// 批量删除多条数据
deleteStmt.setInt(1, 3);
deleteStmt.addBatch();
deleteStmt.setInt(1, 4);
deleteStmt.addBatch();
// 执行批量操作
int[] insertCounts = insertStmt.executeBatch();
int[] updateCounts = updateStmt.executeBatch();
int[] deleteCounts = deleteStmt.executeBatch();
// 关闭资源
insertStmt.close();
updateStmt.close();
deleteStmt.close();
conn.close();
上述代码示例中,我们首先获取了一个 MySQL 数据库连接对象,然后创建了三个 PreparedStatement 对象分别用于执行插入、更新和删除操作。接着,我们使用 PreparedStatement 的 addBatch() 方法向批处理中添加多条记录,最后使用 executeBatch() 方法执行批量操作并获取每次操作所影响的行数。最后,我们关闭了资源。
JAVA 从数据库查询数据 写入Excel 写出JAVA代码
您好,我可以回答这个问题。JAVA 可以通过 JDBC 连接数据库,从数据库中查询数据,然后使用 Apache POI 库将数据写入 Excel 文件中。以下是示例代码:
```
// JDBC 连接数据库
Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/mydatabase", "username", "password");
// 查询数据
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM mytable");
// 创建 Excel 文件
Workbook workbook = new XSSFWorkbook();
Sheet sheet = workbook.createSheet("Sheet1");
// 写入数据
int rowNum = ;
while (rs.next()) {
Row row = sheet.createRow(rowNum++);
row.createCell().setCellValue(rs.getInt("id"));
row.createCell(1).setCellValue(rs.getString("name"));
row.createCell(2).setCellValue(rs.getDouble("price"));
}
// 保存 Excel 文件
FileOutputStream outputStream = new FileOutputStream("data.xlsx");
workbook.write(outputStream);
workbook.close();
outputStream.close();
```
希望能对您有所帮助。