sql插入java后返回id

时间:2016-08-08 13:12:18

标签: java sql

将数据插入我的数据库后,数据库会自动生成ID,我希望将其返回给我。但我不知道怎么......

public Integer addPerson(person p) throws DBException {
    try (Connection conn = ConnectionManager.getConnection();) {
        try (PreparedStatement stmt = conn.prepareStatement(
                "insert into klant(name, date) values (?,?)");) {
            stmt.setString(1, p.getName());
            stmt.setObject(2, Date.valueOf(p.getDate()));
            stmt.execute();
        } catch (SQLException s) {
            throw new DBException(s);
        }

    } catch (SQLException s) {
        throw new DBException(s);
    }
    return id_generated_by_db;
}

0 个答案:

没有答案