Search code examples
javaoracle-databaseexceptionjdbcresultset

Getting Exausted ResultSet Error in Java jdbc oracle


This is the code for jdbc which is written in eclipse. I am just trying to store the result of the query to ResultSet rs and adding it to String name. But on execution the below code i am facing Exhausted Resultset error.

import java.sql.*;

public class DemoClass{
    public static void main(String args[]) throws Exception{
        
        String query = "SELECT name FROM Emp WHERE rollno=1";

        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection con = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:xe", "Subhro", "Kabiraj");
        
        Statement st = con.createStatement();
        ResultSet rs = st.executeQuery(query);
        rs.next();
        String name = rs.getString("name");
        
        System.out.println(name);
        
        st.close();
        con.close();
    }
}

ERROR:

Exception in thread "main" java.sql.SQLException: Exhausted Resultset
    at [email protected]/oracle.jdbc.driver.OracleResultSetImpl.getString(OracleResultSetImpl.java:1270)
    at [email protected]/oracle.jdbc.driver.OracleResultSet.getString(OracleResultSet.java:498)
    at DemoClass.main(DemoClass.java:14)

Note: my table is not empty.

TABLE created in terninal:

SQL> INSERT INTO Emp(rollno, name) values(1, 'abc');

1 row created.

SQL> INSERT INTO Emp(rollno, name) values(2, 'def');

1 row created.

SQL> INSERT INTO Emp(rollno, name) values(3, 'ghi');

1 row created.

SQL> select * from Emp
  2  ;

    ROLLNO
----------
NAME
--------------------------------------------------------------------------------
     1
abc

     2
def

     3
ghi


SQL> SELECT name FROM Emp WHERE rollno=1;

NAME
--------------------------------------------------------------------------------
abc

Any suggestions how to get rid of this error.


Solution

  • UnCOMMITted data is only visible within the session that created it (and will ROLLBACK at the end of the session if it has not been COMMITted). If you can't see the data then make sure you have issued a COMMIT command in the SQL client.

    If you have issued a COMMIT and still can't see the data then make sure that both the SQL Client and the JDBC program are connecting to the same database and are querying the same user's schema of that database.