Re: java.sql.SQLException: Exhausted Resultset

From: Rauf Sarwar <rs_arwar_at_hotmail.com>
Date: 22 Mar 2005 02:01:05 -0800
Message-ID: <1111485665.508806.260890_at_l41g2000cwc.googlegroups.com>


Your code is not very well written. Please read java docs and learn the functions in a given class. My comments are embedded.

Phi wrote:
> Hi all,
> This is the code which i use to test connecting to Oracle db.
> But it says "Exhausted Resultset"
>
> System.out.println("Connecting...");

Need to register the driver here as pointed out by someone else before opening a connection. See DriverManager.registerDriver

> Connection conn = DriverManager.getConnection
> ("jdbc:oracle:oci:_at_" + database, user,
password);
>
> Statement stmt = conn.createStatement();
>
> ResultSet rset = stmt.executeQuery ("SELECT id,name FROM
pmain");
>
> System.out.println(rset.findColumn("Name"));

Why do you have this above statement here? It's redundant unless you just want to print the column index. It can be used in the next System.out call to pass the column index e.g. System.out.println(rset.getString(rset.findColumn("Name"));

> rset.next();

You are not testing the return boolean value from call to next before trying to fetch from it. This is the error you are receiving that the resultset does not have any rows. Generally you put the call to next in a loop e.g.

             while(rset.next()) {
                // It ONLY gets here if there are rows
                System.out.println(rset.getString(2));
             }

OR
test the return value from rset.next()... true/false before calling rset.getxxxx();

> System.out.println(rset.getString(1));

Here, you are passing the column index as 1 but most likely you are looking for the Name value. Resultset columns go as 1, 2, 3 and so on. If you are looking for ID then use 1... use 2 for Name.

> // close the result set, the statement and connect
> rset.close();
> stmt.close();
> conn.close();
> System.out.println("Your JDBC installation is correct.");
>
> pmain has two rows.
>
> ID NAME
> ----------



> 1 Phi
> 2 Tram
> Please give a look and help me to fix this funny bug.
> Cheer,
> Phi.

Regards
/Rauf Received on Tue Mar 22 2005 - 11:01:05 CET

Original text of this message