Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to return the self-increasing primary key value of the new record in Spring JDBC

2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

The main content of this article is to explain "how to return the self-increasing key value of Spring JDBC new records". Interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Next let the editor to take you to learn "Spring JDBC new records how to return self-increasing primary key value"!

In the JDBC3.0 specification, primary key values automatically generated by the database are allowed to be bound to Statement or PreparedStatement when new records are added. When using Statement, you can bind primary key values in the following ways:

Int executeUpdate (String sql,int autoGeneratedKeys)

You can also create a PreparedStatement that is bound to add value through Connection:

PreparedStatement prepareStatement (String sql,int autoGeneratedKeys)

The primary key value generated by the database can be bound when the autoGeneratedKeys parameter is set to the Statement.RETURN_GENERATED_ keys value, and when set to Statement.NO_GENERATED_KEYS, the primary key value is not bound. The following code is shown:

Statement stmt = conn.createStatement (); String sql = "insert into user (username,age) values ('tom',22)"; stmt.executeUpdate (sql,Statement.RETURN_GENERATED_KEYS); ResultSet rs = stmt.getGeneratedKeys (); if (rs.next ()) {int key = rs.getInt (0);}

Using this technique, Spring provides a method to return the corresponding primary key value of the new record.

To understand the springcloud architecture, please add: three, six, two, four, seven, two, 59

Int update (PreparedStatementCreator psc,KeyHolder generatedKeyHolder)

Org.springframework.jdbc.support.KeyHolder is a callback interface, which is used by Spring to save the primary key corresponding to the newly added records. The API method of this API is described as follows:

Number getKey () throws InvalidDataAccessApiUsageException

When only one row of records is inserted and the primary key is not a compound key but a numeric type, the new primary key value can be returned directly by this method. This method throws an InvalidDataAccessApiUsageException if it is a compound primary key, or if more than one primary key is returned.

Map getKeys () throws InvalidDataAccessApiUsageException

In the case of a compound primary key, the column name and column value constitute an Entry in Map. If multiple primary keys are returned, the method throws an InvalidDataAccessApiUsageException exception.

List getKeyList ()

If multiple primary keys are returned, that is, multiple records have been added to PreparedStatement, each primary key corresponds to a Map, and multiple Map form a List.

At this point, I believe that everyone on the "Spring JDBC new records how to return self-increasing primary key value" have a deeper understanding, might as well to the actual operation of it! Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report