javaoracle-databaseresultsetojdbc

Why 'ResultSet.next()' is return false?


Using Windows 11, OracleDB, IntelliJ, Lombok, Java

I'd like to know why rs.next() returns false in the following code and while() statement doesn't run.

//MemberDAO.java
import java.sql.*;
import java.util.*;

public class MemberDAO {
    private Connection connection;
    private String dbusername = "myuser";
    private String dbpassword = "1234";

    public List<MemberDTO> listMembers() {
        List<MemberDTO> members = new ArrayList<MemberDTO>();

        try {
            connectDB();

            String sql = "SELECT * FROM member ";
            System.out.println("query: " + sql);

            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            ResultSet rs = preparedStatement.executeQuery();

            while(rs.next()) {    //In here re.next() return false
                System.out.println("rs running");
                String id = rs.getString("id");
                String password = rs.getString("password");
                String name = rs.getString("name");
                String email = rs.getString("email");
                Date joinDate = rs.getDate("joinDate");

                MemberDTO memberDTO = new MemberDTO();
                memberDTO.setId(id);
                memberDTO.setPassword(password);
                memberDTO.setName(name);
                memberDTO.setEmail(email);
                memberDTO.setJoinDate(joinDate);
                members.add(memberDTO);
            }

            rs.close();
            preparedStatement.close();
            connection.close();
        }
        catch (SQLException e) {
            e.printStackTrace();
        }

        return members;
    }

    private void connectDB() {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            connection = DriverManager.getConnection(
                    "jdbc:oracle:thin:@localhost:1521/xe",
                    dbusername,
                    dbpassword
            );
            System.out.println("Connected to database");
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}
//MemberDTO.java
import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;

import java.sql.Date;

@NoArgsConstructor
@Getter
@Setter
public class MemberDTO {
    private String id;
    private String name;
    private String password;
    private String email;
    private Date joinDate;
}

Two records exist in Oracle DB.enter image description here

But rs.next() return false and while() doesn't run. enter image description here

I want to know why rs.next() returns false and how can I get while() to work.


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 from another session (i.e. in Java) then make sure you have issued a COMMIT command in the SQL client where you INSERTed the data (i.e. SQL*Plus).

    Note: even if you connect as the same user, this will create a separate session and you will not be able to see the uncommitted data in the other session.

    From the COMMIT documentation:

    Until you commit a transaction:

    • You can see any changes you have made during the transaction by querying the modified tables, but other users cannot see the changes. After you commit the transaction, the changes are visible to other users' statements that execute after the commit.
    • You can roll back (undo) any changes made during the transaction with the ROLLBACK statement (see ROLLBACK).