0

我写的代码是

import java.io.IOException; 
import java.io.PrintWriter; 
import java.sql.Connection; 
import java.sql.DriverManager;
import java.sql.ResultSet; 
import java.sql.SQLException;
import java.sql.Statement; 
import java.util.logging.Level;
import java.util.logging.Logger; 
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet; 
import javax.servlet.http.HttpServletRequest; 
import javax.servlet.http.HttpServletResponse;

public class Search extends HttpServlet {

    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException, ClassNotFoundException, SQLException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        Connection con =null;
        Statement stmt = null;
        ResultSet rs = null;
        String veh_no = request.getParameter("vehicle_no");
        try {
            /* TODO output your page here. You may use following sample code. */
            Class.forName("com.mysql.jdbc.Driver");
            con = DriverManager.getConnection ("jdbc:mysql://localhost:3306/pay_n_park","root","admin");             
            stmt = con.createStatement();
            rs = stmt.executeQuery("select block_no from vehicle where vehicle_no='"+veh_no+"' and out_time=NULL");
            if(rs != null){
                out.println("<!DOCTYPE html>");
                out.println("<html>");
                out.println("<head>");
                out.println("<title>Search Results</title>");            
                out.println("</head>");
                out.println("<body>");
                out.println("<h1>Search Results :</h1>");
                out.print("<br/> Vehicle No: " + veh_no);
                out.print("<br/> Block No  : " + rs.getString("block_no"));
                out.println("</body>");
                out.println("</html>");
            }


        } finally {            
            out.close();
        }
    }

    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Search.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Search.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        try {
            processRequest(request, response);
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Search.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(Search.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }
}// </editor-fold>

结果集rs只有一条记录。如果我这样做rs.next(),它会出来if,我不想那样。我应该怎么做才能打印这个记录,它if在这两种情况下都出来了。

4

1 回答 1

0

返回ResultSet的是never null。您确实需要调用ResultSet#next()以将光标设置到下一行。它的默认位置即第一行之前。不这样做只会导致

所以,只需更换

if(rs != null){

经过

if (rs.next()) {

你应该准备好了。

如果您打算处理“未找到结果”的情况,只需在else中进行if

if (rs.next()) {
    // At least one record found.
} else {
    // No records found.
}

也可以看看:


与具体问题无关,此代码还有其他严重问题:存在巨大的 SQL 注入攻击漏洞,并且数据库资源正在泄漏。这个不对。

也可以看看:

于 2013-03-27T14:03:36.450 回答