public+boolean+delete(int+id)+( Connection+conn+.+null; statement+stmt+.+null; ResultSet+rs+=+nul
时间: 2023-12-29 22:05:35 浏览: 135
根据提供的引用内容,以下是一个public boolean delete(int id)方法的例子,其中包含了Connection、Statement和ResultSet的使用:
```java
public boolean delete(int id) {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
Class.forName("com.mysql.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost/users", "username", "password");
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE, ResultSet.CONCUR_READ_ONLY);
String sql = "DELETE FROM users WHERE id = " + id;
int rows = stmt.executeUpdate(sql);
if (rows > 0) {
return true;
}
} catch (ClassNotFoundException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} finally {
try {
if (rs != null) {
rs.close();
}
if (stmt != null) {
stmt.close();
}
if (conn != null) {
conn.close();
}
} catch (SQLException e) {
e.printStackTrace();
}
}
return false;
}
```
阅读全文