无法始终为连接 URL 'null 创建 class '' 的 JDBC 驱动程序
Cannot create JDBC driver of class '' for connect URL 'null all the time
您好,我遇到以下问题:
每当我尝试连接到 MySQL 数据库时,我都会收到此错误:
com.pbs.web.jdbc.ParkingBookSystem.BookControllerServlet doGet
SEVERE: null
**java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'null'**
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2224)
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:2104)
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1563)
at com.pbs.web.jdbc.ParkingBookSystem.BookDbUtil.doesBookExist(BookDbUtil.java:91)
at com.pbs.web.jdbc.ParkingBookSystem.BookControllerServlet.addBook(BookControllerServlet.java:117)
at com.pbs.web.jdbc.ParkingBookSystem.BookControllerServlet.doGet(BookControllerServlet.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:770)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
**Caused by: java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(Unknown Source)**
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2209)
... 29 more
我有 context.xml 放在: C:\Users\userName\eclipse-workspace\WebAppName\src\main\resources\META-INF
这是代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Context>
<Resource
name="ParkingBookSystem"
auth="Container"
type="javax.sql.DataSource"
maxTotal="20"
maxIdle="5"
maxWaitMillis="10000"
username="sql2226123"
password="cccccccc"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://sql2.freemysqlhosting.net:3306/sql2226123"
/>
</Context>
和我的web.xml(放置在C:\Users\userName\eclipse-workspace\WebAppName\src\main\webapp\WEB-INF):
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<description>ParkingBookSystem</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>/ParkingBookSystem</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
这是我的 class 建立数据库连接的构造函数:
// constructor
public BookDbUtil() {
try {
Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env");
dataSource = (DataSource) envContext.lookup("ParkingBookSystem");
} catch (NamingException e) {
throw new IllegalStateException("Could not initialize database", e);
}
}
我正在使用 Maven、Eclipse 和 Tomcat 这是我迄今为止尝试过的:
- 我把 mysql-connector-java-5.1.38-bin 放到 tomcat lib
- 手动把context.xml放到C:\Program Files\Apache Software Foundation\Tomcat 9.0\conf\Catalina\localhost
- 如上,但将名称从 context.xml 更改为 projectName.xml
- 将所有内容放入 server.xml(并注释掉此处的默认资源)
- 我还发现 tomcat 文件中的每一个更改都需要管理员权限,所以我为每个用户添加了完全控制以确保不是这种情况。
我没有更多的想法,任何人都可以提供帮助?
请;
将 mysql 连接器 jar 文件放入 WEB-INF/lib 目录
并更改 context.xml
<Context>
<Resource name="jdbc/ParkingBookSystem" ...
并更改 web.xml
<res-ref-name>jdbc/ParkingBookSystem</res-ref-name>
并更改查找
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
dataSource = (DataSource)envContext.lookup("jdbc/ParkingBookSystem");
好的,我找到了解决方案...问题是我的 META-INF 和 context.xml 在 C:\Users\lukas\eclipse-workspace\ParkingBookSystem\src\main\resources\META-INF\context.xml 中,当我将它放入:
C:\Users\lukas\eclipse-workspace\ParkingBookSystem\src\main\webapp
它开始工作了。
您好,我遇到以下问题: 每当我尝试连接到 MySQL 数据库时,我都会收到此错误:
com.pbs.web.jdbc.ParkingBookSystem.BookControllerServlet doGet
SEVERE: null
**java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'null'**
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2224)
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:2104)
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1563)
at com.pbs.web.jdbc.ParkingBookSystem.BookDbUtil.doesBookExist(BookDbUtil.java:91)
at com.pbs.web.jdbc.ParkingBookSystem.BookControllerServlet.addBook(BookControllerServlet.java:117)
at com.pbs.web.jdbc.ParkingBookSystem.BookControllerServlet.doGet(BookControllerServlet.java:70)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:634)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:741)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:490)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:668)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:74)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:408)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:770)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1415)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
**Caused by: java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(Unknown Source)**
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:2209)
... 29 more
我有 context.xml 放在: C:\Users\userName\eclipse-workspace\WebAppName\src\main\resources\META-INF 这是代码:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xml>
<Context>
<Resource
name="ParkingBookSystem"
auth="Container"
type="javax.sql.DataSource"
maxTotal="20"
maxIdle="5"
maxWaitMillis="10000"
username="sql2226123"
password="cccccccc"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://sql2.freemysqlhosting.net:3306/sql2226123"
/>
</Context>
和我的web.xml(放置在C:\Users\userName\eclipse-workspace\WebAppName\src\main\webapp\WEB-INF):
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<description>ParkingBookSystem</description>
<resource-ref>
<description>DB Connection</description>
<res-ref-name>/ParkingBookSystem</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
</welcome-file-list>
</web-app>
这是我的 class 建立数据库连接的构造函数:
// constructor
public BookDbUtil() {
try {
Context initContext = new InitialContext();
Context envContext = (Context) initContext.lookup("java:/comp/env");
dataSource = (DataSource) envContext.lookup("ParkingBookSystem");
} catch (NamingException e) {
throw new IllegalStateException("Could not initialize database", e);
}
}
我正在使用 Maven、Eclipse 和 Tomcat 这是我迄今为止尝试过的:
- 我把 mysql-connector-java-5.1.38-bin 放到 tomcat lib
- 手动把context.xml放到C:\Program Files\Apache Software Foundation\Tomcat 9.0\conf\Catalina\localhost
- 如上,但将名称从 context.xml 更改为 projectName.xml
- 将所有内容放入 server.xml(并注释掉此处的默认资源)
- 我还发现 tomcat 文件中的每一个更改都需要管理员权限,所以我为每个用户添加了完全控制以确保不是这种情况。
我没有更多的想法,任何人都可以提供帮助?
请;
将 mysql 连接器 jar 文件放入 WEB-INF/lib 目录
并更改 context.xml
<Context>
<Resource name="jdbc/ParkingBookSystem" ...
并更改 web.xml
<res-ref-name>jdbc/ParkingBookSystem</res-ref-name>
并更改查找
Context initContext = new InitialContext();
Context envContext = (Context)initContext.lookup("java:/comp/env");
dataSource = (DataSource)envContext.lookup("jdbc/ParkingBookSystem");
好的,我找到了解决方案...问题是我的 META-INF 和 context.xml 在 C:\Users\lukas\eclipse-workspace\ParkingBookSystem\src\main\resources\META-INF\context.xml 中,当我将它放入: C:\Users\lukas\eclipse-workspace\ParkingBookSystem\src\main\webapp 它开始工作了。