如何从Access数据库获取Java中的Time字段的SUM

时间:2015-09-08 17:22:58

标签: java date ms-access time sum

我的访问数据库中有一个名为Break的表。但是,我需要以与访问数据库中相同的格式找到字段“breakstart”的SUM。例如in(hh:mm:ss)格式。

                Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                con=DriverManager.getConnection("jdbc:odbc:MominDB","","");
                stmt = con.createStatement();
                rs=stmt.executeQuery("select SUM(totalbreak) AS SUM_HOURS from Break ");
                String s1=null;
                while(rs.next())
                {
                System.out.println(rs);

                 s1=rs.getString("SUM_HOURS");

                }
                System.out.println(s1);

它输出为2.66203703703704E-4。任何帮助将不胜感激。

即使是以下代码也无效。

                    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
                con=DriverManager.getConnection("jdbc:odbc:MominDB","","");

                    stmt = con.createStatement();
                    rs=stmt.executeQuery("SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(breakstart))) from Break ");
                    String s1=null;
                    while(rs.next())
                    {
                    System.out.println(rs);

                     s1=rs.getString(1);

                    }
                    System.out.println(s1);

在表达式中显示RUN Time错误为未定义函数'SEC_TO_TIME'。

2 个答案:

答案 0 :(得分:1)

请注意,Access中的日期/时间值基于double。因此,在数字上,总和是双倍,实际上是天数。

在Access中,您可以使用CDate转换值:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using MySql.Data.MySqlClient;


namespace WebApplication1
{
    public partial class usageDisp : System.Web.UI.Page
{
    string connectionstring = @"Data Source=localhost; Database=globaldotdb; user ID=root; Password=peleg1708";

    protected void Page_Load(object sender, EventArgs e)
    {

        if (!IsPostBack)
        {
            //check
           BindData();

        }
    }
    private void BindData()
    {         
        using (MySqlConnection cn = new MySqlConnection(connectionstring))
        {
            MySqlDataAdapter adp = new MySqlDataAdapter(("SELECT  tblusage.codeUsage,tblcustom.Customer, tblvendor.Vendor, tblusage.dateStart, tblusage.dateEnd, tblregion.Region, tblservice.Service, tblservice.unit, tblusage.isSecure,tblusage.Usage FROM     ((((tblvendor INNER JOIN tblusage ON tblvendor.codeVendor = tblusage.codeVendor) INNER JOIN  tblservice ON tblusage.codeService = tblservice.codeService) INNER JOIN  tblregion ON tblusage.codeRegion = tblregion.codeRegion) INNER JOIN  tblcustom ON tblusage.codeCust = tblcustom.codeCust)"), cn);
            DataTable dt = new DataTable();
            adp.Fill(dt);
            if (dt.Rows.Count > 0)
            {
                gv.DataSource = dt;
                gv.DataBind();



            }
        }
    }
 }
}


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="usageDisp.aspx.cs" Inherits="WebApplication1.WebForm1" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="usageDisp" runat="server">
<asp:GridView ID="gv" runat="server" AutoGenerateColumns="False" 
    DataKeyNames="codeUsage">
    <Columns>
        <asp:TemplateField HeaderText="codeusage" Visible="False">
            <EditItemTemplate>
                <asp:TextBox ID="txtcode" runat="server" Text='<%# Eval("codeUsage") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label1" runat="server" Text='<%# Eval("codeUsage") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Customer">
            <EditItemTemplate>
                <asp:TextBox ID="TXTCust" runat="server" Text='<%# Eval("Customer") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label2" runat="server" Text='<%# Eval("Customer") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="Vendor">
            <EditItemTemplate>
                <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("Vendor") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label3" runat="server" Text='<%# Eval("codeVendor") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="dateStart">
            <EditItemTemplate>
                <asp:TextBox ID="TXTDS" runat="server" Text='<%# Eval("dateStart") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label4" runat="server" Text='<%# Eval("dateStart") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="dateEnd">
            <EditItemTemplate>
                <asp:TextBox ID="TXTDE" runat="server" Text='<%# Eval("dateEnd") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label5" runat="server" Text='<%# Eval("dateEnd") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="service">
            <EditItemTemplate>
                <asp:TextBox ID="TXTSe" runat="server" Text='<%# Eval("Service") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label6" runat="server" Text='<%# Eval("Service") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="region">
            <EditItemTemplate>
                <asp:TextBox ID="TXTRe" runat="server" Text='<%# Eval("Region") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label7" runat="server" Text='<%# Eval("Region") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="isSecure">
            <EditItemTemplate>
                <asp:TextBox ID="TXTIS" runat="server" Text='<%# Eval("isSecure") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label8" runat="server" Text='<%# Eval("isSecure") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="unit">
            <EditItemTemplate>
                <asp:TextBox ID="TXTunit" runat="server" Text='<%# Eval("unit") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label9" runat="server" Text='<%# Eval("unit") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
        <asp:TemplateField HeaderText="usage">
            <EditItemTemplate>
                <asp:TextBox ID="TXTusage" runat="server" Text='<%# Eval("Usage") %>'></asp:TextBox>
            </EditItemTemplate>
            <ItemTemplate>
                <asp:Label ID="Label10" runat="server" Text='<%# Eval("Usage") %>'></asp:Label>
            </ItemTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>
    <div>

    </div>
    </form>
</body>
</html>

使用纯数学:

TotalTime = CDate(2.66203703703704E-4) 
' Result: 00:00:23

我不做Java,所以我不知道如何用Java转换,但Access中日期/时间的数字零值是TotalSeconds = 2.66203703703704E-4 * 24 * 60 * 60 ' Result: 23

所以删除你的字符串处理 - 时间不是文本。

最后,这与MySQL无关,因此通过将其称为MySQL查询来添加混乱。

答案 1 :(得分:1)

继Gustav的回答之后,让我们来看看我们从Java中的JDBC-ODBC Bridge获得的内容。代码

String sql = 
        "SELECT SUM(totalbreak) AS breaksum FROM Break";
try (
        Statement st = conn.createStatement();
        ResultSet rs = st.executeQuery(sql)) {
    rs.next();
    Object o = rs.getObject("breaksum");
    System.out.println(o.getClass().getName());
}

返回

java.lang.Double

因此,ODBC驱动程序中的日期/时间字段的SUM()将作为Double返回。

正如Gustav所提到的,Access中日期/时间的内部表示是一个Double值,其中整数部分表示日期,小数部分表示时间。所以,2.66203703703704E-4天* 86400秒/天= 23秒。

一旦获得总秒数(请注意,一秒是Access中日期/时间字段的最大分辨率),您可以将该值格式化为Java代码中的hh:nn:ss。一种方法是使用Apache commons-lang,如下所示:

String sql = 
        "SELECT SUM(totalbreak) AS breaksum FROM Break";
try (
        Statement st = conn.createStatement();
        ResultSet rs = st.executeQuery(sql)) {
    rs.next();
    Double breaksumSeconds = rs.getDouble("breaksum") * 86400;
    Long breaksumMillis = breaksumSeconds.longValue() * 1000;
    String s = org.apache.commons.lang.time.DurationFormatUtils.formatDurationHMS(breaksumMillis);
    System.out.println(s);
}

返回

0:00:23.000