用于打印密钥的getGeneratedKeys()无效。为什么?

问题描述 投票:0回答:1

为什么getGeneratedKeys()返回NULL?这是我的代码:

public void insertNewPreventivo(String cliente, int idProdottoNew, int[] opzioniNew) {
    String query = "INSERT INTO preventivo (prodotto,cliente) VALUES (?,?)";
    ResultSet rs = null;
    PreparedStatement ps = null;
    try {
        ps = connection.prepareStatement(query, ps.RETURN_GENERATED_KEYS);
        ps.setInt(1, idProdottoNew);
        ps.setString(2, cliente);
        ps.executeUpdate();
        rs = ps.getGeneratedKeys();
        if (rs.next()) {
            System.out.println(rs.getString(1));
        }
    }
    catch (SQLException e) {
        e.printStackTrace();
    }

这是我的数据库:

+-----------+-------------+------+-----+-------------------+-------------------+
| Field     | Type        | Null | Key | Default           | Extra             |
+-----------+-------------+------+-----+-------------------+-------------------+
| id        | datetime    | NO   | PRI | CURRENT_TIMESTAMP | DEFAULT_GENERATED |
| prodotto  | int         | NO   | MUL | NULL              |                   |
| cliente   | varchar(45) | NO   | MUL | NULL              |                   |
| impiegato | varchar(45) | YES  | MUL | NULL              |                   |
| prezzo    | double      | NO   |     | 0                 |                   |
+-----------+-------------+------+-----+-------------------+-------------------+

我要打印“ id”值。谢谢

java sql
1个回答
0
投票
© www.soinside.com 2019 - 2024. All rights reserved.