0

i am useing the following code to get only list of mysql tables and add them to the JComboBox. but what I get is only "[]" in the JComboBox! please help me with it

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

public ArrayList<String> tablesArray() throws SQLException
{


    ResultSet result = connecting.getMetaData().getCatalogs();

    while(result.next())
    {
        tables.add(result.getString(0));
    }

    return tables;

}

public JComboBox comboBox()
{


    JComboBox<ArrayList> combo = new JComboBox<ArrayList>();

    combo.addItem(tables);
    combo.setBounds(130,30,190,30);

    return combo;
}
mKorbel
  • 109,525
  • 20
  • 134
  • 319
Rafa
  • 467
  • 4
  • 18
  • Try printing result.getString(0) by appending System.out.println(result.getString(0)) inside the while loop to see if resultset is not empty. – rahulserver Jul 17 '13 at 06:56
  • unrelated: don't do any manual sizing/locating of components, ever - instead use a suitable LayoutManager – kleopatra Jul 17 '13 at 08:30

5 Answers5

2

ArrayList tables = new ArrayList();

is array that contains one or more Items

combo.addItem(tables);

later you added a.m. array as Item, then output could be correct

  • pass array as constructor, more see in JComboBox API Constructor Summary

  • better to create an DefaultComboBoxModel (see a.m. API) and add those Items from ResultSet to model directly

mKorbel
  • 109,525
  • 20
  • 134
  • 319
0

Try the following

while(result.next())
{
    tables.add(result.getString(1));
}

See this question for more information: How to check if a particular database in mysql already exists using java or this one how to get list of Databases "Schema" names of MySql using java JDBC

However I am a bit confused are you looking for database names? Or are you trying to find table names?

When using MySQL you can always query the information_Schema views for LOADS of information regarding the tables, databases columns etc.

Community
  • 1
  • 1
Namphibian
  • 12,046
  • 7
  • 46
  • 76
0

The getCatalogs() method returns catalog names, not table names. I'm not sure what it will do on MySQL, since AFAIK MySQL does not have catalogs.

If you call DatabaseMetaData.getTables(...) and use null for the first param, catalog, it should give you all table names without narrowing the result to any catalog.

mthmulders
  • 9,483
  • 4
  • 37
  • 54
0

you should try this

ResultSetMetaData meta = (ResultSetMetaData) rs.getMetaData();
int columns = meta.getColumnCount();
while(rs.next()){
for(int i =1; i<=columns; i++){
String yourValues = rs.getString(i);
theCombo.addItem(yourValues);
//rs is the result set for the executed query.
}

keneth
  • 197
  • 3
  • 4
  • this code works pretty good but it shows only list of available databases not tables in a specific database! – Rafa Jul 20 '13 at 19:57
  • this code works pretty good but it shows only list of available databases not tables in a specific database! only changing the getCatalogs() function to getTables(null, null, null, new String[] {"TABLE"}) fixes the problem – Rafa Jul 20 '13 at 20:04
0

Here is a complete code to retrieve all tables of a database into JComboBox:

public JComboBox<String> comboBox() throws SQLException
{
    ResultSet rs = connecting.getMetaData().getTables(null, null, "%", null);

    ResultSetMetaData meta = (ResultSetMetaData) rs.getMetaData();

    int columns = meta.getColumnCount();

    while(rs.next())
    {

            String table_names = rs.getString("TABLE_NAME");
            combo.addItem(table_names);
    }

    combo.setBounds(130,30,190,30);
    combo.setSelectedIndex(0);
    combo.setMaximumRowCount(5);
    return combo;
}
rachana
  • 3,344
  • 7
  • 30
  • 49
Rafa
  • 467
  • 4
  • 18