3

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

Crystal Maiden
  • 372
  • 1
  • 7
  • 23
  • I would change the query to retrieve each column and change the names. Otherwise, I would look up for the column names inside `column_name` and change the name (of course, this last is very naive). – Luiggi Mendoza Apr 08 '13 at 04:37
  • Not related to the question but it would be better to change `Vector by `ArrayList`: [Why is Java Vector class considered obsolete or deprecated?](http://stackoverflow.com/q/1386275/1065197) – Luiggi Mendoza Apr 08 '13 at 04:38
  • @LuiggiMendoza didn't really know what is Vector was, I just follow some instruction on a website that I google to display data's to JTable, the problem with the result is that there is one missing data which has a MONEY DATA TYPE and it should be in last part but it didn't come out, is that because I used Vector? – Crystal Maiden Apr 08 '13 at 05:08
  • I don't think so since you're retrieving all the data as `String`. Looks like a driver problem. Since you're connecting to MSSQL it would be better to use [Microsoft JDBC Driver](http://msdn.microsoft.com/en-us/sqlserver/aa937724.aspx) or [jTDS](http://jtds.sourceforge.net/) instead of an ODBC. – Luiggi Mendoza Apr 08 '13 at 05:39
  • sir @LuiggiMendoza I don't know how to used that, I thought it's the same since it's ODBC:'JDBC'. I think I have to start from the scratch again... – Crystal Maiden Apr 08 '13 at 06:00

2 Answers2

4

I'd try to modify your select:

instead

SELECT * FROM dbo.Patients

Use

SELECT pId as "Patient ID", ...

MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
evgenyl
  • 7,837
  • 2
  • 27
  • 32
4

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.

Luiggi Mendoza
  • 85,076
  • 16
  • 154
  • 332
David Pitre
  • 181
  • 4