Bean实体的不规范写法,所导致的奇葩SQLException

我们在使用DBCP作为数据源(由Apache开发的),使用Apache下的开源数据源,利用DBCPUtil工具操作数据源,对数据进行增删改查。

我们会常常遇到这种问题:

二月 18, 2016 1:49:43 下午 org.apache.catalina.core.StandardWrapperValve invoke
严重: Servlet.service() for servlet [com.xbmu.android.ShowOrderExamServlet] in context with path [/DriverHelperServer] threw exception
java.lang.RuntimeException: java.sql.SQLException: Cannot create com.xbmu.bean.OrderExam: com.xbmu.bean.OrderExam Query: select * from t_orderexam where student_id=? Parameters: [p10001]
at com.xbmu.dao.impl.OrderExamDaoImpl.findOrderExamByStuId(OrderExamDaoImpl.java:50)
at com.xbmu.service.impl.OrderExamServiceImpl.findOrderExamByStuId(OrderExamServiceImpl.java:23)
at com.xbmu.android.ShowOrderExamServlet.showOrderExam(ShowOrderExamServlet.java:42)
at com.xbmu.android.ShowOrderExamServlet.doGet(ShowOrderExamServlet.java:33)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:620)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at com.xbmu.filter.SetAllCharacterEncodingFilter.doFilter(SetAllCharacterEncodingFilter.java:41)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1074)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.doRun(AprEndpoint.java:2466)
at org.apache.tomcat.util.net.AprEndpoint$SocketProcessor.run(AprEndpoint.java:2455)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:724)
Caused by: java.sql.SQLException: Cannot create com.xbmu.bean.OrderExam: com.xbmu.bean.OrderExam Query: select * from t_orderexam where student_id=? Parameters: [p10001]
at org.apache.commons.dbutils.AbstractQueryRunner.rethrow(AbstractQueryRunner.java:320)
at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:349)
at org.apache.commons.dbutils.QueryRunner.query(QueryRunner.java:287)
at com.xbmu.dao.impl.OrderExamDaoImpl.findOrderExamByStuId(OrderExamDaoImpl.java:47)
... 29 more

我们观察所写的代码:

Bean实体的不规范写法,所导致的奇葩SQLException_第1张图片

根据异常的提示,是47行报错,但是我们观察sql语句一点问题都没有,因此,我们推断到实体Bean,也就是OrderExam的问题。

最后仔细观察,原来没有重写OrderExam实体Bean的无参数构造方法。当我重写了无参数的构造方法后,这种异常被就解决了。


仔细想想,OrderExam orderExam = qr.query(sql, new BeanHandler<OrderExam>(OrderExam.class), sid);这条语句将从数据库查询的语句通过反射机制映射成实体Bean。

OrderExam.class创建对象时,应该是调用了无参数的构造方法。因此实体Bean中必须写这个无参数构造方法。















你可能感兴趣的:(SQLException)