SQL java获取赋值给自动增量主键的值 [英] SQL java get value assigned to auto increment primary key

查看:254
本文介绍了SQL java获取赋值给自动增量主键的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的表中有一个主键自动增量属性。我想知道为使用statement.executeUpdate()插入的行分配给它的值。如何以最佳方式实现这一目标?

I have a primary key auto increment attribute in my table. I want to know the value assigned to it for a row that is inserted using statement.executeUpdate(). How to achieve this in the best possible manner?

推荐答案

使用 Statement#getGeneratedKeys() 语句#cuteUpdate(String,int) (这是JDBC 3.0特性,您的数据库必须支持JDBC 3.0)。

Use Statement#getGeneratedKeys() and Statement#executeUpdate(String, int) (this is a JDBC 3.0 feature, your database has to support JDBC 3.0).

这是一个返回 ResultSet 的示例,其中包含TABLE1中自动生成列的值:

Here's an example that returns a ResultSet with values for auto-generated columns in TABLE1:

Statement stmt = conn.createStatement();
int rows = stmt.executeUpdate("INSERT INTO TABLE1 (C11, C12) VALUES (1,1)", Statement.RETURN_GENERATED_KEYS);
ResultSet rs = stmt.getGeneratedKeys();

这篇关于SQL java获取赋值给自动增量主键的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