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?

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.