This week's book giveaway is in the OCAJP forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide 1Z0-808 and have Jeanne Boyarsky & Scott Selikoff on-line! See this thread for details.
Originally posted by sree lekha: ..i need to find if my resultSet has got records or not.
As krishnamoorthy said, you can check the next() method of resultset object which gets you inside if the resultset stores some rows fetched out of the query execution.
The resultset object works in a way that it has got a pointer before the very first row of the results fetched. Whenever you want to start iterating (looping it over), you have to invoke the next() on the resultset object, which intern increments the pointer to point to the next row. For the first time, it would point to the very first-and-beginning row.
Once all the rows are iterated (processed) (means,when the pointer is in last row), the next() method returns false as there are NO more rows to process.
..how do i find the size of my resultSet after the query is executed.
What exactly do you mean the size of resultset? Did you mean to ask the number of rows or columns(count) the resultset contains? If that case, there is no direct method provided for the same. But indirectly you can use something called ResultSetMetaData which has a method getColumnCount().
The best way to count the records is to process them and count the number of processed records. If you do not actually need to process the records, it is much better for performance to formulate the SQL query so that it returns just the count and nothing else.
Java has excellent classes, eg. ArrayList, that will grow to accommodate all data you need. You do not have to bother to get the size first, allocate the array and then fill it. Not only the code would be unnecessarily more complex if you do, but, depending on your database and/or driver, the performance might actually be a lot worse than starting with the default-sized ArrayList and having it grow.