Displaying stored values from a SQL statement in JAVA


I have a sql statement (other than select, update, insert, delete) which gives the total number of customers in the table.

How can I display that value in a java programme??

Do, i need to store that value in a variable ("Select total = count(*) from customers) and display that variable or what??

Thanks for your help here asap!!

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zzynxSr. Software engineerCommented:
Handling the result of the query

       select column from table

is just the same as handling the result of the query

       select count(*) from table
zzynxSr. Software engineerCommented:
int total = -1;
try {
        Statement stmt = connection.createStatement();
        ResultSet rs = stmt.executeQuery("SELECT count(*) FROM my_table");
        while (rs.next()) {
            total = rs.getInteger(1);
    } catch (SQLException e) {

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
zzynxSr. Software engineerCommented:
Now the total number of records is in the variable "total".
You can display it now whereever and however you want


JLabel myLabel1 = new JLabel("Total # records:");
JLabel myLabel2 = new JLabel();

Rowby Goren Makes an Impact on Screen and Online

Learn about longtime user Rowby Goren and his great contributions to the site. We explore his method for posing questions that are likely to yield a solution, and take a look at how his career transformed from a Hollywood writer to a website entrepreneur.

The easiest way for an arbitrary query is to do something like resultSetToTableModel here:

PCMENIACAuthor Commented:

Thanks for the solution, but can you pls tellme what does 1 refer to in "total = rs.getInteger(1);"

Also, how can we retrieve more than 1 result returned by the query

>>Also, how can we retrieve more than 1 result returned by the query

See my answer above - it works for any query
zzynxSr. Software engineerCommented:
>> what does 1 refer to in "total = rs.getInteger(1);"
To the first column of the query (in this case there's only one: count(*))

e.g. if you have this query
   select column1, column2, column3 from table

your code could look like:

  String column1 = rs.getString(1);   // get the first field of the resulting record
  int column2 = rs.getInteger(2);     // get the 2nd field of the resulting record
  String column3 = rs.getString(3);  // get the 3rd field of the resulting record
zzynxSr. Software engineerCommented:
So if your table contains

column1     column2     column3
Name1        1               Hello
Name2        2               World

during the first run of the while (rs.next()) { loop

    the 'column1' var will contain "Name1", the 'column2' var will contain 1 and the 'column3' var will contain "Hello"

during the second run of the while (rs.next()) { loop

    the 'column1' var will contain "Name2", the 'column2' var will contain 2 and the 'column3' var will contain "World"

zzynxSr. Software engineerCommented:
Each run of the loop stands for another record in your table
PCMENIACAuthor Commented:

The method is not getInteger() but getInt().

Thanks anyways for the detailed explanation as well.

zzynxSr. Software engineerCommented:
>> The method is not getInteger() but getInt().
Sorry. I typed it by heart.
Is that the reason for giving a C-grade?
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.