繁体   English   中英

Java错误默认构造函数无法处理隐式超级构造函数引发的异常类型SQLException。 必须定义一个显式构造函数

[英]Java error Default constructor cannot handle exception type SQLException thrown by implicit super constructor. Must define an explicit constructor

我正在尝试在春季启动时获得jdbc连接,但我遇到了Java错误

“默认构造函数无法处理隐式超级构造函数引发的异常类型SQLException。必须定义一个显式构造函数。”

这是出现错误的连接对象。

Connection con = jdbcTemplate.getDataSource().getConnection();

下面是我尝试使用con对象的代码。

private String queryBuilder(DetailsRequest request, Map<String, String> headers) throws SQLException {

    StringBuilder AcctNameBuilder= new StringBuilder();
final String QUERY = "select * " +  "from gfc.LSI_ELGBLTY " + "where INSURANCE_ID = ? and " + "SYS_CD = ? and " + "ACCT_TYPE in (?)";
                prSt = con.prepareStatement(QUERY);
                prSt.setString(1, request.getInsuranceId());
                prSt.setString(2, request.getSystemId());
                prSt.setString(3, AcctNameBuilder.toString());
                rs = prSt.executeQuery();
                System.out.println(rs);
}

关于getConnection的原始实现:

Connection getConnection() throws SQLException;

这意味着getConnection可能会抛出SQLException ,并且在该构造函数中,您尝试获取连接而未正确处理该异常。

作为快速/肮脏的解决方法,您可以:

  • 将其包装在try catch语句中
  • 或者只是使构造函数抛出SQLException

另请注意,在构造函数中处理异常不是一种好习惯 ,因此建议不要这样做,并且您会找到一种更合适的方法。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM