I want to get table label of column .like string display in mysql. Like this
However, when I use getColumnName, It turn out there is some difference between returned string and below string. Like this:
But It is right in Variables explorer in eclipse when I debug, Like this:
I can't find other way to get column. It seem returned string is originalColumnName
, but how to get ColumnName
? Anyone know how to fix it?
There is my code, I know there is other problem in code. Please just assume the type of all column is String.
public ResultSet DisplayShowTables() throws SQLException
{
ResultSet Res = Sta.executeQuery("DESC Code2Name");
ResultSetMetaData ResMeta = Res.getMetaData();
String [] ColumnName = new String [ResMeta.getColumnCount()];
int MetaCount = ResMeta.getColumnCount();
for (int i = 0; i < MetaCount; i++) {
ColumnName [i] = ResMeta.getColumnName(i+1);
}
String LeftAlignFormat = "|";
String Separator = "+";
for (int i = 0; i < MetaCount; i++) {
LeftAlignFormat = LeftAlignFormat.concat(" %-20s |");
Separator =Separator.concat("----------------------+");
}
LeftAlignFormat = LeftAlignFormat.concat("%n");
Separator = Separator.concat("%n");
if(Res.isBeforeFirst()){
System.out.format(Separator);
System.out.format(LeftAlignFormat, ColumnName);
System.out.format(Separator);
}
while (Res.next()) {
Vector<String> RowData = new Vector<String>();
for (int i = 0; i < MetaCount; i++) {
RowData.add(Res.getString(i+1).toString());
}
System.out.format(LeftAlignFormat, RowData);
}
if(Res.isAfterLast())
System.out.format(Separator);
return Res;
}