java.sql.SQLException:java.sql.Time的非法小时值’50’,值为’50:51:05

前端之家收集整理的这篇文章主要介绍了java.sql.SQLException:java.sql.Time的非法小时值’50’,值为’50:51:05前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
@H_301_1@当我尝试从MySQL检索时间并将其设置为JLabel时,它会给我一个错误.

@H_301_1@

@H_301_1@java.sql.sqlException: Illegal hour value ’50’ for java.sql.Time type
in value ’50:51:05.

@H_301_1@任何人都可以建议我如何解决这个问题?

@H_301_1@代码如下.

@H_301_1@

        String sql = "SELECT SEC_TO_TIME(SUM(TIME_TO_SEC(ot)))"
                + " FROM attendance"
                + " WHERE department = '"+department+"'"
                + "   AND date BETWEEN '"+dateChooser1+"' AND '"+dateChooser2+"'";
        st = con.createStatement();
        rst = st.executeQuery(sql);
        if(rst.next())
        {
            String time = rst.getString(1);
            oTimeTemp.setText(time);
        }
最佳答案
解决了这是代码,

@H_301_1@

String sql = "SELECT SUM(TIME_TO_SEC(ot))"
            + " FROM attendance"
            + " WHERE department = '"+department+"'"
            + " AND date BETWEEN '"+dateChooser1+"' AND '"+dateChooser2+"'";
    st = con.createStatement();
    rst = st.executeQuery(sql);
    if(rst.next())
    {
            String time = rst.getString(1);
            Double dTime = Double.valueOf(time);
            int intTime = (int) dTime.doubleValue();
            String nTime = calculateTime(intTime);
            oTimeTemp.setText(nTime);
    }

private static String calculateTime(int totalSecs)
{
    int hours = totalSecs / 3600;
    int minutes = (totalSecs % 3600) / 60;
    int seconds = totalSecs % 60;

    String timeString = String.format("%02d:%02d:%02d",hours,minutes,seconds);
    return timeString;
}
原文链接:https://www.f2er.com/mysql/432895.html

猜你在找的MySQL相关文章