servlet在执行时始终显示为null

时间:2016-03-12 09:54:48

标签: java html servlets

当我运行search.html文件时,它会成功运行,但在输入值后,输出如图像output所示,请尽可能提供帮助。 Odbc数据源名称是基础,表名是tab,我使用Microsoft Access进行数据存储Access

database.java

import java.sql.*;
import javax.servlet.annotation.WebServlet;

public class database {

public Connection connect;
public Statement stat;
public String query;
public ResultSet result;

public database()
{
    try
    {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        connect = DriverManager.getConnection("jdbc:odbc:base","","");
        stat = connect.createStatement();
    }catch(Exception e){}
}

}

search.html

<!DOCTYPE html>
<html>
<head>
<meta charset="ISO-8859-1">
<title>Search</title>
</head>
<body>
<form action="search" method="get">
    <input type="number" name="ref" placeholder="reference" />
    <input type="submit" value="search" />
</form>
</body>
</html>

search.java

import java.io.*;
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;


@WebServlet("/search")
public class search extends HttpServlet {
private static final long serialVersionUID = 1L;

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

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

    response.setContentType("text/html");
    PrintWriter out = response.getWriter();
    database DB = new database();

    DB.query = "SELECT * FROM tab where ref="
            +request.getParameter("ref");
    out.println("<html><body><table border=\"2\">"
            +"<tr><th>ref</th><th>des</th><th>pri</th><th>cat</th>"
            +"</tr>");

    try
    {
        DB.result=DB.stat.executeQuery(DB.query);
        while(DB.result.next())
        {
            out.println("<tr>"
                    +DB.result.getInt("ref")
                    +DB.result.getString("des")
                    +DB.result.getDouble("pri")
                    +DB.result.getString("cat")
                    +"</tr>");

        }
        out.println("</table></body></html>");
    }catch(Exception e){
        out.println(e.getMessage());
    }
}

/**
 * @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 个答案:

答案 0 :(得分:-1)

虽然您的以下课程看起来不错,但请尝试以下更改:

<强> database.java

public class database {

public Connection connect;
public String query;
public ResultSet result;

public Connection databaseConnect()
{
    try
    {
        Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
        connect = DriverManager.getConnection("jdbc:odbc:base","","");
        return connect;
    }catch(Exception e){}
}

}

<强> search.java

protected void doGet(HttpServletRequest request, HttpServletResponse response) 
throws ServletException, IOException {
try{
Connection con = database.databaseConnect();
Statement stat = con.createStatement();
response.setContentType("text/html");
PrintWriter out = response.getWriter();
//database DB = new database();

String query = "SELECT * FROM tab where ref="
        +request.getParameter("ref");
out.println("<html><body><table border=\"2\">"
        +"<tr><th>ref</th><th>des</th><th>pri</th><th>cat</th>"
        +"</tr>");

    result=stat.executeQuery(query);
    while(result.next())
    {
        out.println("<tr>"
                +result.getInt("ref")
                +result.getString("des")
                +result.getDouble("pri")
                +result.getString("cat")
                +"</tr>");

    }
    out.println("</table></body></html>");
}catch(Exception e){
    e.printStackTrace();
}
}