The code below works properly and shows me a list of databases in my database. In the code given below, What is TABLE_CAT and Why is it there ?
import java.sql.*;
public class Database{
public static void main(String [] args) {
Connection con = null;
try {
con = DriverManager.getConnection("jdbc:mysql://localhost:3306","cowboy","123456");
DatabaseMetaData meta = con.getMetaData();
ResultSet res = meta.getCatalogs();
System.out.println("List of databases: ");
while (res.next()) {
System.out.println(" " + res.getString("TABLE_CAT"));
}
res.close();
con.close();
} catch (SQLException e) {
System.err.println("SQLException: " + e.getMessage());
}
}
}
TABLE_CAT is the name of the column in your resutSet. As you are iterating over your result set row by row, using
res.getString("TABLE_CAT"))allows you to extract the value from that column in the current result row. Asmeta.getCatalogs()returns catalog names available in a database, the catalog name is then stored under a column called TABLE_CAT.This should make more sense to you now.