简体   繁体   English

使用Java Servlet和HTML表单检索数据库数据

[英]Retrieve DB data with Java servlet and HTML form

I created a simple form and a small servlet to execute a SQL statement from browser. 我创建了一个简单的表单和一个小servlet,以从浏览器执行SQL语句。 When I click "Submit Query" button on the form, then the returinig page shows nothing retrieved from the DB. 当我单击表单上的“提交查询”按钮时,然后returinig页显示未从数据库检索到任何内容。 It only shows "Database Results" ( tag part) on the browser. 它仅在浏览器上显示“数据库结果”(标记部分)。 Please advice me what is wrong with my code. 请告诉我我的代码有什么问题。

SQLTestForm.java SQLTestForm.java

import java.io.*;
import javax.servlet.*;
import javax.servlet.http.*;
import java.sql.*;

public class SQLTestForm extends HttpServlet {
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html");
        PrintWriter out = response.getWriter();
        String title = "Results";
        String docType = 
            "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 " + "Transitional//EN\"\n";
            out.print(docType + "<HTML>\n" + "<HEAD><TITLE>" + title + "</TITLE></HEAD>\n" + "<BODY>" + "<H1>Database Results</H1>\n");

        String driver = request.getParameter("driver");
        String url = request.getParameter("url");
        String username = request.getParameter("username");
        String password = request.getParameter("password");
        String qry = request.getParameter("query");
        showTable(driver, url, username, password, qry, out);
        out.println("</BODY></HTML>");
    }

  public void showTable(String driver, String url, String username, String password, String qry, PrintWriter out) {
    try {
    Class.forName(driver);
    Connection conn = DriverManager.getConnection(url, username, password);
    DatabaseMetaData dbMetaData = conn.getMetaData();
    out.println("<UL>");
    String productName = dbMetaData.getDatabaseProductName();
    String productVersion = dbMetaData.getDatabaseProductVersion();
    out.println(" <LI><B>Database:</B> " + productName + " <LI><B>Version:</B> " + productVersion + "</UL>");
    Statement stmt = conn.createStatement();
    ResultSet rs = stmt.executeQuery(qry);
    out.println("<TABLE BORDER=1>");
    ResultSetMetaData rm = rs.getMetaData();
    int cnum = rm.getColumnCount();
    out.println("<TR>");
        for(int i=1; i <= cnum; i++) {
            out.print("<TH>" + rm.getColumnName(i));
        }
        out.println();
        while(rs.next()) {
            out.println("<TR>");
                for(int i=1; i <= cnum; i++) {
                    out.print("<TD>" + rs.getString(i));
                }
            out.println();
        }
        out.println("</TABLE>");
        conn.close();
    } catch (ClassNotFoundException cnfe) {
        System.err.println("Error loading driver: " + cnfe);
    } catch (SQLException se) {
        System.err.println("Error connecting: " + se);
    } catch(Exception e) {
        System.err.println("Error with input: " + e);   }  } }

SQLTestForm.html SQLTestForm.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
 <HEAD>
  <TITLE>SQL Test Form</TITLE>
 </HEAD>
 <BODY>
  <H2>Query Input:</H2>
  <FORM ACTION="/SQLTestForm" METHOD="POST">
  <TABLE>
   <TR><TD>Driver:
   <TD><INPUT TYPE="TEXT" NAME="driver" VALUE="com.jdbc.mysql.Driver" SIZE="45">
   <TR><TD>URL:
   <TD><INPUT TYPE="TEXT" NAME="url"
   VALUE="jdbc:mysql://localhost:3306/test" SIZE="45">
   <TR><TD>Username:
   <TD><INPUT TYPE="TEXT" NAME="username">
   <TR><TD>Password:
   <TD><INPUT TYPE="PASSWORD" NAME="password">
   <TR><TD VALIGN="TOP">Query:
   <TD><TEXTAREA ROWS="5" COLS="35" NAME="query"></TEXTAREA>
   <TR><TD COLSPAN="2" ALIGN="CENTER"><INPUT TYPE="SUBMIT">
  </TABLE>
  </FORM>
 </BODY>
</HTML>

Your classpath is missing the jdbc Driver. 您的类路径缺少jdbc驱动程序。 This may have two causes: 这可能有两个原因:

  1. You have forgotten to deliver the deiver in you war-file or the shared/commons-lib folder. 您已经忘记了在您的war文件或shared / commons-lib文件夹中提供Deiver。
  2. You misspelled the drivers Classname (in you comment you wrote com.djbc and not com.jdbc 您拼错了驱动程序的类名(在注释中,您写的是com.djbc而不是com.jdbc

You know that you open a backdoor to the database? 您知道您打开了数据库的后门吗? The Database engine checks the source of database requests. 数据库引擎检查数据库请求的源。 Now the requests are from your webserver. 现在,请求来自您的网络服务器。 Every single computer that has access to you webpage will get a connection to your database. 每台有权访问您网页的计算机都将连接到您的数据库。

You should change your code: 您应该更改代码:

  • Never use uncheck parameter and pass them to the database. 切勿使用uncheck参数并将其传递给数据库。
  • Use PreparedStatement instead of Statement 使用PreparedStatement代替Statement
  • Don't make String concatenations to create a query 不要进行字符串串联来创建查询

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM