首先我想说我检查了 stackoverflow 上的所有答案,但我无法修复这个错误!请帮帮我!我花了很多时间,却没有任何结果。
我正在尝试使用 Tomcat8 创建连接池。
我有一个例外:
java.sql.SQLException:无法创建类的 JDBC 驱动程序
'com.mysql.jdbc.Driver' 用于连接 URL
'jdbc:mysql:/localhost:3306/autopark' 位于
org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2160)
在
org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:2032)
在
org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1532)
在 ua.khpi.shapoval.db.DbConnector.init(DbConnector.java:31) 处
ua.khpi.shapoval.db.DbContextListner.contextInitialized(DbContextListner.java:48)
在
org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4842)
在
org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5303)
在
org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:147)
在
org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1407)
在
org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1397)
在 java.util.concurrent.FutureTask.run(FutureTask.java:266) 处
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
在
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
在 java.lang.Thread.run(Thread.java:745) 引起的:
java.sql.SQLException:没有合适的驱动程序
org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2151)
... 13 更多
数据库连接器类
public class DbConnector {
private static Logger log = Logger.getLogger(DbConnector.class.getName());
private static DataSource dataSource;
private static Connection connection;
public static void init() throws ServletException, SQLException, NamingException, ClassNotFoundException {
Context initCtx = new InitialContext();
Context envCtx = (Context) initCtx.lookup("java:comp/env/");
DataSource ds = (DataSource) envCtx.lookup("jdbc/autopark");
System.out.println(ds.getConnection());
}
public static Connection getConnection() throws SQLException {
return dataSource.getConnection();
}
}
上下文.xml位于 META-INF 文件夹中
<?xml version="1.0" encoding="UTF-8"?>
<Context crossContext="true" reloadable="true">
<Resource name="jdbc/autopark" auth="Container" type="javax.sql.DataSource"
username="root" password="161acid161" driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql:/localhost:3306/autopark" maxActive="15" maxIdle="3" />
<ResourceLink name="jdbc/autopark" global="jdbc/autopark"
type="javax.sql.DataSource" />
</Context>
web.xml file
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
id="WebApp_ID" version="3.1">
<display-name>Autostation</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<resource-ref>
<description>Db</description>
<res-ref-name>jdbc/autopark</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<listener>
<listener-class>ua.khpi.shapoval.db.DbContextListner</listener-class>
</listener>
</web-app>
Content of my tomcat/lib directory and my project structure.
![enter image description here](https://i.stack.imgur.com/7pgJl.png)
JDBC URL 不正确,确实缺少斜杠,因此请尝试以下操作:
jdbc:mysql://localhost:3306/autopark
如果你仔细检查的话,真正的错误是没有合适的驱动程序这意味着它找不到任何支持所提供 URL 的 JDBC 驱动程序。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)