jdbc access to just auto-generated primary key

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(
   id int IDENTITY(1,1) PRIMARY KEY NOT NULL,
   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);
stmt.executeUpdate();
ResultSet rs = stmt.getGeneratedKeys();
int newid = 0;
if(rs.next()) newid = rs.getInt(1);
  • del.icio.us
  • StumbleUpon
  • Digg
  • TwitThis
  • Mixx
  • Technorati
  • Facebook
  • NewsVine
  • Reddit
  • Google
  • LinkedIn
  • YahooMyWeb

Related Posts of jdbc access to just auto-generated primary key

  • Hibernate II Study Notes

    11. Many-to-many Of many that can be converted to two one-to-many <set name="students" table="teacher_student"> <key column="techer_id"/> <many-to-many column="student_id"/> </set> many-to-many data only from one end of the mainten

  • Choose Hibernate or iBATIS has its reasons

    Hibernate features: Hibernate powerful database has nothing to do with good, O / R mapping ability, and if you are very proficient in Hibernate, but also for Hibernate to conduct an appropriate package, then your project will be the entire persistence lay

  • Hibernate Inteceptor

    The end of the project stage, the client suddenly put forward a very troublesome but normal demand, the system records all changes must be carried out. Formats such as: 2004.1.1 12:30 Ikuya wind orders Sales Order Date 2004.1.2-> 2004.1.3 The firs ...

  • FLEX: integrating Spring + Hibernate

    Before a friend also wanted to study development of FLEX. Asked me to help him to be a small sample. Spent a weekend time, to integrate a sampleproject. Client: FLEX Server: Spring2.5 + Hibernate3.2 + Hibernate-annotations3.3.1 + MySQL5 FDS: BlazeDS3 IDE:

  • hibernate using c3p0 connection pooling

    Private http://www.lifevv.com/tenyo/doc/20070605102040991.html c3p0 for open source's JDBC connection pool, with the release hibernate. This article describes how to use the hibernate configuration in c3p0. c3p0 connection pool configuration is v ...

  • Hibernate configuration parameters hibernate.hbm2ddl.auto

    Hibernate in the configuration file: <properties> <property name="hibernate.hbm2ddl.auto" value="create" /> </ properties> Parameter Description: validate load hibernate, the authentication to create a database t ...

  • In the servlet use Bean

    According to Sun's definition, JavaBean is a reusable software components. In fact JavaBean is a Java class, through the package into a property and methods of treatment of a function or a business object, referred to as bean. Because JavaBean is ...

  • Hibernate secondary cache

    Hibernate cache: 2-bit cache, also known as process-level cache or SessionFactory level cache, secondary cache can be shared by all of the session Cache configuration and the use of: Will echcache.xml (the document code in hibernate package directory ...

  • The level Hibernate cache

    Hibernate cache level: (1) a cache is very short and the session life cycle consistent, also known as session-level cache-level cache or transaction-level cache (2) Ways of Supporting level cache: get (); load (); iterator (); only entity object cach ...

blog comments powered by Disqus
Recent
Recent Entries
Tag Cloud
Random Entries