Reputation: 19
I am not able to display the last row of my database in jsp. I already tried to print rs.getString(1)
, but it does not work.
ResultSet rs = st.executeQuery("Select MAX(CustomerID) from Customer");
while(rs.next()){out.print(rs.getString(1));}
Upvotes: 1
Views: 1145
Reputation: 106
I don't know if this is what you want but if you are trying to get the whole row there are some ways to accomplish that
ResultSet rs = st.executeQuery("select max(customerid) as id from customer");
rs.next();
String id = rs.getString("id");
rs = st.executeQuery("select field_a, field_b from customer where customerid = " + id);
rs.next();
String row = id + "," + rs.getString("field_a") + "," + rs.getString("field_b");
out.println(row);
Of course you need to replace the field_a
and field_b
columns with the ones in your customer
table and add or remove columns according to your needs.
The shorter way is using order by
and limit
keywords like this
ResultSet rs = st.executeQuery("select customerid, field_a, field_b from customer order by customerid desc limit 1");
rs.next();
String row = rs.getString("customerid") + "," + rs.getString("field_a") + "," + rs.getString("field_b");
out.println(row);
Be secure of add a primary key
or unique
constraint to the customerid
column for improve the performance of the both methods.
Upvotes: 1