String sql = "DELETE FROM student WHERE id = '" + id + "'"
但是上述方式存在sql注入风险,
perstmt = conn.prepareStatement("DELETE FROM student WHERE id = ?")
perstmt.setString(1,id)
Java连接数据库,要删除指定行的信息,可以使用delete语句,传入某行的参数,示例如下:
public class Update1 {public static void main(String[] args) throws ClassNotFoundException, SQLException {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver")//加载SQL SERVER数据库
Connection con = DriverManager.getConnection("jdbc:odbc:tt", "sa", "www")//连接数据库
String sq="delete from t1 where userName=?"//删除指定行
PreparedStatement ps = con.prepareStatement(sq)
ps.setString(1, "cct")
int t = ps.executeUpdate()
if (t > 0) {//判断是否删除成功
System.out.println("ok")
} else {
System.out.println("false")
}
ps.close()
con.close()
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)