为什么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”值。谢谢