简体   繁体   English

Java sqllite get_genic_keys不起作用

[英]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);
  }



  }

So when I check my database, the 'klant' gets added, but it's not returning the generated key. 因此,当我检查数据库时,会添加'klant',但不会返回生成的密钥。 Anyone know what I'm doing wrong? 有人知道我在做什么错吗?

SQL-exception in toevoegenKlant - statementjava.sql.SQLException: Before start of result set exception.DBException toevoegenKlant中的SQL异常-statementjava.sql.SQLException:结果集异常开始前。

You need to call next method before working on Result set 您需要在处理结果集之前调用下一个方法

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM