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.
Java version: jdk 1.8.0
.next()
method?next()
, and enjoy the magic show. However, I suspect that, whatever is happening, is happening outsidenext()
.