You are viewing a plain text version of this content. The canonical link for it is here.
Posted to derby-user@db.apache.org by larsk <la...@yahoo.se> on 2008/02/12 22:41:47 UTC

Get auto increment value

I am making a java-application that uses an apache derby database. One of the
fields in one of the tables has auto increment applied (GENERATED BY DEFAULT
AS IDENTITY). Is it possible to get the value that this field is going to
get or what value it got when making an insert? Or do I have to make a
select to the database to know this?
I use the following code to insert data to the database:

--------------------

Connection con = DriverManager.getConnection(connectionString);

Statement state = con.createStatement();

try
{
	state.executeUpdate(queryString);
}

catch (Exception ex)
{
	throw(ex);
}

con.close();

--------------------

If anyone has any idea on how to make this know I would appreciate any help.
-- 
View this message in context: http://www.nabble.com/Get-auto-increment-value-tp15443701p15443701.html
Sent from the Apache Derby Users mailing list archive at Nabble.com.


Re: Get auto increment value

Posted by bruehlicke <br...@gmail.com>.
     Look in the doc for   VALUES IDENTITY_VAL_LOCAL()     this function
gives you the latest ID generated.

B-)


On Feb 12, 2008 3:41 PM, larsk <la...@yahoo.se> wrote:

>
> I am making a java-application that uses an apache derby database. One of
> the
> fields in one of the tables has auto increment applied (GENERATED BY
> DEFAULT
> AS IDENTITY). Is it possible to get the value that this field is going to
> get or what value it got when making an insert? Or do I have to make a
> select to the database to know this?
> I use the following code to insert data to the database:
>
> --------------------
>
> Connection con = DriverManager.getConnection(connectionString);
>
> Statement state = con.createStatement();
>
> try
> {
>        state.executeUpdate(queryString);
> }
>
> catch (Exception ex)
> {
>        throw(ex);
> }
>
> con.close();
>
> --------------------
>
> If anyone has any idea on how to make this know I would appreciate any
> help.
> --
> View this message in context:
> http://www.nabble.com/Get-auto-increment-value-tp15443701p15443701.html
> Sent from the Apache Derby Users mailing list archive at Nabble.com.
>
>

Re: Get auto increment value

Posted by larsk <la...@yahoo.se>.
Thanks a lot! :)


Peter Yuill wrote:
> 
> Oops,
> 
> try statement.executeUpdate("some query",
> Statement.RETURN_GENERATED_KEYS)
> 
> 
> ----- Original message -----
> From: "Peter Yuill" <py...@objectix.com.au>
> To: "Derby Discussion" <de...@db.apache.org>,
> derby-user@db.apache.org
> Date: Wed, 13 Feb 2008 08:52:28 +1100
> Subject: Re: Get auto increment value
> 
> The standard way to do it is to use the alternate execute eg
> statement.executeUpdate("some query", true);
> and then
> statement.getGeneratedKeys()
> 
> Cheers,
> Peter
> ----- Original message -----
> From: "larsk" <la...@yahoo.se>
> To: derby-user@db.apache.org
> Date: Tue, 12 Feb 2008 13:41:47 -0800 (PST)
> Subject: Get auto increment value
> 
> 
> I am making a java-application that uses an apache derby database. One
> of the
> fields in one of the tables has auto increment applied (GENERATED BY
> DEFAULT
> AS IDENTITY). Is it possible to get the value that this field is going
> to
> get or what value it got when making an insert? Or do I have to make a
> select to the database to know this?
> I use the following code to insert data to the database:
> 
> --------------------
> 
> Connection con = DriverManager.getConnection(connectionString);
> 
> Statement state = con.createStatement();
> 
> try
> {
> 	state.executeUpdate(queryString);
> }
> 
> catch (Exception ex)
> {
> 	throw(ex);
> }
> 
> con.close();
> 
> --------------------
> 
> If anyone has any idea on how to make this know I would appreciate any
> help.
> -- 
> View this message in context:
> http://www.nabble.com/Get-auto-increment-value-tp15443701p15443701.html
> Sent from the Apache Derby Users mailing list archive at Nabble.com.
> 
> 
> 

-- 
View this message in context: http://www.nabble.com/Get-auto-increment-value-tp15443701p15461796.html
Sent from the Apache Derby Users mailing list archive at Nabble.com.


Re: Get auto increment value

Posted by Peter Yuill <py...@objectix.com.au>.
I think my previous post might have been misleading. The code below is
only getting the int value of the RETURN_GENERATED_KEYS constant. 

