当我运行项目时..只打印数据库结果并且没有表格的行。当我不使用Servlet时,我将显示。请帮我解释一下代码。
_AdminShared.cshtml
当我运行项目时..只打印数据库结果并且没有表格的行。当我不使用Servlet时,我将显示。请帮我解释一下代码。
答案 0 :(得分:0)
希望这会对你有所帮助
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// processRequest(request, response);
String url="jdbc:mysql://localhost:3306/world";
// Database credentials
String us = "root";
String pwd = "root";
// Set response content type
response.setContentType("text/html");
PrintWriter out = response.getWriter();
String title = "Database Result";
String docType =
"<!doctype html public \"-//w3c//dtd html 4.0 " +
"transitional//en\">\n";
out.println(docType +
"<html>\n" +
"<head><title>" + title + "</title></head>\n" +
"<body bgcolor=\"#f0f0f0\">\n" +
"<h1 align=\"center\">" + title + "</h1>\n");
try{
// Register JDBC driver
Class.forName("com.mysql.jdbc.Driver");
// Open a connection
Connection conn=DriverManager.getConnection(url,us,pwd);
// Execute SQL query
Statement stmt = conn.createStatement();
String sql;
sql = "Select * from city LIMIT 10";
ResultSet rs = stmt.executeQuery(sql);
// Extract data from result set
while(rs.next()){
//Retrieve by column name
String d1=rs.getString("ID");
String d2=rs.getString("Name");
String d3=rs.getString("CountryCode");
String d4=rs.getString("District");
String d5=rs.getString("Population");
//String last = rs.getString("last");
//Display values
out.println("ID: " + d1 + "<br>");
// out.println(", Age: " + age + "<br>");
out.println(", Name: " + d2 + "<br>");
// out.println(", Last: " + last + "<br>");
out.println(", Code: " + d3 + "<br>");
out.println(", District: " + d4 + "<br>");
out.println(", Pop: " + d5 + "<br>");
}
out.println("</body></html>");
// Clean-up environment
rs.close();
stmt.close();
conn.close();
}
catch(Exception se){
//Handle errors for JDBC
se.printStackTrace();}
}