0

I have a class in which i am getting resultset from the database:

public ResultSet GetDataFromDB() {
    ResultSet resultset = null;
    try {   
        DriverManager.registerDriver(new oracle.jdbc.driver.OracleDriver());   
        System.out.println("Connecting to the database...");   
        Connection connection = DriverManager.getConnection(
                "jdbc:oracle:thin:@host:1521:DBname", "user123","pass123");
        Statement statement = connection.createStatement();   
        resultset = statement.executeQuery("select * from tablename"); 

        while (resultset.next()) {
            System.out.println(resultset.getInt(1) + " " +
            resultset.getInt(2) + " " + 
            resultset.getInt(3) + " " + 
            resultset.getString(4));
        }
        // statement.close();   
        //connection.close(); 
    } catch (Exception e) {   
        System.out.println("The exception raised is:" + e);   
    }

    return resultset;
} 

In this classs i am able to print the data which i am getting in resultset. but when i tried to get this resultset in another class:

Classname obj= new Classname();
ResultSet tempResultSet = obj.GetDataFromDB();
System.out.println("Records Exist "+tempResultSet.next()); <-----false

I am not getting any data here. also there is no datatable here in java like in .net so that i can use that...Please concern thanks

1 Answer 1

2
while (resultset.next()) {

You already read all of the data from the ResultSet in this loop.

ResultSet is a single-use, forward-only view of the data; you can only iterate it once.

3
  • thanks.. thats a sort of weird that we can use it only once :)
    – Aquarius24
    Jun 11, 2013 at 4:51
  • 1
    @Aquarius24: No; it's not weird. ResultSet does not store the results in memory; instead, it streams them directly from the database. Otherwise, it would consume large amounts of memory.
    – SLaks
    Jun 11, 2013 at 13:35
  • Ok but there should be a "datatable" sort of component so that we can use it anytime and n number of times.
    – Aquarius24
    Jun 12, 2013 at 3:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.