Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JTable Header format

I want to change the header of my JTable that displays the data from SQL Server database because it also display the same column name on my database. I just need the Data itself, not the column name.

here is the code that I used to display the Data:

public void search() throws Exception{
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            String url = "jdbc:odbc:*****";
            String user = "*****";
            String pass = "*****";
            Connection con =  DriverManager.getConnection(url, user, pass);
            Statement state = con.createStatement();
            ResultSet rs = state.executeQuery("SELECT * FROM dbo.Patients");
            ResultSetMetaData rsmetadata = rs.getMetaData();
            int columns = rsmetadata.getColumnCount();
            DefaultTableModel dtm = new DefaultTableModel();
            Vector column_name = new Vector();
            Vector data_rows = new Vector();

            for (int i=1; i<columns;i++){
                column_name.addElement(rsmetadata.getColumnName(i));
            }
            dtm.setColumnIdentifiers(column_name);

            while(rs.next()){
                data_rows = new Vector();
                for (int j=1; j<columns; j++){
                data_rows.addElement(rs.getString(j));
                }
                dtm.addRow(data_rows);
            }
            tblPatient.setModel(dtm);
    }

and this is the result :

THIS

I want to change that pIDNo to Patient ID, pLName to Last Name, pFName to First Name, so on and so forth...

like image 620
Crystal Maiden Avatar asked Jan 01 '26 08:01

Crystal Maiden


2 Answers

Changing your SELECT * FROM dbo.Patients to SELECT pIDNo AS 'Patient ID', pLName AS '.... is the easiest way. And, naming the column names instead of using * is faster.

like image 191
David Pitre Avatar answered Jan 03 '26 22:01

David Pitre


I'd try to modify your select:

instead

SELECT * FROM dbo.Patients

Use

SELECT pId as "Patient ID", ...

like image 40
evgenyl Avatar answered Jan 03 '26 22:01

evgenyl



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!