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

时间:2013-03-27 13:59:34

标签: mysql servlets jdbc

我写的代码是

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

1 个答案:

答案 0 :(得分:0)

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

所以,只需替换

if(rs != null){

通过

if (rs.next()) {

你应该全力以赴。

如果您打算处理“未找到结果”的情况,请在else的{​​{1}}中执行此操作:

if

另见:


对于具体问题,

无关,此代码还存在其他严重问题:SQL注入攻击漏洞巨大,数据库资源被泄漏。这不对。

另见: