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异常吗?