public class EmployeeDaoImpl extends JdbcDaoSupportImpl implements EmployeeDao {
@Override
public int save(Employee employee) throws EmployeeException {
String sql = "INSERT INTO EMPLOYEE VALUES(?,?,?)";
Connection conToUse = null;
PreparedStatement ps = null;
int status = 0;
try {
conToUse = getConnection();
ps = conToUse.prepareStatement(sql);
ps.setInt(1, employee.getEmpNo());
ps.setString(2, employee.getEmpName());
ps.setLong(3, employee.getEmpSal());
status = ps.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
throw new EmployeeException(
"%%% Exception occured in EmployeeDao save() %%% " + e);
} finally {
DbUtils.closeQuietly(ps);
}
return status;
}
}
在示例代碼中,EmployeeDaoImpl類將插入記錄到數據庫中。如果發生任何異常,則將EmployeeException(應用程序異常)拋出到服務層。拋出用戶定義的異常需要什麼?在這種情況下,我們也可以拋出SQLException,是否正確?而不是重新拋出用戶定義的異常,我們可以重新拋出Dao中的SQLException和ClassNotFound異常嗎?