0

Please,I am working on an application where I need to select a table from the database i.e, the tables must be available all the time on that panel of selection. I created a method to show all available tables on a database and the method is present in my constructor. Now,only one of the tables is showing where required and i tried to set the tables to jTextFieds but still, one of the tables is showing. Please how do i resolve this to show all available tables to designated textfields and from there and i shall work on setting them to a jCombobox. Bellow is my snippet:

public void combo(){
       try {

               String sql="SHOW TABLES FROM cctdba";
                //SQL for selecting the table cctdba Database  

                 pst = conn.prepareStatement(sql);

               rs=pst.executeQuery(sql);
                while (rs.next()) {
                     jTextField1.setText(rs.getString(1));
                     jTextField2.setText(rs.getString(2));
                    //Displaying the first two tables into the textfield(Unresloved)  

                }

        // TODO add your handling code here:
    } 
    catch(Exception e){

    }
}

Please I need help. Thanks

Gstuntz
  • 434
  • 4
  • 10

4 Answers4

0

i think "select * from information_schema.tables" will be fine for you

Matteo Gatto
  • 616
  • 11
  • 28
0

Your problem is that you are setting and resetting the text field. You either need to add more text fields for each result, or you need to build a concatenated string.

           rs=pst.executeQuery(sql);
           string allNames1;
           string allNames2;
            while (rs.next()) {
                 allNames1 += rs.getString(1) +" ";
                 allNames2 += rs.getString(2) +" ";
            }
            jTextField1.setText(allNames1);
            jTextField2.setText(allNames2);
Bill Gregg
  • 7,067
  • 2
  • 22
  • 39
0
SELECT TABLE_NAME 
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_TYPE = 'BASE TABLE' AND TABLE_SCHEMA='cctdba'
Neophile
  • 1,519
  • 12
  • 15
0

I think you should use a list as data model to load all the available tables that you have.

Try this:

 int i = 1;
 List<String> data = new Arraylist<>();
 while (rs.next()) {
     data.add(rs.getString(i));
     i++;
 }
 JList<String> myJList = new JList<String>(data);

and then use the myJList object in your panel.

aUserHimself
  • 1,589
  • 2
  • 17
  • 26