This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
I tried to execute following code,i got problem: Statement stmt; ResultSet rs = stmt.executeQuery("select *......"); my question is: how can i check whether any record return or not any record return. i check the api, it says resultset never be null value, thanks
The ResultSet isn't pointing to a row when it is created. You have to do a rs.next() to get the first row. The rs.next() will return false if there are no more rows to get. So if nothing is returned, the first rs.next() will return false.
Paul, Don't you have to do a rs.next() anyway at some point to get your data? If you don't need the data (and only want to know if the query returns results) try doing: select count(*) from ... This should be faster because there is less network activity. If you do need all the fields, you need to optimize your query or your network.
thanks for your reply earlier. yes. I don't need any value from the table. I just check whether the record exist or not. I was trying to use 'select count(*),,, " as well as you said. It's still too slow down. Someone said that the resultset have to bring all row and return the value or no of row. Thus, we don't have a solution to reduce the performance through jdbc. Is that right? Urgent, i need to solve this issue if possible. please help me. Actually, this situation is to read all records from transaction table which has 50,000 and chain to code table whether the transaction record exist in code table or not. if the code table has 10,000 row, this will be happen to 50,000 * 10,000 times. thus, it could be problem. Am I right to explain? I have to figure out this issue as soon as possible. if anyone can provide a good solution. It would be really appreciated. thanks, paul
author & internet detective
Paul, Since you don't need the data, there are two strategies for reducing the response time: 1) Faster query - Add indexes if you don't have them already. If at all possible, put an index on the columns you are joining. 2) Return less data - If count(*) is too slow, see if you can do count(fieldWithIndex). If the database can stay within the index, it should speed up the query. Also someone showed me a neat trick where you can return just the first row of a select * query. It is database specific and is slightly faster than the aggregate functions. "Someone said that the resultset have to bring all row and return the value or no of row. Thus, we don't have a solution to reduce the performance through jdbc. Is that right?" That is correct. As mentioned above, you need to tune the query and the amount of data on the network.