Atul Rai
Atul Rai

Reputation: 354

Table column headers are not correct in JDBC

I have stated the connectivity of mysql with java using eclipse ide and add some records but the result is not correct. When I display the list of table, the table fields are not showing, here I got the database name.

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());
}

Upvotes: 0

Views: 118

Answers (2)

SkyWalker
SkyWalker

Reputation: 29168

Your while loop look be like this. you have missed to add </td>

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

Upvotes: 0

JB Nizet
JB Nizet

Reputation: 692081

From the javadoc

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

You probably want 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.

Everything becomes simpler when you read the documentation.

Upvotes: 2

Related Questions