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();
        }
}
1
  • 1
    I think (but I'm unsure what you are asking) that the code for resultSetToTableModel might help you get an answer Commented Mar 2, 2017 at 12:41

4 Answers 4

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));
}
Sign up to request clarification or add additional context in comments.

2 Comments

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 ?
hello @G.S.. please go through stackoverflow.com/questions/21898053/… it will help you.
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

3 Comments

Thank you sir...but get error...java.lang.ArrayIndexOutOfBoundsException: 11
@G.S My bad typo error. Just initialize i=0 instead i=1
@G.S could you update your question with new code and error exception
1
String column_i = table.getModel().getColumnName(i);

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

1 Comment

I tried it......................... int i = 0; String column_i = table.getModel().getColumnName(i);................Sir nothing to display in table
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.

4 Comments

I did...but table not displayed...and here I directly form table in swing by using JTable
@G.S, see edit. You need to do some basic debugging by simplifying the problem.
@camickr...I get whole table with all data...but only header part(where column names are there) not displays..even not getting
@G.S, if you don't get rows of data then your SQL query is wrong.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.