我想在mysql中获取列的类似字符串显示的表标签.像这样
但是,当我使用getColumnName时,结果是返回的字符串和下面的字符串之间存在一些差异.像这样:
但是当我调试时,它在Eclipse中的变量浏览器中是正确的,像这样:
我找不到其他方式来获取专栏.它似乎返回字符串originalColumnName,但如何获得ColumnName?谁知道怎么修它?
有我的代码,我知道代码中还有其他问题.请假设所有列的类型都是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;
}
Run Code Online (Sandbox Code Playgroud)
它看起来DESC只是查询具有列别名的信息模式的快捷方式.
可以使用检索列别名ResultSetMetadata.getColumnLabel(int).
JDBC将列标签定义为:
获取指定列的建议标题,以便在打印输出和显示中使用.建议的标题通常由SQL
AS子句指定.如果AS未指定SQL ,则返回getColumnLabel的值将与getColumnName方法返回的值相同.
这也意味着在几乎所有情况下你应该使用getColumnLabel而不是getColumnName.