11

Hello I'm trying to make an error when there is no matched student... and it will display like this No matching records found and I want the column name still the same but still not figuring it out... can some one tell me if this is right??

Heres my function for that... and I add comment there where I put the error... but i don't know how to get the columnname

public void SearchTableStudent() {
        String tempSearchValue = searchStudent.getText().trim();
        boolean empty = true;
        sql = "SELECT student_id as 'Student ID',"
                + "concat(lastname, '  ,  ', firstname, ' ', middlename) as 'Name'"
                + "FROM user "
                + "WHERE CAST(student_id as CHAR) LIKE '%" + tempSearchValue + "%'";
        try {
            pst = conn.prepareStatement(sql);
            rs = pst.executeQuery();
            while(rs.next()) {
                table.setModel(DbUtils.resultSetToTableModel(rs));
                empty = false;
            }
            if(empty) {
                String error = "";
                table.setModel(new javax.swing.table.DefaultTableModel(
                    new Object [][] {
                        {"No matching records found",null}
                    },
                    new String [] {
     /** I WANT TO PUT THE SAME COLUMN NAME ON MY DATABASE SELECTED BUT DON't Know
 WHAT FUNCTION TO DO*/
                    }
                ));
            }
        } catch (Exception e) {
            JOptionPane.showMessageDialog(null, e.getMessage());
        }
    }

I try like this but still gave me NULL!!! this code is below of empty = false;

for(int i=0; i<table.getColumnCount(); i++) {
    test[i] = table.getColumnName(i);
}
JeraldPunx
  • 309
  • 3
  • 8
  • 18
  • It happens because, if there's no info from the database, you assign true to the variable empty, thus triggering the if(empty) block, which is not assigning any column names to your model. You would have to manually insert column names, but what do you want them for if the resultset is empty? – Martin Sep 30 '13 at 13:08

5 Answers5

30
ResultSetMetaData metaData = resultSet.getMetaData();
int count = metaData.getColumnCount(); //number of column
String columnName[] = new String[count];

for (int i = 1; i <= count; i++)
{
   columnName[i-1] = metaData.getColumnLabel(i);
   System.out.println(columnName[i-1]);
}
Nash N
  • 342
  • 2
  • 17
Prabhakaran Ramaswamy
  • 25,706
  • 10
  • 57
  • 64
  • hello when I try this... the result is `student_id | Name` and the expected result is `Student ID | Name` since this is the default column name – JeraldPunx Sep 30 '13 at 13:23
  • I think there's an extra closing parenthesis there - should be `columnName[i-1] = metaData.getColumnLabel(i);` – jamsandwich May 27 '16 at 04:13
4

Try this.

      ResultSetMetaData meta = resultset.getMetaData(); 

      Integer columncount = meta.getColumnCount();

      int count = 1 ; // start counting from 1 always

      String[] columnNames = new String[columncount];

      while(count<=columncount){

         columnNames [count-1] = meta.getColumnLabel(count);
         count++;

      }

Since here your expecting is to get the columns alias instead of column name, so you have to use ResultSetMetaData.getColumnLabel instead of ResultSetmetaData.getColumnName.

Ashok kumar
  • 435
  • 4
  • 13
  • hello when I try this... the result is `student_id | Name` and the expected result is `Student ID | Name` since this is the default column name – JeraldPunx Sep 30 '13 at 13:29
  • 3
    There is a difference between column name and column label.So you should use the method ResultSetMetaData.getColumnLabel instead of ResultSetmetaData.getColumnName – Ashok kumar Sep 30 '13 at 13:38
2

Get ResultSetMetaData using ResultSet#getMetaData():

ResultSetMetaData meta = rs.getMetaData();

And then to get column name of 1st column:

String col1Name = meta.getColumnLabel(1);

Similarly to get column name of 2nd column:

String col2Name = meta.getColumnLabel(2);
Community
  • 1
  • 1
anubhava
  • 761,203
  • 64
  • 569
  • 643
0

Get the metadata

ResultSetMetaData metaData = rs.getMetaData();

Then you can do:

String columnName = metaData.getColumnName(int index);
ppeterka
  • 20,583
  • 6
  • 63
  • 78
0

rs.getMetaData().getColumnName(int i);

and do not concat the query param!

krstf
  • 627
  • 7
  • 25