jdbc access to just auto-generated primary key

分类:Java 2010-03-29 来源:CodeWeblog.com 人气:396

His school days to a company to site, encountered a small problem: how to obtain the record just inserted automatically generated primary key.

For example I would like to generate an order, you will need to insert a record in Table 2: "Order Form" and "commodity items" ( "commodity items" to "Order Form" of the primary key as foreign key).

I was very clumsy way, insert the record, according to the record number of conditions, the primary key check out, Hehe;

But later, to avoid this problem, I should first get the primary key and then to insert, anyway, are stupid enough.

When I was looking to learn jdbc JDBC Database Programming with J2EE This book, then I think enough of all of the above talk about how to obtain a new record no automatic generation of primary key, on-line had not seized, I thought no way, and some time ago to write api document jdbc courseware to see only found in related support, special published this article and give the students just getting started reminder.

We need to use the following two methods:

Connection interface defines methods

 PreparedStatement prepareStatement(String sql, int autoGeneratedKeys)
  Creates a default PreparedStatement object that gets automatically generated keys.

Statement interface defines methods

ResultSet getGeneratedKeys()
 Gets the object because execution of this Statement and create all the auto-generated keys.

The following table as an example:

 CREATE TABLE userinfo(
   name varchar(50) NULL

java code to achieve:

String sql = "insert into userinfo values (' Smith, John  ')";
PreparedStatement stmt = con.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
ResultSet rs = stmt.getGeneratedKeys();
int newid = 0;
if(rs.next()) newid = rs.getInt(1);
blog comments powered by Disqus


  • Primary key and foreign key difference 2010-06-25

    Primary key and foreign key difference between 2008-04-24 16:55 Primary key is to define a table play a major role in the data items, these items of data in the table is unique, while the system according to the primary key for table indexing. Foreign key

  • How to build Oracle in the form on the specified primary key and foreign key 2010-06-30

    Create table syntax - Create table syntax: create table table name ( Field Name Field Type 1 (length) is empty, Field name 2 field type is empty ); - Increase the primary key alter table primary key table name add constraint name primary key (field n

  • primary key and foreign key oracle 2010-03-25

    Primary keys and foreign keys - Add a primary key table to MID_A_TAB alter table mid_a_tab add constraint a_pk primary key (detpno); - Add a primary key table to MID_B_TAB alter table mid_b_tab add constraint b_pk primary key (a); - To the child tabl ...

  • Now many of the database table is not built not by the primary key and foreign key constraints? 2010-04-14

    There is a problem to you confirmation, we did the project, whether there are some very complex business, construction of tables in the database when the foreign key is not established, or even to establish the primary key, and to achieve the constra ...

  • On the design of the primary foreign key database design principles [learn posts collection] 2010-11-04

    1. Re: database primary keys and foreign key design principles pinglexp: support for the landlord behind the words if, as people have said, to consider the performance, the performance is not a single primary key to optimization alone, and other comp

  • oracle --- constraints (primary key, unique, not null, check, foreign key) 2010-09-30

    约束分为以下5种: 主键 ---唯一(表中只有一个主键),非空,不重复 唯一约束 检查约束: 检查一个列的内容是否合法 |---例如:年龄:0~150岁 非空约束 外键约束: 在两张表中进行约束的操作 create table person( pid varchar2(10) primary key ) Oracle错误信息: ORA-0001:违反唯一性约束条件(SCOTT.SYS_C005270) 错误信息上给出的是一个编号的形式,实际上此编号就是表示约束的名称,是系统分配的名称 如果我们想

  • View all tables oracle database foreign key relationship between the primary 2010-11-01

    In daily data maintenance, often delete data, if the data of the table has a foreign key association, not set to cascade delete, then you need to empty the child table to the relevant data. To find out all the main foreign key is a more difficult thi

  • constraints in oracle ----- Not null.unique.primary key.foreign key and check the similarities and differences 2011-04-12

    constraints in oracle Data Integrity Database not only stores data, it must also ensure the correctness of the data it holds. If the data is incorrect or inconsistent, then the integrity of the data may be destroyed, thus the reliability of the datab

  • Primary key constraints and foreign key integrity constraints 2011-09-21

    In the implementation of DML statements (such as INSERT, UPDATE or DELETE), the database will ensure that table rows can maintain its own integrity. This means that for any changes made to rows in a table can not affect the table's primary, forei

  • Oracle foreign key constraint defined constraints 2010-07-01

    Foreign key constraints to ensure referential integrity. Foreign key constraints limit the range of a column. One example is the limit state abbreviation in a limited set of values, this set of values is another control structure - a parent table Here we

iOS 开发

Android 开发

Python 开发



PHP 开发

Ruby 开发






Javascript 开发

.NET 开发



Copyright (C) codeweblog.com, All Rights Reserved.

CodeWeblog.com 版权所有 黔ICP备15002463号-1

processed in 0.229 (s). 12 q(s)