如何在Java Servlet中处理多个查询

时间:2018-11-15 11:11:37

标签: java mysql

我正在尝试发送图片的增加计数变量(每当新会话点击图片时,通过增加+1来增加该变量)。我收到以下错误消息,但我正在检查空结果集。我的思维过程是,我可以尝试选择已调用的pictureNo,如果找不到该pictureNo,我们只需将第一个计数插入表中,如果可以找到,则更新该计数。

错误消息: “ SQLException:对空结果集进行非法操作。”

增加会话计数的代码

 HttpSession session = request.getSession() ;


    Integer counter = (Integer) session.getAttribute("counter");
    String accCount = (String) session.getAttribute("attributeKey") ;
    String url = "http://localhost:8080/techfore";
    String encodedURL = url + ";jsessionid=" + request.getSession().getId();

    if (accCount == null || encodedURL == null) { // New session?
        response.sendRedirect("/techfore/WelcomePage");

        }

    else{

        if(counter == 0) {

         counter = new Integer(counter.intValue() + 1);
            session.setAttribute("counter", counter);

        }
    } 



    Utilities.initalCount(out, pictureName, counter);

运行查询的代码

   public static void initalCount(PrintWriter out, String pictureName, int accessCount) {
        Connection con = null;
        try { // Connect to the database
           con = openConnection(out);
        }
        catch (Exception e) {  // Failed to open the connection
           out.println("<P>" + e.getMessage());

        }

    try {

            Statement stmt = con.createStatement();
            String query0;
            ResultSet rs1;




          query0="SELECT PictureNo FROM Statistics WHERE PictureNo = (SELECT PictureNo FROM Pictures WHERE ShortName = '"+pictureName+"')";





           rs1 = stmt.executeQuery(query0);

           if(rs1.next()){
               //yes exist
                String description = rs1.getString("Description");
               int pictureNo = rs1.getInt("PictureNo");  
               IncreaseCount(out, pictureNo, accessCount);
            }
           else {
           //if rs is null insert
           int pictureNo = rs1.getInt("PictureNo");  
           AddCount(out, pictureNo, accessCount);


           }


            stmt.close() ;




        }
        catch(SQLException ex) {
            out.println("<P>SQLException: " + ex.getMessage()) ;
         }




   }

   public static void AddCount(PrintWriter out, int pictureNo, int accessCount) {

        Connection con = null;
        try { // Connect to the database
           con = openConnection(out);
        }
        catch (Exception e) {  // Failed to open the connection
           out.println("<P>" + e.getMessage());
           return;
        }



        try {

            Statement stmt = con.createStatement();
            String query;
            ResultSet rs1;





            query="INSERT INTO Statistics VALUES "+pictureNo+","+accessCount+" ";


            stmt.executeUpdate(query);


            stmt.close() ;






        }
        catch(SQLException ex) {
            out.println("<P>SQLException: " + ex.getMessage()) ;
         }

 }

   public static void IncreaseCount(PrintWriter out, int pictureNo, int accessCount) {

        Connection con = null;
        try { // Connect to the database
           con = openConnection(out);
        }
        catch (Exception e) {  // Failed to open the connection
           out.println("<P>" + e.getMessage());
           return;
        }



        try {

            Statement stmt = con.createStatement();
            String query;
            ResultSet rs1;





            query="UPDATE Statistics SET AccessCount = "+accessCount+" + 1 WHERE PictureNo = "+pictureNo+"";


            stmt.executeUpdate(query);


            stmt.close() ;





        }
        catch(SQLException ex) {
            out.println("<P>SQLException: " + ex.getMessage()) ;
         }

  }

新插入

 query="INSERT INTO Statistics VALUES (SELECT PictureNo FROM Pictures WHERE FileName = '"+pictureName+"'),"+accessCount+" ";

0 个答案:

没有答案