Java Servlet - 显示列名称

时间:2016-05-28 15:57:43

标签: java database servlets

我一直在研究Java Servlet,它选择index.html中用户输入的表中的所有数据。但是我遇到了一个问题,因为我有一个包含许多不同ID的数据库,因此我想在一个表中显示列名 - 现在它只显示没有任何列名的数据,我一直看似无法找到一个解决方案或者自己想出一个解决方案

start.java:

package start;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.Statement;
import java.sql.SQLException;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class Start
 */
@WebServlet("/Start")
public class Start extends HttpServlet {
    private static final long serialVersionUID = 1L;
    public static void getColumnNames(ResultSet rs) throws SQLException {
        if (rs == null) {
          return;
        }
        ResultSetMetaData rsMetaData = rs.getMetaData();
        int numberOfColumns = rsMetaData.getColumnCount();


        for (int i = 1; i < numberOfColumns + 1; i++) {
          String columnName = rsMetaData.getColumnName(i);
          String tableName = rsMetaData.getTableName(i);
          System.out.println("column name=" + columnName + " table=" +     tableName);
    }
  }

/**
 * @see HttpServlet#HttpServlet()
 */
public Start() {
    super();
    // TODO Auto-generated constructor stub
}

/**
 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
 *      response)
 */
protected void doGet(HttpServletRequest request, HttpServletResponse respond) throws ServletException, IOException {
    PrintWriter pw = respond.getWriter();
    respond.setContentType("text/html");
    String tb = request.getParameter("table");
    try {

        Class.forName("com.mysql.jdbc.Driver").newInstance();
        Connection con = DriverManager.getConnection("jdbc:mysql://localhost/music","root","1234");
        Statement st = con.createStatement();
        System.out.println("connection established successfully!");
        ResultSet rs = st.executeQuery("SELECT * FROM " + tb);

        pw.println("<table border=1>");
        while (rs.next()) {
            pw.println("<tr><td>" + rs.getInt(1) + "</td>" + "<td>" + rs.getString(2) + "</td>" + "<td>" + rs.getString(3)
                    + "</td></tr>");
        }
        pw.println("</table>");
        pw.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    // TODO Auto-generated method stub
    // response.getWriter().append("Served at:
    // ").append(request.getContextPath());
}

/**
 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
 *      response)
 */
protected void doPost(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {
    // TODO Auto-generated method stub
    doGet(request, response);
    }
}

结果如下: 1 任何帮助将不胜感激!

2 个答案:

答案 0 :(得分:1)

您应该使用ResultSetMetaData从结果中检索元数据。您将使用ResultSetMetaData#getColumnName(int)

检索列名称
ResultSetMetaData rsmd = rs.getMetaData();
pw.println("<table border=1>");
pw.println("<tr>");
for (int i = 1 ; i <= rsmd.getColumnCount() ; i++) {
    pw.println("<th>" + rsmd.getColumnName(i) + "</th>");
}
pw.println("</tr>");
while (rs.next()) {
    pw.println("<tr><td>" + rs.getInt(1) + "</td>" + "<td>" + rs.getString(2) + "</td>" + "<td>" + rs.getString(3)
            + "</td></tr>");
}
pw.println("</table>");

答案 1 :(得分:0)

尝试获取表格列:

String dbname="test";
String tablename="emp"; 
Statement st=mySqlCon.createStatement();
 int i=st.executeUpdate("use "+dbname);
 ResultSet clmnRs=st.executeQuery("desc "+tablename);
 while(clmnRs.next())
{
 System.out.println(clmnRs.getString(1));
}