Return Auto Generated Key Queryjdbc Mysql Java

Posted on  by 

Specifically, this method uses some Spring JDBC classes and methods to (a) execute a SQL INSERT statement, (b) get the generated id from the database for the record I just inserted (i.e., the value of the autoincrement field in a MySQL database table), and (c) return that integer value at the end of the method. Create a table that contains an AUTOINCREMENT column. Write a simple sp that inserts a record into this table. Call the sp using the 'RETURNGENERATEDKEYS' value in the prepareStatement function. Attempt to obtain the new insert ID value using the getGeneratedKey function. The resultset is empty.

List:MySQL and Java
From:Christopher TaylorDate:January 8 2003 1:32pm
Subject:Re: retrieving auto-generated keys on bulk insert
View as plain text
Java
Thread
• retrieving auto-generated keys on bulk insertDane Foster8 Jan
• Re: retrieving auto-generated keys on bulk insertMark Matthews8 Jan
• Re: retrieving auto-generated keys on bulk insertPaul DuBois8 Jan
• Re: retrieving auto-generated keys on bulk insertDane Foster8 Jan
• Re: retrieving auto-generated keys on bulk insertMark Matthews8 Jan
• Re: retrieving auto-generated keys on bulk insertPaul DuBois8 Jan
Re: retrieving auto-generated keys on bulk insertChristopher Taylor8 Jan
• Re: retrieving auto-generated keys on bulk insertJeff Mathis8 Jan
• Re: retrieving auto-generated keys on bulk insertDane Foster8 Jan
• Re: retrieving auto-generated keys on bulk insertDane Foster8 Jan
• Re: retrieving auto-generated keys on bulk insertJeff Mathis8 Jan
• Re: retrieving auto-generated keys on bulk insertDane Foster8 Jan
• Re: retrieving auto-generated keys on bulk insertPaul DuBois8 Jan
• Re: retrieving auto-generated keys on bulk insertJeff Mathis8 Jan
• Re: retrieving auto-generated keys on bulk insertTim Endres9 Jan
• Re: retrieving auto-generated keys on bulk insertDane Foster8 Jan

Return Auto Generated Key Queryjdbc Mysql Java 10

-->Return

Return Auto Generated Key Query Jdbc Mysql Java Tutorial

Queryjdbc

The Microsoft JDBC Driver for SQL Server supports the optional JDBC 3.0 APIs to retrieve automatically generated row identifiers. The main value of this feature is to provide a way to make IDENTITY values available to an application that is updating a database table without a requiring a query and a second round-trip to the server.

Because SQL Server doesn't support pseudo columns for identifiers, updates that have to use the auto-generated key feature must operate against a table that contains an IDENTITY column. SQL Server allows only a single IDENTITY column per table. The result set that is returned by getGeneratedKeys method of the SQLServerStatement class will have only one column, with the returned column name of GENERATED_KEYS. If generated keys are requested on a table that has no IDENTITY column, the JDBC driver will return a null result set.

As an example, create the following table in the sample database:

In the following example, an open connection to the sample database is passed in to the function, an SQL statement is constructed that will add data to the table, and then the statement is run and the IDENTITY column value is displayed.

See also

Coments are closed