简体   繁体   中英

How to fetch user defined variable(@sql) of pivot query to resultset Of java

From query I don't know how to retrieve the values in resultset of java. I want to display the attendance report for user.

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

this procedure im called in java code now I want to retrieve values to result to display to users

    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*/
                    }

call the resultset.getXXX() method's respective to your output. ie) you are expecting the three rows of data type int, string,sting then call the result set by resultset.getInt(1) -> gives the integer value , resultset.getString(2) --> gives the string value, resultset.getString(3) --> gives the string value.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM