我正在控制台中打印来自 MySQL 查询的输出,并打印到我的 gui 应用程序的文本区域中。现在,只要名称长于其列的宽度,文本区域中的列就会变得混乱。如何修复下面的代码段以使文本区域的输出看起来一致?
else if (e.getSource() == executeButton) {
//Create a statement
try{
String commandString = enterSQLText.getText();
Statement statement = conn.createStatement();
//Execute a statement
ResultSet resultSet = statement.executeQuery(commandString);
//Process query results
ResultSetMetaData metaData = resultSet.getMetaData();
int numberOfColumns = metaData.getColumnCount();
for(int i = 1; i<= numberOfColumns; i++){
executionResultText.append(metaData.getColumnName(i) + "\t");
System.out.printf("%20s\t", metaData.getColumnName(i));
}
executionResultText.append("\n");
System.out.println();
while (resultSet.next()){
for (int i = 1; i <= numberOfColumns; i++){
executionResultText.append(resultSet.getObject(i).toString() + "\t");
System.out.printf("%20s\t", resultSet.getObject(i));
}
executionResultText.append("\n");
System.out.println();
}
System.out.println("Execute Button Works!");
}
catch (Exception h){
h.printStackTrace();
}
}
我很感激帮助。