2014-03-06 52 views
0

如何從java訪問中刪除記錄?我嘗試了以下,但它似乎並沒有工作。從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() + "')"); 
} 

回答

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(); 
} 
0

你應該使用JDBC而不是使這一切一個大的字符串

的PreparedStatement ST = connection.createStatement使用參數化查詢(「DELETE FROM [AssignmentDetails],其中[輸入日期] =?[項目ID] = ?,等等);

然後設置在準備好的語句中的參數,然後執行它