如何从java中删除访问记录?我尝试了以下但它似乎不起作用。
public void Delete() throws SQLException {
st.executeUpdate("DELETE FROM [AssignmentDetails] WHERE ([Entry Date],[Project ID],[Employee],[Hours Worked],[Firm Deadline],[Description of Assignment],[Additional Details],[ID]) = ('" + AbstractVariables.getEntryDate() + "','" + AbstractVariables.getProjectID() + "','" + AbstractVariables.getEmployee() + "','" + AbstractVariables.getHoursWorked() + "','" + AbstractVariables.getFirmDeadline() + "','" + AbstractVariables.getDescription() + "','" + AbstractVariables.getAdditionalDetails() + "','"+AbstractVariables.getID() + "')");
}
答案 0 :(得分:1)
try {
Connection con;
Statement stmt;
String url = "jdbc:odbc:GameData.mdb";
// DATABASE CONNECTION MAGIC :-)
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
con = DriverManager.getConnection(url, "user", "password");
stmt = con.createStatement();
// DELETE SOME SOME STUDENT DATA (RARELY USED IN GOOD PRACTICE)
stmt.executeUpdate("delete from students where phone = '000-0000'");
// DATABASE CLOSE/CLEANUP
stmt.close();
con.close();
}
catch(Exception e) {
e.printStackTrace();
}
答案 1 :(得分:0)
您应该使用JDBC进行参数化查询,而不是将其全部放入一个大字符串
PreparedStatement st = connection.createStatement(“DELETE FROM [AssignmentDetails] WHERE [Entry Date] =?,[Project ID] = ?, etc ...);
然后在预准备语句上设置参数,然后执行它