Pergunta

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...

Foi útil?

Solução

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.

Outras dicas

I'd try to modify your select:

instead

SELECT * FROM dbo.Patients

Use

SELECT pId as "Patient ID", ...

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top