0

I want to get all table name which is in database and display it in JTable.

It is printing:

con =  DriverManager.getConnection("jdbc:h2:C:/SimpleGST/GST/INVOICES","sa","");
String[] types = {"TABLE"};
DatabaseMetaData metadata = con.getMetaData();
ResultSet resultSet = metadata.getTables(null, null, "%", types);
while (resultSet.next()) {
    String tableName = resultSet.getString(3);
    System.out.println(tableName);
    table.setModel(net.proteanit.sql.DbUtils.resultSetToTableModel(resultSet));
}

It is printing the table names but it's not displaying in the table.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
Karan Malhotra
  • 125
  • 3
  • 11
  • 1) For better help sooner, post a [MCVE] or [Short, Self Contained, Correct Example](http://www.sscce.org/). 2) Use a logical and consistent form of indenting code lines and blocks. The indentation is intended to make the flow of the code easier to follow! 3) A single blank line of white space in source code is all that is *ever* needed. Blank lines after `{` or before `}` are also typically redundant. – Andrew Thompson Sep 02 '18 at 01:03

2 Answers2

1
while (resultSet.next()) 
{
    String tableName = resultSet.getString(3);
    System.out.println(tableName);
    table.setModel(net.proteanit.sql.DbUtils.resultSetToTableModel(resultSet));
}

You have a while loop that keeps replacing the model with every row of data you read. So all the data from the ResultSet has been read the last time you try to set the model.

The point of using the resultSetToTableModel(...) method is that is will read all the data from the ResultSet for you and create the TableModel. So there is no need for the while loop.

You need to replace the above code with a single line of code:

table.setModel(net.proteanit.sql.DbUtils.resultSetToTableModel(resultSet));

Edit:

but it show too many other columns .. i just want to display tables

Then you need to create the table model manually. The code would be something like:

Vector columnNames = new Vector();
columnNames.addElement("Table");

DefaultTableModel model = new DefaultTableModel(columnNames, 0);

while (resultSet.next()) 
{
    Vector row = new Vector();
    row.addElement( resultSet.getString(3) );
    model.addRow(row);
}

table = new JTable( model );
scrollPane.setViewportView( table );
TT.
  • 15,774
  • 6
  • 47
  • 88
camickr
  • 321,443
  • 19
  • 166
  • 288
  • Then the problem is in another part of your code. Maybe you don't add the table to a scrollPane and the scrollpane to the frame. Did you add any debug code to display the row count and column count of the JTable to make sure the model was created correctly? – camickr Sep 01 '18 at 15:27
  • i just set the scrollpane to setViewportView and now its working in loop. – Karan Malhotra Sep 01 '18 at 15:28
  • but it show too many other columns .. i just want to display tables – Karan Malhotra Sep 01 '18 at 15:29
  • i am creating a billing software.. so need to save bills .. so i was doing that create table for each bill and save the data to it..... if you have better idea.. then welcome – Karan Malhotra Sep 01 '18 at 15:57
  • Once again I repeat - did you do any debugging??? Did you display the data as you add it to the model? Did you display the row/column count of the table? Do you see the column headers? You should at least see the column headers since you hard coded those values. If you don't then you didn't add the scrollpane to the frame or you didn't revalidate() and repaint() the frame after you added the scrollpane to the frame. – camickr Sep 02 '18 at 00:47
  • yes. i debugged and it is working fine . when i print.... but it is not displaying in table – Karan Malhotra Sep 02 '18 at 02:46
  • . If the table contains the data, then you haven't added the table/scrollpane to the frame properly. I don't know if you added the scrollpane to the frame when you created the GUI and are just trying to change the view of the scrollpane or whether you are dynamically trying to add a new scrollpane to the frame. Post your [mcve] that demonstrates the problem. – camickr Sep 02 '18 at 03:25
  • i added the table to scrollpane and scroll pane to frame... and it also show the table names in table but it also show other columns. and i just want to show table names – Karan Malhotra Sep 02 '18 at 03:39
  • it show other columns like TABLE_CAT, TABLE_TYPE, TABLE_SCHEMA, but all i want is to display TABLE_NAME column – Karan Malhotra Sep 02 '18 at 03:43
  • `but it also show other columns.` - I told you to forget about using the DbUtils class because it will display all the columns returned in the result set. In my edit I showed you how to define the columns and add individual rows of data!!! Where is your "mcve" that shows how you implemented the suggestion I gave you??? Good luck I don't have any more time tonight. – camickr Sep 02 '18 at 03:43
  • the edits you gave just show blank scrollPane and nothing... i set the scrollpane to contentPane and table to scrollPane . printed the result using system.out.println. all working fine... dude just not showing in table – Karan Malhotra Sep 02 '18 at 03:52
  • i again tried your editing but it just show a column name 'table' no rows is showing but table names are printing fine in console – Karan Malhotra Sep 02 '18 at 04:04
  • okay i got it... you have done 2 mistakes in the code... first mistake is in the column variable of vector you did vector.addElement . instead of columnNames. and 2nd mistake is in while loop . you just declared the row variable row is just the variable not the pre-defined method. compiler have to know that this is the row because u did same for the column. just add model.addRow(row); okay i am posting the full answer for understanding. – Karan Malhotra Sep 02 '18 at 05:43
  • The code was typed up quickly because I had somewhere to go. The code is never tested and is included to give you the idea of what needs to be done. I asked you if you `printed the row/columns of the table`. This is how you verify that the data was added properly. It then allows you to determine if the problem is with the data, or how the table is added to the frame. Displaying the data from the ResultSet only proves the SQL query is correct, it does not show you created the TableModel correctly. That is why I asked you to post `your code`, so I could double check if you did it properly. – camickr Sep 02 '18 at 12:37
  • I would then have noticed that I made a typing error. `what is mcve ?.` I gave you the link in my earlier comment. It is how you post simple executable code that we can look at to get the complete idea of what your code is doing. This allows us to help debug the code for simple mistakes. The code should be small and simple and only include code directly related to the question, but we should be able to compile it. – camickr Sep 02 '18 at 12:40
  • Only model.add(); Method for model table is not defined. You have to add model.addRow(); – Karan Malhotra Sep 02 '18 at 12:46
  • Thanks @camcker for this. I was trying from yesterday morning then i posted the code in evening. You saved my day... Love you bro – Karan Malhotra Sep 02 '18 at 12:55
0
Vector columnNames = new Vector();
    columnNames.addElement("Table");

    DefaultTableModel model = new DefaultTableModel(columnNames, 0);
    try{con = DriverManager.getConnection("your url");
        String[] types = {"TABLE"};
        DatabaseMetaData metadata = con.getMetaData();
        ResultSet resultSet = metadata.getTables(null, null, "%", types);


            while (resultSet.next()) 
            {

                Vector row = new Vector();
                row.addElement( resultSet.getString(3) );
                model.addRow(row);
            }

            table = new JTable(model  );
            scrollPane = new JScrollPane( table );
            scrollPane.setBounds(10, 11, 724, 639);
            contentPane.add( scrollPane );
            scrollPane.setViewportView(table);

thanks to @camickr for the code. i will not figure this out without your help. thanks man

Karan Malhotra
  • 125
  • 3
  • 11