Java sqllite get_generated_keys not working


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'被添加,但它没有返回生成的键。有人知道我哪里做错了吗?

SQL-exception in toevoegenKlant - statementjava.sql.SQLException:在结果集开始之前例外。DBException

在处理结果集之前需要调用next方法

if(rs.next()){
   rs.getInt(1);
}

最新更新