我使用预处理语句进行交叉表查询(使用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 :(得分: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 = '''?''' "