我的 DAO 实现中有以下方法:
@Override
public String getResultEntry(String arg1, String key, String date) throws SQLException, IOException {
String res = "";
String sql = buildSQL(arg1, key, date);
MapSqlParameterSource namedParameters = new MapSqlParameterSource();
namedParameters.addValue("arg1", arg1);
namedParameters.addValue("id", key);
namedParameters.addValue("date", date);
try {
logger.info("getResultEntry is about to execute the following query:[" + sql + "]");
Blob blob = namedParameterJdbcTemplate.queryForObject(sql, namedParameters, Blob.class);
long blobLength = blob.length();
byte[] bytes = blob.getBytes(1, (int)blobLength);
logger.info("getResultEntry END");
res = convertBytesToString(bytes);
} catch (EmptyResultDataAccessException erdae) {
logger.error("getResultEntry failed to execute the query due to exception! Return an empty string ", erdae);
logger.info("getResultEntry END");
res = "";
}
return res;
}
有时查询返回空结果并EmptyResultDataAccessException
抛出。我抓住它并返回一个空字符串。但是,我一直看到异常,之后执行终止。
例外:
org.springframework.dao.EmptyResultDataAccessException: Incorrect result size: expected 1, actual 0
at org.springframework.dao.support.DataAccessUtils.requiredSingleResult(DataAccessUtils.java:71)
at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.queryForObject(NamedParameterJdbcTemplate.java:179)
at org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate.queryForObject(NamedParameterJdbcTemplate.java:191)
at com.ars.linking.mrretriever.dao.MRRetrieverDAOImpl.getResultEntry(MRRetrieverDAOImpl.java:82)
at com.ars.linking.mrretriever.exec.MRRetriver.processResults(MRRetriver.java:125)
at com.ars.linking.mrretriever.exec.MRRetriver.main(MRRetriver.java:81)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:616)
at org.eclipse.jdt.internal.jarinjarloader.JarRsrcLoader.main(JarRsrcLoader.java:58)
我应该怎么做才能“正确”处理异常?(我没有兴趣重新抛出它,只是吞下它并返回一个空字符串)