Java操作MySQL之增删改查

2023-02-22 21:10:13
**// 准备SQL语句** String sql = "SQL语句"; stmt = conn.prepareStatement(sql); **// 绑定占位符的值** stmt.setString(1, "new book"); //String类型数据 stmt.setInt(2, 1); //int类型数据 > 1. String sql = "INSERT INTO book (name, quantity) VALUES (?, ?)"; > stmt.execute(); //执行插入数据的SQL > 2. String sql = "SELECT * FROM book WHERE quantity > ? and name = ?"; > rs = stmt.executeQuery(); // 执行查询类型的SQL 3. String sql = "UPDATE book SET name = ? WHERE id = ?"; int rows = stmt.executeUpdate(); // 执行修改更新数据的SQL,返回受影响行数 4. String sql = "DELETE FROM book WHERE id = ?"; int rows = stmt.executeUpdate(); // 执行删除数据的SQL,返回受影响行数 **//准备SQL语句stmt = conn.prepareStatement(sql);**