我想在java中获得员工可用的叶子数量

时间:2015-11-10 07:16:15

标签: java

CONSTClass otherClass = new CONSTClass();
availableLeave = otherClass.serviceperiod(leaveType);

CONSTClass

private int calDaysDiff(){
    String sql = "SELECT `joining` FROM `employee` WHERE `EMP_ID`='"+empId+"'";
    Conn c=new Conn();
    Date dateFromSql=null;

    try {
        Connection conn=c.createConn();
        Statement s=conn.createStatement();
        ResultSet rs=s.executeQuery(sql);
        while (rs.next()) {
           sdate=rs.getString(1);
        }

        dateFromSql = dateFormat.parse(sdate);
        System.out.println(sdate);     
    } catch (Exception e) {
        e.printStackTrace();
    }

    Calendar cal=Calendar.getInstance(); 
    cal.setTime(dateFromSql);
    Calendar cal2=Calendar.getInstance();
    cal2.setTime(cal2.getTime());   
    int daysDiff =(int)((cal2.getTimeInMillis()-cal.getTimeInMillis())/(1000*3600*24));
    return daysDiff;
}

public int serviceperiod(String Type){
    int daysDiff = calDaysDiff();
    int deductyear= daysDiff - 365;
    int days = 0;
    String sql = "SELECT numOfChildren FROM employee WHERE EMP_ID = '"+empId+"'";

    String sql2= null;
    Conn c=new Conn();
    int numchildren= Integer.parseInt(sql);

    try {
        Connection conn=c.createConn();
        Statement s=conn.createStatement();
        numchildren = Integer.parseInt(sql);
        System.out.println(sql);  
    } 
    catch (Exception e) {
        e.printStackTrace();
    }

    if(daysDiff >= 365){  
        if(Type == "Casual"){
            sql2 = "SELECT `LeaveNo` FROM `leave` WHERE `Leave_Id`= 'LV_1'";
        }
        else if(Type == "Medical"){
            sql2 = "SELECT `LeaveNo` FROM `leave` WHERE `Leave_Id`= 'LV_2'";
        }
        else if(Type == "Annual"){
            if(deductyear <=90 && deductyear >0){
              sql2 = "SELECT `LeaveNo` FROM `leave` WHERE `Leave_Id`= 'LV_3'";
            }
            else if(deductyear <=181 && deductyear >=90){
              sql2 = "SELECT `LeaveNo` FROM `leave` WHERE `Leave_Id`= 'LV_4'";   
            }
            else if (deductyear <=273 && deductyear >=181){
               sql2 = "SELECT `LeaveNo` FROM `leave` WHERE `Leave_Id`= 'LV_5'"; 
            }
            else if (deductyear <=366 && deductyear >=273){
               sql2 = "SELECT `LeaveNo` FROM `leave` WHERE `Leave_Id`= 'LV_6'"; 
            }
            else{
                sql2 = "SELECT `LeaveNo` FROM `leave` WHERE `Leave_Id`= 'LV_7'"; 
            }
        }
        else if(Type == "Maternity"){
            if(numchildren<=2){
                sql2 = "SELECT `LeaveNo` FROM `leave` WHERE `Leave_Id`= 'LV_8'";
            }
            else{
                sql2 = "SELECT `LeaveNo` FROM `leave` WHERE `Leave_Id`= 'LV_9'";
            }
        }
        else if (Type == "Short_Leave"){
            sql2 = "SELECT `LeaveNo` FROM `leave` WHERE `Leave_Id`= 'LV_10'";
        }
        days = Integer.parseInt(sql2);
        System.out.println(sql2);
     }
     else{
        days = 0;
     }
     return days;
 }

calDaysDiff()方法的作用是计算员工加入日期与今天之间的差异。 public int serviceperiod(String Type)为员工提供休假。

点击与休假类型相关的下拉项后,它应自动计算可用的树叶数并显示在文本框中。我想知道代码中有什么问题吗?

1 个答案:

答案 0 :(得分:0)

看起来像是

 ResultSet rs=s.executeQuery(sql);

没有给出结果。所以

 while (rs.next()) {
       sdate=rs.getString(1);

    }

没有填写sdate。如果你想解析它 dateFromSql = dateFormat.parse(sdate); ,你得到NullPointerException。