java - JDBC 中的表列标题不正确

标签 java mysql jdbc columnheader

我已经使用eclipse ide说明了mysql与java的连接并添加了一些记录,但结果不正确。 当我显示表列表时,表字段没有显示,这里我得到了数据库名称。

enter image description here

out.println("<body><tt><table border=1 width=90%><tr>");

try {

    ResultSet rs = com.db.Admin.getData();
    ResultSetMetaData mdata = rs.getMetaData();
    int NOC = mdata.getColumnCount();

    for (int i = 1; i <= NOC; i++) {
        out.println("<th>" + mdata.getCatalogName(i));

    }

    while (rs.next()) {
        out.println("<tr>");
        for (int i = 1; i <= NOC; i++)
            out.print("<td>" + rs.getString(i));
        out.println("</tr>");
    }

} catch (Exception e) {
    // TODO: handle exception
    out.print(e.toString());
}

最佳答案

来自the javadoc

Gets the designated column's table's catalog name.

您可能想要getColumnLabel() :

Gets the designated column's suggested title for use in printouts and displays. The suggested title is usually specified by the SQL AS clause. If a SQL AS is not specified, the value returned from getColumnLabel will be the same as the value returned by the getColumnName method.

当您阅读文档时,一切都会变得更简单。

关于java - JDBC 中的表列标题不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35874773/

相关文章:

java - 如何使我的代码显示一定数量的行(就像它一样)但向后显示

java - 安卓/Java : How to import classes in a subpackage from another sub-package?

java - 如何使用 while() 循环在 vertx 上运行无限循环

c# - MySQL 连接字符串 C#

php - MySql小数类型显示问题

java - Spring 的存储过程 - 从过程返回的结果总是空的

java - 将值插入 sqlite - java

mysql - 如何在 MySQL 中使用带有 Group by 的 Set?

java - "=?"在 SQL 查询中使用时代表什么

mysql - 真正流式传输大型 MySQL 结果集