2011-07-08 82 views
-5

我有一個名爲empbeans.java的bean,它調用dao類(userdao.java),用於將數據插入到名爲emp_leave的表中。它根據從session.i帶來的ename插入值一個小小的編碼問題,它顯示的信息是「Sorry!Could not Apply For Leave!」。userdao.java中是否有任何錯誤?plz解決。java代碼問題

這裏是代碼。

empbeans.java

public void apply(ActionEvent evt) { 
    ename = util.getSession().getAttribute("ename").toString(); 
    boolean done= userdao.apply(this); 
    if (done) { 
     reason = ""; 
     leavedate=""; 
     message = "Applied For Leave Successfully!"; 
    } 
    else 
     message = "Sorry! Could Not Apply For Leave!"; 
} 

userdao.java

public static boolean apply(empbeans e) { 
    Connection con = null; 
    PreparedStatement ps=null; 
    PreparedStatement ps1=null; 
    try { 

     con = Database.getConnection(); 
     ps1=con.prepareStatement("select eid from employee where ename=?"); 
     ps1.setString(1,e.getEname()); 
     ResultSet rs=ps1.executeQuery(); 
     ps = con.prepareStatement(
       "insert into emp_leaves values(?,?,?,default)"); 
     ps.setInt(1,rs.getInt(1)); 
     ps.setString(1,e.getLeavedate()); 
     ps.setString(2,e.getReason()); 
     int count = ps.executeUpdate(); 
     return count == 1; 
    } catch (Exception ex) { 
     System.out.println("Error in inserting into time sheet -->" + ex.getMessage()); 
     return false; 
    } finally { 
     Database.close(con); 
    } 
} 
+0

控制檯上沒有任何異常? –

+0

有沒有實際插入的數據庫?這可能是jdbc驅動程序的問題。 –

+1

恩......也許你真的不能申請假 – aldrin

回答

1

你已經錯過了rs.next();。因此,你會在ps.setInt(1,rs.getInt(1));

0

問題我不明白爲什麼申請方法需要變量EVT時,實際上方法從來沒有使用它。

然後請更改代碼以這是能夠理解返回什麼異常:

userdao.java

public static void apply(empbeans e) throws Exception{ 
    Connection con = null; 
    PreparedStatement ps=null; 
    PreparedStatement ps1=null; 
    try { 

     con = Database.getConnection(); 
     ps1=con.prepareStatement("select eid from employee where ename=?"); 
     ps1.setString(1,e.getEname()); 
     ResultSet rs=ps1.executeQuery(); 
     ps = con.prepareStatement(
       "insert into emp_leaves values(?,?,?,default)"); 
     ps.setInt(1,rs.getInt(1)); 
     ps.setString(1,e.getLeavedate()); 
     ps.setString(2,e.getReason()); 
     int count = ps.executeUpdate(); 
    }catch(Exception e) 
    {e.printStackTrace(); throw e;} 

} 

empbeans.java

public void apply(ActionEvent evt) { 
    String ename = util.getSession().getAttribute("ename").toString(); 
    try{ 
    userdao.apply(this); 
    reason = ""; 
    leavedate=""; 
    message="Leave applied successfully"; 
    }catch(Exception e) 
    { 
    message = "Sorry! Could Not Apply For Leave! reason:"+e.getMessage(); 
    } 
} 

請記住,錯誤不會被拋出。

從這裏你可以得到它不工作的確切原因,我猜測rs.next()。