如何将用户定义的数据透视查询变量(@sql)提取到java的结果集中

时间:2015-02-05 06:56:15

标签: java mysql

从查询中我不知道如何检索java的resultset中的值。我想为用户显示出勤报告。

BEGIN
     SET SESSION group_concat_max_len = 1000000000;

      SET @sql = NULL ;
  SELECT
    GROUP_CONCAT(
      DISTINCT CONCAT(
        'max(CASE WHEN attendance.date = '',
        DATE_FORMAT(DATE, '%Y-%m-%d'),
        '' THEN coalesce(p.present, '') END) AS `',
        DATE_FORMAT(DATE, '%Y-%m-%d'),
        '`'
      )
    ) INTO @sql
  FROM
    calendar
  WHERE DATE >= '2015-01-01'
    AND DATE <= '2015-01-31' ;
  SET @sql = CONCAT(
    'SELECT attendance.present,attendance.user_id, ', @sql,'
        from
            (
              select c.date, a.user_id,a.present
              from calendar c
              cross join attendance a
            ) attendance
            left join attendance  p
              on attendance.user_id= p.user_id
              and attendance.date = p.attendance_date
           where attendance.date>='2015-01-01'
              and attendance.date <= '2015-01-31'
          group by attendance.user_id'
  ) ;
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
END

这个程序我现在在java代码中调用我想检索要显示给用户的结果值

    String sp_query = "CALL AttendanceTrying()";


     try {
                    connection = DBUtil.getInstance().getConnection();

                    callableStatement = connection.prepareStatement(sp_query);
                    resultSet = callableStatement.executeQuery();

                    while(resultSet.next()) {

                     Attendance attendance = new Attendance();
                    /*here I don't know how to retrieve the values of procedure*/
                    }

1 个答案:

答案 0 :(得分:0)

将resultset.getXXX()方法分别调用到您的输出。 ie)你期望三行数据类型为int,string,sting然后通过resultset.getInt(1)调用结果集 - &gt;给出整数值,resultset.getString(2) - &gt;给出字符串值,resultset.getString(3) - &gt;给出字符串值。