我刚接触Java并做作业。我有两个关系表,我不知道插入外键的最佳方法是什么。那是我的代码:
String sql = "INSERT INTO verkoper (voornaam, naam, email) VALUES (?,?,?)";
preparedStatement = conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
preparedStatement.setString(1, txtVoornaam.getText());
preparedStatement.setString(2, txtNaam.getText());
preparedStatement.setString(3, txtEmail.getText());
preparedStatement.executeUpdate();
myRs = preparedStatement.getGeneratedKeys();
myRs.next();
String sql2 = "INSERT INTO boek (naam, auteur, uitgever, taal, jaar, verkoperid) VALUES (?,?,?,?,?,?,?,?,?)";
preparedStatement = conn.prepareStatement(sql2);
preparedStatement.setString(1, txtBoekNaam.getText());
preparedStatement.setString(2, txtAuteur.getText());
preparedStatement.setString(3, txtUitgever.getText());
preparedStatement.setString(4, txtTaal.getText());
preparedStatement.setString(5, txtJaar.getText());
preparedStatement.setString(6, Integer.toString(myRs.getInt(1)) );
preparedStatement.execute();