>             s.execute("INSERT INTO " + testTable1 + " (NAME)
> VALUES('xyz')");
>             int keyVal = Statement.RETURN_GENERATED_KEYS;
>             System.out.println("KEYVAL = "+keyVal);

The code needs to look like this:

s.execute("INSERT INTO " + testTable1 + " (NAME) VALUES('xyz')",
Statement.RETURN_GENERATED_KEYS);
ResultSet keys = s.getGeneratedKeys();
if (keys.next()) {
  System.out.println("KEYVAL = " + keys.getInt(1));
}

Re: Get auto increment value

Posted by bruehlicke <br...@gmail.com>.
Nice!!!!!  did not know that

            s.execute("INSERT INTO " + testTable1 + " (NAME)
VALUES('xyz')");
            int keyVal = Statement.RETURN_GENERATED_KEYS;
            System.out.println("KEYVAL = "+keyVal);

works perfect.

B-)

On Feb 12, 2008 3:57 PM, Peter Yuill <py...@objectix.com.au> wrote:

> Oops,
>
> try statement.executeUpdate("some query",
> Statement.RETURN_GENERATED_KEYS)
>
>
> ----- Original message -----
> From: "Peter Yuill" <py...@objectix.com.au>
> To: "Derby Discussion" <de...@db.apache.org>,
> derby-user@db.apache.org
> Date: Wed, 13 Feb 2008 08:52:28 +1100
> Subject: Re: Get auto increment value
>
> The standard way to do it is to use the alternate execute eg
> statement.executeUpdate("some query", true);
> and then
> statement.getGeneratedKeys()
>
> Cheers,
> Peter
> ----- Original message -----
> From: "larsk" <la...@yahoo.se>
> To: derby-user@db.apache.org
> Date: Tue, 12 Feb 2008 13:41:47 -0800 (PST)
> Subject: Get auto increment value
>
>
> I am making a java-application that uses an apache derby database. One
> of the
> fields in one of the tables has auto increment applied (GENERATED BY
> DEFAULT
> AS IDENTITY). Is it possible to get the value that this field is going
> to
> get or what value it got when making an insert? Or do I have to make a
> select to the database to know this?
> I use the following code to insert data to the database:
>
> --------------------
>
> Connection con = DriverManager.getConnection(connectionString);
>
> Statement state = con.createStatement();
>
> try
> {
>        state.executeUpdate(queryString);
> }
>
> catch (Exception ex)
> {
>        throw(ex);
> }
>
> con.close();
>
> --------------------
>
> If anyone has any idea on how to make this know I would appreciate any
> help.
> --
> View this message in context:
> http://www.nabble.com/Get-auto-increment-value-tp15443701p15443701.html
> Sent from the Apache Derby Users mailing list archive at Nabble.com.
>
>

Re: Get auto increment value

Posted by Peter Yuill <py...@objectix.com.au>.
Oops,

try statement.executeUpdate("some query",
Statement.RETURN_GENERATED_KEYS)


----- Original message -----
From: "Peter Yuill" <py...@objectix.com.au>
To: "Derby Discussion" <de...@db.apache.org>,
derby-user@db.apache.org
Date: Wed, 13 Feb 2008 08:52:28 +1100
Subject: Re: Get auto increment value

The standard way to do it is to use the alternate execute eg
statement.executeUpdate("some query", true);
and then
statement.getGeneratedKeys()

Cheers,
Peter
----- Original message -----
From: "larsk" <la...@yahoo.se>
To: derby-user@db.apache.org
Date: Tue, 12 Feb 2008 13:41:47 -0800 (PST)
Subject: Get auto increment value


I am making a java-application that uses an apache derby database. One
of the
fields in one of the tables has auto increment applied (GENERATED BY
DEFAULT
AS IDENTITY). Is it possible to get the value that this field is going
to
get or what value it got when making an insert? Or do I have to make a
select to the database to know this?
I use the following code to insert data to the database:

--------------------

Connection con = DriverManager.getConnection(connectionString);

Statement state = con.createStatement();

try
{
	state.executeUpdate(queryString);
}

catch (Exception ex)
{
	throw(ex);
}

con.close();

--------------------

If anyone has any idea on how to make this know I would appreciate any
help.
-- 
View this message in context:
http://www.nabble.com/Get-auto-increment-value-tp15443701p15443701.html
Sent from the Apache Derby Users mailing list archive at Nabble.com.


Re: Get auto increment value

Posted by Peter Yuill <py...@objectix.com.au>.
The standard way to do it is to use the alternate execute eg
statement.executeUpdate("some query", true);
and then
statement.getGeneratedKeys()

Cheers,
Peter
----- Original message -----
From: "larsk" <la...@yahoo.se>
To: derby-user@db.apache.org
Date: Tue, 12 Feb 2008 13:41:47 -0800 (PST)
Subject: Get auto increment value


I am making a java-application that uses an apache derby database. One
of the
fields in one of the tables has auto increment applied (GENERATED BY
DEFAULT
AS IDENTITY). Is it possible to get the value that this field is going
to
get or what value it got when making an insert? Or do I have to make a
select to the database to know this?
I use the following code to insert data to the database:

--------------------

Connection con = DriverManager.getConnection(connectionString);

Statement state = con.createStatement();

try
{
	state.executeUpdate(queryString);
}

catch (Exception ex)
{
	throw(ex);
}

con.close();

--------------------

If anyone has any idea on how to make this know I would appreciate any
help.
-- 
View this message in context:
http://www.nabble.com/Get-auto-increment-value-tp15443701p15443701.html
Sent from the Apache Derby Users mailing list archive at Nabble.com.