G.S
G.S

Reputation: 21

Fetching column name with table from database

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();
        }
}

Upvotes: 0

Views: 1819

Answers (4)

camickr
camickr

Reputation: 324207

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.

Upvotes: 0

Ravi MCA
Ravi MCA

Reputation: 2631

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

Upvotes: 2

user3145373 ツ
user3145373 ツ

Reputation: 8156

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));
}

Upvotes: 3

Vijayan Kani
Vijayan Kani

Reputation: 368

String column_i = table.getModel().getColumnName(i);

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

Upvotes: 1

Related Questions