Java Timediff作为结果集

Java Timediff作为结果集,java,sql,Java,Sql,我遇到了麻烦,我正在寻找解决办法 情况是这样的:我通过查询从sql数据库中的视图获取信息,并将其显示在xls文件中 问题是,在最后一列中,我必须显示开始日期和结束日期之间的小时差。我已经尝试了很多方法,但没有找到解决办法。有人能帮我吗 /* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | T

我遇到了麻烦,我正在寻找解决办法

情况是这样的:我通过查询从sql数据库中的视图获取信息,并将其显示在xls文件中

问题是,在最后一列中,我必须显示开始日期和结束日期之间的小时差。我已经尝试了很多方法,但没有找到解决办法。有人能帮我吗

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.mycompany.mavenproject2;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import org.apache.poi.hssf.usermodel.HSSFRow;
import org.apache.poi.hssf.usermodel.HSSFSheet;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
/**
 *
 * @author Stagiaire
 */
public class DataExportService {
    ConnectDB newConnectionDB = new ConnectDB();

    public void getXls() throws Exception  {
        try {
            Class.forName("com.mysql.jdbc.Driver");
            ConnectDB con = new ConnectDB();
            PreparedStatement pst = ConnectDB.getConnection().prepareStatement("");
            ResultSet rs = pst.executeQuery("SELECT * FROM v_facture_infos WHERE real_begin_date >= DATE_FORMAT( CURRENT_DATE - INTERVAL 1 MONTH, '%Y/%m/01' ) AND real_end_date < DATE_FORMAT( CURRENT_DATE, '%Y/%m/01' )");
            HSSFWorkbook workbook = new HSSFWorkbook();
            HSSFSheet sheet = workbook.createSheet("test");
            HSSFRow rowhead = sheet.createRow((short) 0);
            rowhead.createCell((short) 0).setCellValue("Date commencement");
            rowhead.createCell((short) 1).setCellValue("Date fin");
            rowhead.createCell((short) 2).setCellValue("Nom");
            rowhead.createCell((short) 3).setCellValue("Prénom");
            rowhead.createCell((short) 4).setCellValue("Heures");
            int i = 1;
            while (rs.next()){
                HSSFRow row = sheet.createRow((short) i);
                row.createCell((short) 0).setCellValue(rs.getDate("real_begin_date"));
                row.createCell((short) 1).setCellValue(rs.getDate("real_end_date"));
                //query total heure avec la différence de real begin date et real end date : SELECT TIMEDIFF(@real_begin_date, @real_end_date)
                row.createCell((short) 2).setCellValue(rs.getString("last_name"));
                row.createCell((short) 3).setCellValue(rs.getString("first_name"));               
                row.createCell((short) 4).setCellValue(rs.getString("SELECT TIMEDIFF(@real_begin_date, @real_end_date)"));
                i++;
            }
            String yemi = "C:\\Users\\Stagiaire\\Desktop\\test.xls";
            FileOutputStream fileOut = new FileOutputStream(yemi);
            workbook.write(fileOut);
            fileOut.close();
        } catch (ClassNotFoundException e1) {
            e1.printStackTrace();
        } catch (SQLException e1) {
            e1.printStackTrace();
        } catch (FileNotFoundException e1) {
            e1.printStackTrace();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }
}

尝试将其作为sql的一部分进行计算

ResultSet rs = pst.executeQuery("SELECT real_begin_date, real_end_date, first_name, last_name, TIMEDIFF(real_begin_date, real_end_date) FROM...");

您可以通过SQL获取小时差,如下所示:

DATEDIFFhour,开始日期,结束日期

如果需要分数小时数,可以使用更高分辨率的DATEDIFF并将结果除以:

DATEDIFF(second, start_date, end_date) / 3600.0

您不能用rs.get*执行这样的查询-您必须将其写入SELECT语句中。我已经尝试过了,netbeans告诉我我必须同时连接多个连接。是的,它可以工作,非常感谢,我只是在timediff之后添加了一个AS TIME,以便在rs.getString上调用它。如下所示:选择real\u begin\u date、real\u end\u date、first\u name、last\u name、TIMEDIFFreal\u begin\u date、real\u end\u date作为时间起始日期。。。