简体   繁体   中英

Type mismatch: cannot convert from element type Object to String JDBC MetaData

My Code

public static ArrayList getTablesMetadata() throws SQLException{
    String table[] = {"TABLE"};
    ResultSet rs = null;
    ArrayList tables = null;

    rs = metadata.getTables(null, null, null, table);
    tables = new ArrayList();
    while(rs.next()){
        tables.add(rs.getString("TABLE_NAME"));
    }
    return tables;
}

public static void getColumnsMetadata(ArrayList tables)
        throws SQLException{
    ResultSet rs = null;

    for(String actualTable : tables){  <-----------------------------------
        rs = metadata.getColumns(null, null, actualTable, null);
        System.out.println(actualTable.toUpperCase());
        while(rs.next()){
            System.out.println(rs.getString("COLUMN_NAME")+ " "
                    + rs.getString("TYPE_NAME")+ " "
                    + rs.getString("COLUMN_SIZE"));
        }
        System.out.println("\n");
    }
}

Error is pointed out with the arrow, what im getting is:

Type mismatch: cannot convert from element type Object to String

I have tried adding the to certain parts but think im doing it wrong, or something is wrong in the code that I cant seem to see.

ArrayList uses Generics .

Your table variable is a list of Object since the generic type was not specified. You can:

  1. Cast each table object to String
  2. Use ArrayList<String> instead (recommended)

If you don't specify the array list type, it is assumed to hold Object.
try

getColumnsMetadata(ArrayList<String> tables) 

I guess you'll also need to change:

tables = new ArrayList<String>(); 

and change the return value of getTablesMetadata() to

 ArrayList<String>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM