try {
Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
conn = DriverManager.getConnection("jdbc:ucanaccess://C:\\Users\\AhzelBrend\\Documents\\Student.mdb");
Statement stmt = conn.createStatement();
String Query = "DELETE FROM StudentTable WHERE FULLNAME='"+ List1.getSelectedValue()+ "'";
stmt.execute(Query);
JOptionPane.showMessageDialog(null,"Record Deleted");
FillList();
IKlir();
}
catch(ClassNotFoundException ex) {
Logger.getLogger(Student.class.getName()).log(Level.SEVERE,null, ex);
}
catch(SQLException ex){
JOptionPane.showMessageDialog(null, ex.toString());
}
答案 0 :(得分:0)
在您的数据库中试用此代码并查看输出。您可以更改一下并使用数据库进行测试。希望有所帮助
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class DbTest {
public static void main(String[] args) {
try {
// Your MS Access driver
// Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
// My Test H2 driver
Class.forName("org.h2.Driver");
// Your MS Acess connection
// Connection conn = DriverManager.getConnection("jdbc:ucanaccess://C:\\Users\\AhzelBrend\\Documents\\Student.mdb");
// My H2 connection
Connection conn = DriverManager.getConnection("jdbc:h2:mem:test;DB_CLOSE_DELAY=10");
Statement stmt = conn.createStatement();
// Create and fill database
stmt.execute("CREATE TABLE StudentTable (FULLNAME VARCHAR(255))");
for (int i = 0; i < 10; i++) {
stmt.executeUpdate("INSERT INTO StudentTable(FULLNAME) VALUES ('Name " + i + "')");
}
// Your delete statement
// String Query = "DELETE FROM StudentTable WHERE FULLNAME='"+ List1.getSelectedValue()+ "'";
// My delete statement
String Query = "DELETE FROM StudentTable WHERE FULLNAME='Name 5'";
int rows = stmt.executeUpdate(Query);
System.out.println(rows + " rows affected");
conn.commit();
conn.close();
// Open a new Connection to test th result
conn = DriverManager.getConnection("jdbc:h2:mem:test;DB_CLOSE_ON_EXIT=FALSE");
stmt = conn.createStatement();
ResultSet results = stmt.executeQuery("SELECT * FROM StudentTable");
// Stundent 'Name 5' has been deleted
while (results.next()) {
System.out.println("Student " + results.getString("FULLNAME"));
}
// JOptionPane.showMessageDialog(null,"Record Deleted");
// FillList();
// IKlir();
} catch (ClassNotFoundException ex) {
ex.printStackTrace();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
}