执行查询时出错

时间:2011-05-27 06:21:08

标签: java mysql jdbc

我有一个从数据库中获取数据的函数,并以表格格式显示在JSP中。该函数是用java类编写的,在JSP中调用。但是查询没有执行。同样的查询可以在SQL yog中执行。可能是什么问题?

我的代码就像这样

 public ArrayList<CustomReportDTO> getHostAndUserAndResponsereport(String argHostId, String argColoumns, String argStartDate, String argEndDate) {
        Connection con = null;
        Statement stmt = null;
        ResultSet rs = null;
        ArrayList<CustomReportDTO> arrMetrics = new ArrayList<CustomReportDTO>();
        StringBuffer strQuery;
        argStartDate = getDate(argStartDate);
        argEndDate = getDate(argEndDate);
        if (!argColoumns.contains(",")) {
            argColoumns = argColoumns + ",";
        } else {
            argColoumns = argColoumns + ",";
        }

        String[] arrColoumns = argColoumns.split(",");
        try {
            System.out.println("Before Getting Connection");
            con = DBConnect.getInstance().getConnection();
            System.out.println("Connection__________ " + con);
            strQuery = new StringBuffer();
            strQuery.append("SELECT " + argColoumns + " u.Time_stamp AS host_time_stamp,");
            strQuery.append("db1.queryresultset AS Concurrent_Users,");
            strQuery.append("db2.queryresultset AS Active_Users, ");
            strQuery.append("db3.queryresultset AS Logged_Users,");
            strQuery.append("db4.queryresultset AS Anonymous_Users,");
            strQuery.append("urlhs.totalrsptime AS totalrsptime");
            strQuery.append(" FROM ");
            strQuery.append("unix_host_history u, ");
            strQuery.append("db_query_history db1, ");
            strQuery.append("db_query_history db2, ");
            strQuery.append("db_query_history db3, ");
            strQuery.append("db_query_history db4 ,");
            strQuery.append("url_history urlhs ");
            strQuery.append("WHERE ");
            strQuery.append("u.hostid = 'HS001' AND ");
            strQuery.append("db1.compid='CP009' AND ");
            strQuery.append("db2.compid='CP010' AND ");
            strQuery.append("db3.compid='CP011' AND ");
            strQuery.append("db4.compid='CP012' AND ");
            strQuery.append("urlhs.compid='CP013' AND ");

            strQuery.append("SUBSTR(u.time_Stamp,1,15) = SUBSTR(db1.time_stamp,1,15) AND ");
            strQuery.append("SUBSTR(u.time_Stamp,1,15) = SUBSTR(urlhs.time_stamp,1,15) AND ");
            strQuery.append("SUBSTR(db1.time_Stamp,1,15) = SUBSTR(db2.time_stamp,1,15) AND ");
            strQuery.append("SUBSTR(db1.time_Stamp,1,15)= SUBSTR(db3.time_stamp,1,15) AND ");
            strQuery.append("SUBSTR(db1.time_Stamp,1,15) = SUBSTR(db4.time_stamp,1,15) AND ");

            strQuery.append("u.time_stamp > '" + argStartDate + "' AND ");
            strQuery.append("u.time_stamp < '" + argEndDate + "' ");
            strQuery.append("ORDER BY u.time_stamp ");
            System.out.println("Query" + strQuery);
            System.out.println("After Getting Connection_____________");
            stmt = con.createStatement();
            System.out.println("Stament " +stmt);
            System.out.println("After Creating Statmnet_________________________");
            rs = stmt.executeQuery(strQuery.toString());
            System.out.println("After Excecuting Query______________");

CustomReportDTO objUnix = null;

            if (rs != null) {
                while (rs.next()) {
                    objUnix = new CustomReportDTO();
                    for (int i = 0; i < arrColoumns.length; i++) {
                        if (arrColoumns[i].equalsIgnoreCase("OpenFilesCount")) {
                            objUnix.setOpenFilesCount(rs.getString("OpenFilesCount"));

 }
                   }
 arrMetrics.add(objUnix);

                }


            }
            rs.close();
            stmt.close();
            con.close();
        } catch (SQLException se) {
            System.out.println("SQL Exception : ");
            System.out.println(se.getMessage());

        }
        System.out.println("Size Result _______" + arrMetrics.size());
        return arrMetrics;
    }
}

我可以在创建语句之前获得Sysouts。

1 个答案:

答案 0 :(得分:0)

没有例外,这只是一个猜测,但也许argColumns最后需要一个逗号而没有一个逗号?