Java School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!

Welcome to Dream.In.Code
Become a Java Expert!

Join 300,477 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,750 people online right now. Registration is fast and FREE... Join Now!




More efficient way of accessing a ResultSet

 

More efficient way of accessing a ResultSet

javamad

2 Jul, 2009 - 02:09 AM
Post #1

D.I.C Head
**

Joined: 8 Jun, 2009
Posts: 65



Thanked: 1 times
My Contributions
Hi, Please see below for a section of code which works that queries a sql database for the number of rows of information in the table pair and retrurns this number as part of a ResultSet. However in order to access the result I then have to perform a while loop on the RecordSet in order to access this number. My question, is there a more efficient way to access the number of records seeing as the while loop only has to loop once (only one value is returned from the SQL query), or do I have to continue using the while loop?

Many thanks
CODE

try {

        
        SQLConn Sconn = new SQLConn(serverName, SQLPortNumber, userName,password,  databaseName, giveOutput);
        
         ResultSet rs = Sconn.executeQuery("SELECT COUNT(pair_id) FROM pair");
        
        // Fetch each row from the result set
            while (rs.next()) {
                // Get the data from the row using the column index
                String s = rs.getString(1);
                System.out.println(s);
        
                // Get the data from the row using the column name
                s = rs.getString("col_string");
            }
        } catch (SQLException e) {

        }


This post has been edited by javamad: 2 Jul, 2009 - 02:12 AM

User is offlineProfile CardPM
+Quote Post


cfoley

RE: More Efficient Way Of Accessing A ResultSet

2 Jul, 2009 - 02:43 AM
Post #2

D.I.C Addict
Group Icon

Joined: 11 Dec, 2007
Posts: 645



Thanked: 60 times
Dream Kudos: 25
My Contributions
If you know there's only one result just forget the while loop and get the result.

java
ResultSet rs = Sconn.executeQuery("SELECT COUNT(pair_id) FROM pair");
String s = rs.getString(1);
System.out.println(s);

// Get the data from the row using the column name
s = rs.getString("col_string");

User is offlineProfile CardPM
+Quote Post

macosxnerd101

RE: More Efficient Way Of Accessing A ResultSet

2 Jul, 2009 - 08:54 AM
Post #3

Self-Trained Economist
Group Icon

Joined: 27 Dec, 2008
Posts: 1,688



Thanked: 180 times
Dream Kudos: 325
Expert In: Java

My Contributions
@Javamad: The variables you have declared in the try block are local to that try. If you want to be able to use them outside that area, you have to declare them so they have greater scope.
User is offlineProfile CardPM
+Quote Post

xclite

RE: More Efficient Way Of Accessing A ResultSet

2 Jul, 2009 - 09:04 AM
Post #4

LIKE A BOSS
****

Joined: 12 May, 2009
Posts: 629



Thanked: 13 times
My Contributions
QUOTE(cfoley @ 2 Jul, 2009 - 06:43 AM) *

If you know there's only one result just forget the while loop and get the result.

java
ResultSet rs = Sconn.executeQuery("SELECT COUNT(pair_id) FROM pair");
String s = rs.getString(1);
System.out.println(s);

// Get the data from the row using the column name
s = rs.getString("col_string");



One thing about this is you have to call resultSet.next() to get to the first result. So the above will work, as long as:
CODE

rs.next();

is called before
CODE

String s = rs.getString(1);


User is offlineProfile CardPM
+Quote Post

cfoley

RE: More Efficient Way Of Accessing A ResultSet

2 Jul, 2009 - 01:25 PM
Post #5

D.I.C Addict
Group Icon

Joined: 11 Dec, 2007
Posts: 645



Thanked: 60 times
Dream Kudos: 25
My Contributions
Thanks for that addition xclite. I don't know much about connecting to databases with Java. smile.gif
User is offlineProfile CardPM
+Quote Post

pbl

RE: More Efficient Way Of Accessing A ResultSet

2 Jul, 2009 - 03:24 PM
Post #6

Java Lover
Group Icon

Joined: 6 Mar, 2008
Posts: 9,537



Thanked: 1126 times
Dream Kudos: 450
My Contributions
QUOTE(xclite @ 2 Jul, 2009 - 09:04 AM) *

QUOTE(cfoley @ 2 Jul, 2009 - 06:43 AM) *

If you know there's only one result just forget the while loop and get the result.

java
ResultSet rs = Sconn.executeQuery("SELECT COUNT(pair_id) FROM pair");
String s = rs.getString(1);
System.out.println(s);

// Get the data from the row using the column name
s = rs.getString("col_string");



One thing about this is you have to call resultSet.next() to get to the first result. So the above will work, as long as:
CODE

rs.next();

is called before
CODE

String s = rs.getString(1);


and then rs.close(); // to release resources
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic

Time is now: 11/8/09 03:33AM

Live Java Help!

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter Fan Us On Facebook

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month