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?
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?
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.
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
Works like a charm thank you for all the help.