先直接说结论:
c3p0 jar包 和 Apache-commons-dbutils的jar包不匹配(我使用的是Apache-commons-dbutils来操作数据库)
c3p0-0.9.5.2.jar + commons-dbutils-1.7.jar 改为 c3p0-0.9.1.2.jar + commons-dbutils-1.6.jar
这个错浪费我2个小时时间,问题得到解决,具体信息如下:
PS:遇到类似错误,多半是jar包不全或是有冲突或是不匹配。
1、Servlet类
public class RegisterServlet extends HttpServlet {
private static final long serialVersionUID = 1L;
private UserService userService = new UserServiceImpl();
public RegisterServlet() {
super();
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String method = request.getParameter("method");
System.out.println("method="+method);
Map<String, String[]> params = request.getParameterMap();
for(Map.Entry<String, String[]> entry : params.entrySet()) {
System.out.println(entry.getKey());
}
User user = new User();
try {
BeanUtils.populate(user, params);
user.setId(UUIDGenerater.getUUID());
user.setGender(0);
user.setBirthday("2018-12-4 12:11:34");
user.setActivationCode("sgsgeyg");
user.setIsActivated(0);
user.setRegTime("2018-12-4 12:11:34");
user.setHeadPortraitId("sdgsdg");
user.setLastLoginTime("2018-12-4 12:11:34");
user.setMemberLevel(1);
userService.addUser(user);
} catch (IllegalAccessException e1) {
e1.printStackTrace();
} catch (InvocationTargetException e1) {
e1.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}
}
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
doPost(request, response);
}
}
2、service下是dao层,到层加载c3p0
public class UserServiceImpl implements UserService {
private UserDao userDao = new UserDaoImpl();
@Override
public int addUser(User user) throws SQLException {
return userDao.addUser(user);
}
}
public class UserDaoImpl implements UserDao {
private QueryRunner qr = new TxQueryRunner(JdbcC3P0Utils.getDataSource());
@Override
public int addUser(User user) throws SQLException {
String sql = "INSERT INTO T_USER(id, name, gender, password,nickName,email,address,cellPhoneNumber,birthday,activationCode,isActivated,regTime,headPortraitId," +
"lastLoginTime,memberLevel) VALUES(?, ?, ?, ?,?,?, ?, ?, ?,?,?, ?, ?, ?,?)";
Object[] params = {user.getId(),user.getUsername(),user.getGender(),user.getPassword(),user.getNickName(),
user.getEmail(),user.getAddress(),user.getCellPhoneNumber(),user.getBirthday(),user.getActivationCode(),
user.getIsActivated(),user.getRegTime(),user.getHeadPortraitId(),user.getLastLoginTime(),user.getMemberLevel()};
int num = qr.update(sql, params);
return num;
}
}
3、数据源工具类:
public class JdbcC3P0Utils {
//数据源
private static DataSource ds = new ComboPooledDataSource();
//本地线程,解决线程安全问题
private static ThreadLocal<Connection> tl = new ThreadLocal<>();
}
4、报错入下:
java.lang.ClassNotFoundException: com.mchange.v2.ser.Indirector
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1352)
at org.apache.catalina.loader.WebappClassLoaderBase.loadClass(WebappClassLoaderBase.java:1180)
at pers.liyonghong.mall.utils.JdbcC3P0Utils.<clinit>(JdbcC3P0Utils.java:21)
at pers.liyonghong.mall.dao.impl.UserDaoImpl.<init>(UserDaoImpl.java:18)
at pers.liyonghong.mall.service.impl.UserServiceImpl.<init>(UserServiceImpl.java:17)
at pers.liyonghong.mall.web.servlet.RegisterServlet.<init>(RegisterServlet.java:26)
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at org.apache.catalina.core.DefaultInstanceManager.newInstance(DefaultInstanceManager.java:151)
at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1085)
at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:811)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:135)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:94)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:492)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:141)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:80)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:620)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:88)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:502)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1152)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:684)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1539)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.run(NioEndpoint.java:1495)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
5、报错时,工程jar包
6、修正后jar包