gpt4 book ai didi

java - 在preparedstatement中创建自动增量id

转载 作者:行者123 更新时间:2023-12-02 06:39:57 25 4
gpt4 key购买 nike

我想使用preparedstatement创建自动增量id,我正在使用oracle数据库,这是我的代码

public Client newClient(Client client){
try {
con = DBConnection.getConnection(driver, url, name, pass);
pstmt = con.prepareStatement("INSERT INTO CLIENT (FIRSTNAME, LASTNAME, CAREER, CSALARY) VALUES (?, ?, ?, ?)", Statement.RETURN_GENERATED_KEYS);
pstmt.setString(1, client.getFirstName());
pstmt.setString(2, client.getLastName());
pstmt.setString(3, client.getCareer());
pstmt.setInt(4, client.getcSalary());
pstmt.executeUpdate();
rs = pstmt.getGeneratedKeys();
rs.next();

int id = rs.getInt(1);
client.setcId(id);
}catch(Exception ex){
ex.printStackTrace();
return null;
}finally{
try{ rs.close(); }catch (Exception e){}
try{ pstmt.close();}catch (Exception e){}
try{ con.close();}catch (Exception e){}
}//finally
return client;
}`

但我有这个错误

 java.sql.SQLException: ORA-01400: cannot insert NULL into ("AHMAD"."CLIENT"."CID")

请帮助我

最佳答案

听起来你需要一个 autonumber sequence

To retrieve the next value in the sequence order, you need to use nextval.

For example:

supplier_seq.nextval

这将为您的INSERT提供自动增量序列ID(即您只需执行INSERT,数据库将插入自动增量值)

关于java - 在preparedstatement中创建自动增量id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19244373/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com