Home:ALL Converter>JavaFX Sqlite Insert with Foreign Key

JavaFX Sqlite Insert with Foreign Key

Ask Time:2016-03-18T21:27:16         Author:Roman

Json Formatter

I am currently learning JavaFX and Sqlite.

I have an Sqlite Database which I built with MySQL manager, there are already foreign key rules etc.

For example user and adress:

CREATE TABLE "Benutzer" ("Benutzername" TEXT PRIMARY KEY  NOT NULL , "Vorname" VARCHAR, "Nachname" VARCHAR, "Email" TEXT UNIQUE , "Passwort" TEXT, "AdresseID" INTEGER, FOREIGN KEY(AdresseID) REFERENCES Adresse(AdresseID))

CREATE TABLE "Adresse" ("AdresseID" INTEGER PRIMARY KEY  NOT NULL , "Land" VARCHAR, "Stadt" VARCHAR, "Straße" TEXT, "Hausnr." INTEGER)

I connected the database to my JavaFX application, now I am trying to create the register function, so a new user is on an fxml template where he can fill in "Benutzername", "Vorname" etc. and his adress "Land", "Stadt" etc.

Here is the form:

enter image description here

Adding these values to the database works, but I do not have an idea how to tell javaFX that this current user which is inserted has to take the current adress which is inserted (its AdressID) as foreign key.

User is inserted without foreign key: enter image description here

I was reseaching and found things like

mysql_insert_id returns the last auto-increment value generated by the database connection currently in use.

But I dont know how to access it.

Thats my model which I use to fill in the data into my database:

public class RegisterModel {
Connection conection;

// Konstruktor
public RegisterModel() {
    conection = SqliteConnection.Connector();

    // Wenn die Verbindung nicht erfolgreich ist, dann App schließen
    if (conection == null) {
        System.out.println("connection not successful");
        System.exit(1);
    }
}

public boolean isDbConnected() {
    try {
        return !conection.isClosed();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return false;
    }
}

public boolean RegisterUser(String user_benutzername, String user_vorname, String user_nachname, String user_email,
        String user_passwort, String adress_land, String adress_stadt, String adress_strasse,
        String adress_hausnummer) throws SQLException {

    PreparedStatement preparedStatement = null;
    PreparedStatement preparedStatement_for_adress = null;

    // or ignore besprechen
    String query = "insert or ignore into Benutzer (Benutzername, Vorname, Nachname, Email, Passwort) values(?,?,?,?,?)";
    String query_adress = "insert into Adresse (Land, Stadt, Straße, 'Hausnr.') values(?,?,?,?)";

    try {
        preparedStatement = conection.prepareStatement(query);

        preparedStatement.setString(1, user_benutzername);
        preparedStatement.setString(2, user_vorname);
        preparedStatement.setString(3, user_nachname);
        preparedStatement.setString(4, user_email);
        preparedStatement.setString(5, user_passwort);
        System.out.println("Benutzer wurde eingefuegt");

        preparedStatement_for_adress = conection.prepareStatement(query_adress);

        preparedStatement_for_adress.setString(1, adress_land);
        preparedStatement_for_adress.setString(2, adress_stadt);
        preparedStatement_for_adress.setString(3, adress_strasse);
        preparedStatement_for_adress.setString(4, adress_hausnummer);
        System.out.println("Adresse wurde eingefügt");

        preparedStatement.executeUpdate();
        preparedStatement_for_adress.executeUpdate();
        return true;

    } catch (Exception e) {
        e.printStackTrace(); 
        return false;
    } finally {
        preparedStatement.close();
        preparedStatement_for_adress.close();

    }
}

}

I think there must be a 3rd query which takes the AdressID of the current adress and assigns it to the current user as his adress? So thats the interessting part:

String query = "insert or ignore into Benutzer (Benutzername, Vorname, Nachname, Email, Passwort) values(?,?,?,?,?)";
    String query_adress = "insert into Adresse (Land, Stadt, Straße, 'Hausnr.') values(?,?,?,?)";

Author:Roman,eproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/36085649/javafx-sqlite-insert-with-foreign-key
yy