Hi. i made a database table table and i showed it in a jframe through jtable.I want to change my column name displayed.So far i used the follwing code.

private void Update_table(){
try{
String sql="select stu_id ,stu_name ,Blood_group  from student";
pst=conn.prepareStatement(sql);
rs=pst.executeQuery();
md = rs.getMetaData();
int columnCount = md.getColumnCount();

Vector data = new Vector(columnCount);
Vector row = new Vector(columnCount);
Vector columnNames = new Vector(columnCount);


for (int i = 1; i <= columnCount; i++) {
    columnNames.addElement(md.getColumnName(i));
}
while (rs.next()) {
    for (int i = 1; i <= columnCount; i++) {
        row.addElement(rs.getObject(i));

    }
    data.addElement(row);
    row = new Vector(columnCount); // Create a new row Vector
}
DefaultTableModel model = new DefaultTableModel(data, columnNames);

table_student.setModel( model ); 
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}

i changed the query to "select stu_id as 'id no',stu_name as 'name',Blood_group as 'blood group' from student" and "select stu_id as "\id no"\,stu_name as "\name"\,Blood_group as "\blood group"\ from student". But it didn't work.The column name remain the same.What should i do?

Dani AI

Generated

your SQL aliases did not appear because you are reading headers with ResultSetMetaData.getColumnName(...), which always returns the physical column name. If you want the names from your AS aliases, use getColumnLabel(...) instead. It returns the alias when present, or falls back to the name. (docs.oracle.com)

Example fix (replace the line where you add column names):

// use the alias from SQL (eg, AS `id no`)
columnNames.add(md.getColumnLabel(i));

If you prefer not to change the query or metadata call, you can rename headers after installing the model. This keeps the data as-is and only changes what the header shows:

// after table_student.setModel(model);
table_student.getColumnModel().getColumn(0).setHeaderValue("id no");
table_student.getColumnModel().getColumn(1).setHeaderValue("name");
table_student.getColumnModel().getColumn(2).setHeaderValue("blood group");
table_student.getTableHeader().repaint();

TableColumn#setHeaderValue is the supported way to update header text. (docs.oracle.com)

Small MySQL tip to avoid quoting confusion: when an alias contains spaces, use backticks (or no quotes if there are no spaces). For example:

SELECT stu_id AS `id no`,
       stu_name AS `name`,
       Blood_group AS `blood group`
FROM student;

Backticks make it an identifier; single quotes make a string literal, which can behave unexpectedly outside the SELECT list. The manual also notes that column aliases can be referenced in ORDER BY/GROUP BY using the identifier form. (dev.mysql.com)

’s suggestion of supplying your own columnNames vector works too; the getColumnLabel(...) route is just the least code change if you want SQL-driven labels.

Recommended Answers

All 6 Replies

Lines 14-16 create the Vector of column names from the result set (did you write that code yourself, or just copy it?). If you want some other name(s), put them into the Vector instead of the current value(s).

Sorry i didn't understand.Shouldn't i change the query?Please make myself clear.I am biginner

Do you understand what lines 14-16 are doing? Do you understand how Vector columnNames is used?

yeah i understand.But don't have a clear idea.Please help.

If you understand that then you will already know that whatever names you want for the columns just put those names in Vector columnNames
It's as easy as that.

columnNames.addElement("Column 1");
columnNames.addElement("My name for col2");
columnNames.addElement("This is col 3");
etc
commented: mmake crystal clear +0

Thanks a lot.You are a life saver.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.