Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

java.sql.SQLException: Illegal hour value '50' for java.sql.Time type in value '50:51:05

When I try to retrieve time from MySQL and set it to a JLabel, it gives me an error.

java.sql.SQLException: Illegal hour value '50' for java.sql.Time type in value '50:51:05.

Can anyone suggest me how to fix this?

Code is as follows.

        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);
        }
like image 553
H Athukorala Avatar asked Oct 28 '22 16:10

H Athukorala


2 Answers

I solved it. Here is the code,

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;
}
like image 61
H Athukorala Avatar answered Nov 15 '22 05:11

H Athukorala


There is a mysql bug , use it as follows it would work fine:

 String sql = "SELECT concate(SEC_TO_TIME(SUM(TIME_TO_SEC(ot))),'')"
 + " FROM attendance"
 + " WHERE department = '"+department+"'"
 + "   AND date BETWEEN '"+dateChooser1+"' AND '"+dateChooser2+"'";
like image 34
arpita Avatar answered Nov 15 '22 06:11

arpita