简体   繁体   English

如何获取枢纽查询的用户定义变量(@sql)到Java的结果集

[英]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. 从查询中,我不知道如何检索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 此过程我在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*/
                    }

call the resultset.getXXX() method's respective to your output. 分别调用您的输出的resultset.getXXX()方法。 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. 即)您期望三行数据类型为int,string,sting然后通过resultset调用结果集。getInt(1)->给出整数值,resultset.getString(2)->给出字符串值,resultset .getString(3)->给出字符串值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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