2012-05-10 71 views
0

我用準備好的聲明中 我想添加一個參數(中心),但它不是在這裏工作(使用的是Postgres)交叉表查詢是我的代碼:Postgres的交叉參數

public List<openbookBean> summarylist(String center) throws SQLException { 
     Connection connection = null; 
     PreparedStatement statement = null; 
     ResultSet resultSet = null; 

     String querystring = "select team_manager as team_manager2, " 
         + "compliance_1 as day1, " 
         + "compliance_2 as day2, " 
         + "compliance_3 as day3 " 

         + "FROM crosstab('select team_manager, date_compliance, compliance from openbook_comp where(" 
         + "extract(day from date_compliance)= ''1'' or " 
         + "extract(day from date_compliance)= ''2'' or " 
         + "extract(day from date_compliance)= ''3'') " 

         + "and center = '''?''' " 

         + "order by 1,2') AS openbook_comp (team_manager text, " 
         + "compliance_1 varchar," 
         + "compliance_2 varchar," 
         + "compliance_3 varchar)"; 





     List<openbookBean> summarylist_array = new ArrayList<openbookBean>(); 

     try { 

      connection = database.getConnection(); 
      statement = connection.prepareStatement(querystring); 

       statement.setString(1, center); 


      resultSet = statement.executeQuery(); 

      while (resultSet.next()) { 
       openbookBean summarylistarray = new openbookBean(); 
        summarylistarray.setTeam_manager2(resultSet.getString("team_manager2")); 
        summarylistarray.setDay1(resultSet.getString("day1")); 
        summarylistarray.setDay2(resultSet.getString("day2")); 
        summarylistarray.setDay3(resultSet.getString("day3")); 


       summarylist_array.add(summarylistarray); 
      } 
     } finally { 
      try { resultSet.close(); } catch (SQLException logOrIgnore) {} 
      try { statement.close(); } catch (SQLException logOrIgnore) {} 
      try { connection.close(); } catch (SQLException logOrIgnore) {} 
     } 

     return summarylist_array; 
    } 

    } 

,這裏是一個該servlet的一部分

if (request.getParameter("show").equals("summary")) { 
    try { 
     List<openbookBean> summarylist_array = openbookDAO.summarylist(region); 

     request.setAttribute("summarylist_array", summarylist_array); 
     request.getRequestDispatcher("summary.jsp").forward(request, response); 
    } 

    catch (SQLException e) { 
     throw new ServletException("Cannot retrieve areas", e); 
    } 

    } 

我總是收到此錯誤:

java.lang.NullPointerException 
    source.openbookDAO.summarylist(openbookDAO.java:358) 
    source.openbookServlet.doGet(openbookServlet.java:74) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:617) 
    javax.servlet.http.HttpServlet.service(HttpServlet.java:717) 

回答

0

你是不是關閉最後一個括號,並有一個額外的逗號:

  + "order by 1,2') AS openbook_comp (team_manager text, " 
      + "compliance_1 varchar," 
      + "compliance_2 varchar," 
      + "compliance_3 varchar)" 

而且你可以使用這個:

  + "extract(day from date_compliance) in (1, 2, 3) " 

如果center是數字不要用引號,後添加一個空格參數:

   + "and center = ? " 

如果不是數字,嘗試這樣的:

  + "and center = '''?''' " 
+0

你好Clodoaldo ..我做了你所說的但是它仍然沒有工作... – toink

+0

@toink是否有一些錯誤信息或結果是不是你所期望的?你可以在設置參數後顯示內置的字符串嗎? –

+0

你好,這是我的DAO – toink