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);
}
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;
}
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+"'";
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With