我在带有SQlite数据库的javaFx项目中使用JDBC。我有两个表"PROJECT"
和"SMAllPROJECT"
,例如PROJECT具有此结构PROJECT(name,date,state)
而SMAllPROJECT
具有此结构SMAllPROJECT(name,date,state,*project_name*)
。 Project_name
的{{1}}列引用了name
。
我正在尝试从PROJECT table
删除行,并且应删除PROJECT
中引用的行。在sqlite studio中,当我从父表中删除一行时,我配置了SMAllPoject
方法,当我在SQlite studio中测试时,每个东西都很好,但是在我的代码中,它只是从父表中删除了行。
这是我的代码:
控制器方法
onCascade
DAO模式方法
public void ExecuteDeleteProject() {
if (!SearchIdProjectSupp.getText().isEmpty()) {
Project project = new ProjectDao().FindString(SearchIdProjectSupp.getText());
new ProjectDao().Delete(project);
String title = "Suppression";
String message = "Vous avez supprimé le projet " + SearchIdProjectSupp.getText() + ".";
NotificationType notification = NotificationType.SUCCESS;
TrayNotification tray = new TrayNotification();
tray.setTitle(title);
tray.setMessage(message);
tray.setNotificationType(notification);
tray.setAnimationType(AnimationType.SLIDE);
tray.setImage(new Image("Images/check.png"));
tray.setRectangleFill(Paint.valueOf("#a8a9fe"));
tray.showAndDismiss(Duration.seconds(4));
SearchIdProjectSupp.setText("");
SuppPaneProject.setVisible(false);
DeleteProjetButton.setDisable(true);
CountP.setVisible(true);
CountP();
CountR();
CountPP();
}
}
这是我的DDL
public boolean Delete(Project Object) {
try {
String queryDeletePerson = "DElETE FROM PROJECT WHERE name=" + "'" + Object.getName() + "'";//Query Insertion in Person_Table
PreparedStatement preparedStatementPerson = Dbaconnection.getConnection().prepareStatement(queryDeletePerson);//Prepared statement i use this for high performance
preparedStatementPerson.execute();
} catch (SQLException ex) {
Logger.getLogger(EMPDao.class.getName()).log(Level.SEVERE, null, ex);
}
return true;
}
答案 0 :(得分:2)
根据SQLite Foreign Key Support和How do you enforce foreign key constraints in SQLite through Java? 的回答 ,它应该在执行任何查询之前配置数据库连接。我有这个问题,因为我没有强制执行外键约束。
public static final String DB_URL = "jdbc:sqlite:database.db";
public static final String DRIVER = "org.sqlite.JDBC";
public static Connection getConnection() throws ClassNotFoundException {
Class.forName(DRIVER);
Connection connection = null;
try {
SQLiteConfig config = new SQLiteConfig(); //I add this configuration
config.enforceForeignKeys(true);
connection = DriverManager.getConnection(DB_URL,config.toProperties());
} catch (SQLException ex) {}
return connection;
}