PreparedStatement pstmt = null; String sql = "delete from [key_store] where key_id = " + keyld; pstmt = (PreparedStatement) kdbc.dbConn.prepareStatement(sql); pstmt.executeUpdate(); PreparedStatement pstmt = null; String sql = "delete from [key_list] where key_name = '" + keyName + "'"; pstmt = (PreparedStatement) dbec.dbConn.prepareStatement(sql); pstmt.executeUpdate();
时间: 2023-11-27 09:55:28 浏览: 79
PreparedStatement详细用法
这段代码是在Java中使用JDBC执行两个SQL语句,分别是从名为"key_store"的表中删除指定"key_id"的记录以及从名为"key_list"的表中删除指定"key_name"的记录。其中,SQL语句中使用了变量"keyld"和"keyName",它们应该是在代码中被定义并赋值了相应的值。
需要注意的是,这段代码存在SQL注入的风险,应该使用预编译语句(Prepared Statement)来避免。具体方法是将SQL语句中的变量部分使用"?"来代替,然后通过调用预编译语句的setXXX()方法来设置变量的值,最后执行预编译语句即可。例如:
```
PreparedStatement pstmt = null;
String sql = "delete from key_store where key_id = ?";
pstmt = (PreparedStatement) kdbc.dbConn.prepareStatement(sql);
pstmt.setInt(1, keyld);
pstmt.executeUpdate();
```
```
PreparedStatement pstmt = null;
String sql = "delete from key_list where key_name = ?";
pstmt = (PreparedStatement) dbec.dbConn.prepareStatement(sql);
pstmt.setString(1, keyName);
pstmt.executeUpdate();
```
阅读全文