1

I installed OracleDB(XE - free version) and I tried to use it in Java. I have this code:

       static final String DB_URL = "jdbc:oracle:thin:@localhost:1521/XE";
       static final String USER = "AdministratorPDB1";
       static final String PASS = "admin.admin";
       static final String QUERY = "SELECT * FROM TestTable";

       public static void main(String[] args) {

          try(Connection conn = DriverManager.getConnection(DB_URL, USER, PASS);
             Statement stmt = conn.createStatement();
             ResultSet rs = stmt.executeQuery(QUERY);
          ) {             
             while(rs.next()){
                //Display values
                System.out.print(rs.getString("fam_name"));
                System.out.print("          ");
                System.out.print(rs.getString("giv_name"));
                System.out.print("          ");
                System.out.print(rs.getInt("age"));
                System.out.print("          ");
                System.out.print(rs.getString("email") + "\n");
             }
          } catch (SQLException e) {
             e.printStackTrace();
          } 
       }

For some reason, the program seems to print just the first row from the table. I can confirm the fact that there are more entries in that table if I open Windows Command Prompt, connect using sqlplus command, and run the SELECT * FROM TestTable;, query, all three rows are printed in the command line.

I also tried to loop over the rows like this:

ResultSetMetaData metaData = resultSet.getMetaData();
int numCols = metaData.getColumnCount();

while (resultSet.next()) {
    for (int i = 1; i <= numCols; i++) {
        String colName = metaData.getColumnName(i);
        String colValue = resultSet.getString(i);
        System.out.println(colName + ": " + colValue);
    }
    System.out.println();
}

The same problem. Just the first entry is displayed. It seems to have something to do with resultSet.next().

Now, I know I might have not given enough information for a solve, but this is all I got. This is a hard problem to debug.

OracleDB version: 21c
Java version: jdk 1.8.0
3
  • "This is a hard problem to debug" -- I don't think it is, but have you done any debugging? Like, actually using a debugger?
    – mustaccio
    Commented Sep 13, 2023 at 19:42
  • WDYM? What debugging I can do. I just call a method from a library, how should I get low-level access to what's happeing behind the scenes with the .next() method? Commented Sep 13, 2023 at 19:46
  • 1
    Fire up your favourite debugger, step into next(), and enjoy the magic show. However, I suspect that, whatever is happening, is happening outside next().
    – mustaccio
    Commented Sep 13, 2023 at 19:54

0

Your Answer

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

Browse other questions tagged or ask your own question.