0
public Integer toevoegenKlant(Klant nieuweKlant) throws DBException {
try (Connection conn = ConnectionManager.getConnection();) {
try (PreparedStatement stmt = conn.prepareStatement(
"insert into klant(naam, voornaam, geboortedatum, opmerking, debetstand_limiet, actief) values(?,?,?,?,?,?)",PreparedStatement.RETURN_GENERATED_KEYS);) {
stmt.setString(1, nieuweKlant.getNaam());
stmt.setString(2, nieuweKlant.getVoornaam());
stmt.setDate(3, Date.valueOf(nieuweKlant.getGeboorteDatum()));
stmt.setString(4, nieuweKlant.getOpmerking());
stmt.setDouble(5, nieuweKlant.getDebetstandLimiet().doubleValue());
byte b;
if (nieuweKlant.isActief() == true){
b = 1;
}
else{
b = 0;
}
stmt.setByte(6, b);
stmt.execute();
ResultSet rs = stmt.getGeneratedKeys();
return rs.getInt(1);
} catch (SQLException sqlEx) {
throw new DBException("SQL-exception in toevoegenKlant - statement"+ sqlEx);
}
} catch (SQLException sqlEx) {
throw new DBException(
"SQL-exception in toevoegenKlant - connection"+ sqlEx);
}
}
所以當我檢查我的數據庫時,'klant'被添加,但它沒有返回生成的密鑰。任何人都知道我在做什麼錯了?Java sqllite get_generated_keys不工作
SQL-例外toevoegenKlant - statementjava.sql.SQLException:在結果的開始時設置 exception.DBException
'如果(RS .next()){}' – sidgate
接受答案,如果它解決了你的目的 – sidgate