Search code examples
javawhile-loopmetadatateradataresultset

Storing values inside while loop using JDBC


ResultSet rs = dbmd.getSchemas(); 
while(rs.next()) {  
    String DbNames = rs.getString("TABLE_SCHEM");
}

I'm trying to store the value of DbNames and use it later. I tried using ArrayList

ResultSet rs = dbmd.getSchemas(); 
ArrayList<String> dbs = new ArrayList<>();
while(rs.next()) {
    dbs.add(rs.getString("TABLE_SCHEM"));
}
for(String[] s : dbs)
{
   System.out.println(Arrays.toString(s));
}

I'm new to programming and used StackOverflow resources to fix my problem, but I'm still having problems. Please help me figure this out.


Solution

  • Currently, your ArrayList is a raw type because you have not specified a data type for the ArrayList. Change your ArrayList declaration to a generic type by using

    ArrayList<String> dbs = new ArrayList<>();
    

    That way, when you try to access the values later, they will be String instead of Object.

    Your new code will be

    ResultSet rs = dbmd.getSchemas();
    ArrayList<String> dbs = new ArrayList<>();
    while(rs.next()) { 
        dbs.add(rs.getString("TABLE_SCHEM"));
    }
    for(String s : dbs) {
        System.out.println(s);
    }