Mysql查询返回servlet中的单个记录。如何打印?

问题描述 投票:0回答:2

我写的代码是

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中出来的。

mysql servlets jdbc
2个回答
0
投票

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

所以,只需替换

if(rs != null){

作者

if (rs.next()) {

您应该已经准备就绪。

如果要处理“未找到结果”的情况,只需在elseif中进行处理:

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

另请参见:


Unrelated涉及到具体问题,此代码还有其他严重问题:存在巨大的SQL注入攻击漏洞,并且数据库资源正在被泄漏掉。这是不对的。

另请参见:

© www.soinside.com 2019 - 2024. All rights reserved.