org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver'

背景:

把连接池操控数据库的java程序做成jar包导致如下错误

报错如下:

org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver'
    at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:766)
    at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:540)
    at cn.com.jdbc.JdbcUtils_DBCP.getConnection(JdbcUtils_DBCP.java:48)
    at cn.com.Socket.ThreadReader.insert_num(ThreadReader.java:153)
    at cn.com.Socket.ThreadReader.othernum(ThreadReader.java:135)
    at cn.com.Socket.ThreadReader.read(ThreadReader.java:74)
    at cn.com.Socket.ThreadReader.Port(ThreadReader.java:39)
    at cn.com.Socket.ThreadReader.run(ThreadReader.java:25)
    at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.ClassNotFoundException: com.mysql.jdbc.Driver
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.net.URLClassLoader$1.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.net.URLClassLoader.findClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
    at java.lang.ClassLoader.loadClass(Unknown Source)
    at java.lang.Class.forName0(Native Method)
    at java.lang.Class.forName(Unknown Source)
    at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:760)
    ... 8 more

原因:

使用连接池dbcp,虽然引用了commons-dbcp-1.2.jar 、commons-pool-1.6.jar,但是少了mysql-connector-java-5.1.8-bin.jar

加上这个jar包就好了!

 

你可能感兴趣的:(org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class 'com.mysql.jdbc.Driver')