0
我想使用PreparedStatement的,我使用的Oracle数据库中创建自动递增的ID,这是我的代码创建的PreparedStatement自动增量ID
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")
PLZ帮我
AFAIK,Oracle没有任何称为auto_increment的东西。如果您使用的是序列,则必须明确地将sequence.nextval设置为该键,否则使用触发器更新该列。 –
您需要创建一个触发器来处理自动ID生成。 –
非常感谢你的答复,但你可以给我举例cuz我尝试使用seq.nextval,但我有相同的错误 – Ahmad