Getting Data from a Result Set

A result set contains the results of a SQL query. The results are kept in a set of rows, one of which is designated the current row. A row must be made current before data can be retrieved from it. The result set maintains a reference to the current row called the cursor.

The cursor is positioned before the first row when a result set is created. When a result set's next() method is called, the cursor moves to the first row of the result set and that row becomes the current row.

There are two ways to retrieve the data from the current row. The first uses a column index starting from 1. The second uses a column name. For example, with the query `\cv{SELECT col1, col2 FROM table}', the value for col2 can be retrieved using a column index of 2 or with the column name col2. This example demonstrates both methods.

try {
    // Create a result set containing all data from my_table
    Statement stmt = connection.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT * FROM my_table");

    // 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);

        // Get the data from the row using the column name
        s = rs.getString("col_string");
    }
} catch (SQLException e) {
}
Here is another example of retrieving data from a result that uses the various getXXX() methods. This example uses the table created in Creating a MySQL Table to Store Java Types.
try {
    // Create a result set containing all data from mysql_all_table
    Statement stmt = connection.createStatement();
    ResultSet rs = stmt.executeQuery("SELECT * FROM mysql_all_table");

    // Fetch each row from the result set
    while (rs.next()) {
        boolean bool = rs.getBoolean("col_boolean");
        byte b = rs.getByte("col_byte");
        short s = rs.getShort("col_short");
        int i = rs.getInt("col_int");
        long l = rs.getLong("col_long");
        float f = rs.getFloat("col_float");
        double d = rs.getDouble("col_double");
        BigDecimal bd = rs.getBigDecimal("col_bigdecimal");
        String str = rs.getString("col_string");
        Date date = rs.getDate("col_date");
        Time t = rs.getTime("col_time");
        Timestamp ts = rs.getTimestamp("col_timestamp");
        InputStream ais = rs.getAsciiStream("col_asciistream");
        InputStream bis = rs.getBinaryStream("col_binarystream");
        Blob blob = rs.getBlob("col_blob");
    }
} catch (SQLException e) {
}

Comments

19 Jan 2010 - 3:15am by Ivan (not verified)

I guess your example doesn't work too good. Having that while(rs.next()) there, I suppose the first thing the algoritm does is to make the next(), skipping the first element of the result set. Isn't that correct?

21 Feb 2010 - 5:24pm by hagar54 (not verified)

Spot on example, thank you very much. I couldn't find a more concise or accurate example anywhere.

27 Mar 2010 - 12:46pm by Matt (not verified)

Hi i keep getting the following error: ERROR OCCURED!java.sql.SQLException: Before start of result set

the code i am executing is:

public void loginQuery(String username,String password)
{
this.password = password;

Statement stmt;
try {
stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("SELECT * FROM PersonCustomer");
String s = rs.getString(1);
s = rs.getString("col_string");
error = s;
} catch (SQLException e) {
error = "ERROR OCCURED!" + e.toString();
e.printStackTrace();
}

27 Mar 2010 - 3:22pm by md (not verified)

As said by above, concise and accurate, thank you very much!
@Matt: Read the documentation: "the cursor is positioned _before_ the first row"
You _have_ to move the cursor of the ResulSet to the next row.

1 Jun 2010 - 7:22am by Valter Henrique (not verified)

thank you dude, you really help me a lot man, god bless you.

3 Jun 2010 - 3:04pm by Anonymous2 (not verified)

The best message is the one that Anonymous posted @May 23. Very helpful :D Thanks dude! You helped me a lot

27 Jun 2010 - 11:05pm by sriram (not verified)

i have crated table registration with the 5 fields . But i want to select only two fields how can it possible if there is any code send to my emaid is sriram1214@gmail.com plz its urgent.........

3 Aug 2011 - 5:21am by Ganesh (not verified)

Very useful thanks,,,,,

8 Aug 2011 - 11:18am by Anonymous (not verified)

@Sriram: U can use the query as
Select column1,column2 from registration;

10 Oct 2011 - 3:39am by Anonymous (not verified)

good work

31 Oct 2011 - 11:49am by Anonymous (not verified)

i want to generate a new id by using recently added id.(by adding 1 to the recent id)
code segment i've used is >>>
===================================

DBConnect dBConnect = new DBConnect();
Connection connection = dBConnect.connect();

try{
Statement stmt = connection.createStatement();
ResultSet rs = stmt.executeQuery("select max(cus_id) from customer");

while (rs.next())
{

String s = rs.getString("cus_id"); }

txtCid.setText(""+(s+1));

}catch(SQLException ee){

}
================================
what is wrong with this code segment?
can any one help plzzzz....???????

5 Nov 2011 - 7:36am by Anonymous (not verified)

@Above

I think it is because you are treating your string as an integer.
You should use convert it to int first and then back to string.

15 Nov 2011 - 2:50am by Anonymous (not verified)

asdfghjklqwertyuiopzxcvbnm

8 Dec 2011 - 9:02am by Anonymous (not verified)

This blog is really helpful, thanks. :)

13 Dec 2011 - 9:36pm by saravana (not verified)

nice one

21 Dec 2011 - 7:09am by Anonymous (not verified)

Good example..

26 Jan 2012 - 2:02am by Rohit raj (not verified)

Excellent coding.....

30 Apr 2012 - 6:49am by Anonymous (not verified)

ResultSet rs = stmt.executeQuery("select max(cus_id) from customer");
while (rs.next())
{
String s = rs.getString("cus_id"); }

++++++++++++
Give it a column alias "select max(cus_id) cus_id from customer"

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.