Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java - How to get Column name on Result Set [duplicate]

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);
}
like image 583
JeraldPunx Avatar asked Sep 30 '13 13:09

JeraldPunx


3 Answers

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]);
}
like image 124
Prabhakaran Ramaswamy Avatar answered Oct 18 '22 08:10

Prabhakaran Ramaswamy


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.

like image 27
Ashok kumar Avatar answered Oct 18 '22 09:10

Ashok kumar


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);
like image 23
anubhava Avatar answered Oct 18 '22 08:10

anubhava