oracle插入数据时,返回自增的id

来源:互联网 发布:知世故而不世故 下联 编辑:程序博客网 时间:2024/06/05 00:33

public int addRole(Role role){

Connection connection = null;
PreparedStatement pstmt = null;
ResultSet rs = null;

try {
String sql = "insert into siteRole values(seq_siteRole_id.nextval,?)";
connection = DbConnection.getConnection();
int a[] = {1};
//String generatedColumns[] = {"ORDER_ID"};
pstmt = connection.prepareStatement(sql,a);
//pstmt = connection.prepareStatement(sql,generatedColumns);
pstmt.setString(1, role.getName());
int i = pstmt.executeUpdate();
rs = pstmt.getGeneratedKeys();
rs.next();
int j =  (int) rs.getLong(1);
return j;

} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}

return 0;
}

0 0
原创粉丝点击