Storing max(id).

I am want to find the max(id) from a table named customers and column Id. I am using this SQL command "SELECT max(Id) AS maxid FROM Customers". Now how do I store the maxid into a variable so that I can use it?

[217 byte] By [Antoniosa] at [2007-11-27 10:22:01]
# 1

As stated the select is returning a result set.

So you get the result set, get the first row and get the first value from the row.

Note that databases normally have other solutions for unique key ids if that is what you are attempting. And if that is what you attempting you need to consider what implications there are if there are multiple users/connections that do the same thing.

jschella at 2007-7-28 17:13:27 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 2

Consider this:

try

{

Statement s = conn.createStatement ();

ResultSet rs = s.executeQuery ("SELECT MAX(ID) FROM tblClient");

int nextID = 0;

if (rs.next ())

{

nextID = rs.getInt (1) + 1;

}

rs.close ();

}

catch (SQLException se)

{

JOptionPane.showMessageDialog (this, "Error with SQL", "Error", JOptionPane.ERROR_MESSAGE);

}

That will use the rs(ResultSet) add 1 and give you a new ID, if you were adding a new entry. So as you can see, the ResultSet can be used to get any data, by using 'get's. I'm a java baby so i hope what i'm saying makes sences and is correct, but it works for me :)

Regards

Paul

3xad3u5a at 2007-7-28 17:13:27 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...
# 3

Works like a charm thank you for all the help.

Antoniosa at 2007-7-28 17:13:27 > top of Java-index,Database Connectivity,Java Database Connectivity (JDBC)...