0

I want to display column name row while accessing table. Here I tried this code... but only table displayed without column name. using java eclipse and sqlite database

    try
    {
        String query="Select * from client";
        PreparedStatement pst=conn.prepareStatement(query);
        ResultSet rs=pst.executeQuery();

        ResultSetMetaData rsmd = rs.getMetaData();
        int columnCount = rsmd.getColumnCount();

        DefaultTableModel tm = (DefaultTableModel) table.getModel();

        for(int i=1;i<=columnCount;i++)
        {
            tm.addColumn(rsmd.getColumnName(i));
        }

        while (rs.next()) 
        {
            String[] a = new String[columnCount];
            for(int i = 0; i < columnCount; i++)
            {
                a[i] = rs.getString(i+1);

            }
            tm.addRow(a);
    //      tm.fireTableDataChanged();

            rs.close();
            pst.close();
        } 
    } 

        catch(Exception e)
        {
            e.printStackTrace();
        }
}
G.S
  • 21
  • 1
  • 9
  • 1
    I think (but I'm unsure what you are asking) that the code for `resultSetToTableModel` might help you get an answer –  Mar 02 '17 at 12:41

4 Answers4

3

You can use following code to get column names:

ResultSetMetaData metaData = rs.getMetaData();
int count = metaData.getColumnCount(); // get column count

for (int i = 1; i <= count; i++){
   System.out.println(metaData.getColumnLabel(i));
}
user3145373 ツ
  • 7,858
  • 6
  • 43
  • 62
  • Sir, by using s.o.p displayed on console...but want to display table in swing using JTable component...so which method I have to use instead of s.o.p ? – G.S Mar 03 '17 at 07:14
  • hello @G.S.. please go through http://stackoverflow.com/questions/21898053/display-records-from-mysql-database-using-jtable-in-java it will help you. – user3145373 ツ Mar 03 '17 at 09:00
2

ResultSet always contains the returned rows but not the column names.

To get the column names you can use below code.

ResultSetMetaData metadata = rs.getMetaData();

int columnCount = metadata.getColumnCount();

String column_names[] = new String[ columnCount ]; // define a array to store the column names

for (int i=0; i<=columnCount; i++) {

  column_names[ i ] = metadata.getColumnLabel(i); // push column names into array
}

DefaultTableModel table_model = new DefaultTableModel( column_names, columnCount ); // create a table model based of the columns and column count

table=new JTable( table_model ); // create a new table with that model
Ravi MCA
  • 2,491
  • 4
  • 20
  • 30
1
String column_i = table.getModel().getColumnName(i);

Iterate through 'i'; as it represents the index of the column. Cheers!

Vijayan Kani
  • 368
  • 3
  • 9
  • I tried it......................... int i = 0; String column_i = table.getModel().getColumnName(i);................Sir nothing to display in table – G.S Mar 03 '17 at 07:17
0
JScrollPane pane = new JScrollPane(table);
contentPane.add(table);
JscrollPane.add(table);

The above code is all confused:

  1. First you create a scrollpane using the table (which is correct), but then you add the table to the content pane (which is incorrect). A table can only have a single parent so it gets removed from the scrollpane.

  2. Then you try to add the table back to the scrollpane which won't work because you need to add the table to the viewport of the scrollpane, not the scrollpane directly.

So bottom line all you need is:

JScrollPane pane = new JScrollPane(table);
//contentPane.add(table);
//JscrollPane.add(table);

Edit:

First get the code working without the SQL. Use the above suggestion and then change your current code:

//table.setModel(DbUtils.resultSetToTableModel(rs));   
table.setModel( new DefaultTableModel(5,5) );

This should display an empty table with 5 rows and 5 columns.

  1. If you see the table then the problem is with your SQL, you are returning an empty ResultSet.

  2. If you don't see the table then you have a problems somewhere else in your code.

camickr
  • 321,443
  • 19
  • 166
  • 288
  • I did...but table not displayed...and here I directly form table in swing by using JTable – G.S Mar 03 '17 at 05:27
  • @G.S, see edit. You need to do some basic debugging by simplifying the problem. – camickr Mar 03 '17 at 15:47
  • @camickr...I get whole table with all data...but only header part(where column names are there) not displays..even not getting – G.S Mar 08 '17 at 07:19
  • @G.S, if you don't get rows of data then your SQL query is wrong. – camickr Mar 08 '17 at 16:40