
How to get the aggregate value using JDBC?
Like I am executing the statement "Select Max(ItemId) from Items", and I get only one row, only one column in return and I want to get that data using JDBC. How may I do it?

Java JDBC retrieve selected value
import java.sql.*;
class Get
{
public static void main(String[] args) throws Exception
{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection("jdbc:mysql://localhost:3306/test", "root", "root");
Statement st=con.createStatement();
ResultSet rs=st.executeQuery("Select Max(id) from items");
int id=0;
if(rs.next()){
id=rs.getInt("Max(id)");
}
System.out.println(id);
ResultSet rs1=st.executeQuery("Select * from items where id="+id+"");
while(rs1.next()){
System.out.println(rs1.getString(1)+" "+rs1.getString(2)+" "+rs1.getString(3)+" "+rs1.getString(4));
}
}
}
If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.
Ask your questions, our development team will try to give answers to your questions.