问题描述
首先,我想说的是,我在stackoverflow上检查了所有答案,但我无法修复此错误!请帮帮我!我花了很多时间,但没有结果.我正在尝试使用Tomcat8创建连接池.我有一个例外:
Firstly I want to say that i checked all answers at stackoverflow, and i can't fix this bug! Help me please! I spend a lot of time, but no result.I'm trying to create connection pool using Tomcat8.I have an exception:
DbConnector.class
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();
}
}
位于META-INF文件夹中的
context.xml
context.xml that located in META-INF folder
<?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 文件
<?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>
我的 tomcat/lib 目录的内容和我的项目结构.
Content of my tomcat/lib directory and my project structure.
推荐答案
JDBC URL不正确,实际上您缺少斜杠,因此请尝试以下操作:
The JDBC URL is not correct, indeed you have a missing slash so try this:
jdbc:mysql://localhost:3306/autopark
如果检查得很好,真正的错误是没有合适的驱动程序,这意味着它找不到支持所提供URL的任何JDBC驱动程序.
If you check well the real error is No suitable driver which means that it cannot find any JDBC driver that supports the provided URL.
这篇关于如何解决“无法为连接URL创建类'com.mysql.jdbc.Driver'的JDBC驱动程序"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!