java.sql.SQLException:无法在 IntelliJ Web 应用程序中为连接 URL 'null' 创建类 '' 的 JDBC 驱动程序
Posted
技术标签:
【中文标题】java.sql.SQLException:无法在 IntelliJ Web 应用程序中为连接 URL \'null\' 创建类 \'\' 的 JDBC 驱动程序【英文标题】:java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'null' in IntelliJ web applicationjava.sql.SQLException:无法在 IntelliJ Web 应用程序中为连接 URL 'null' 创建类 '' 的 JDBC 驱动程序 【发布时间】:2021-11-16 00:01:52 【问题描述】:我正在尝试使用 IntelliJ 上的 servlet 构建一个小型 Web 应用程序。我正在尝试使用 JDBC 将 mysql DB 与应用程序连接起来。
我有所需的 Maven 依赖项,检查了我的 context.xml 文件。 我手动尝试使用已成功连接的 IntelliJ 上的 GUI 连接 MySQL DB。
<Context>
<Resource name="jdbc/student_tracker"
auth="Container" type="javax.sql.DataSource"
maxActive="20" maxIdle="5" maxWait="10000"
username="webstudent" password="webstudent"
driverClassName="com.mysql.jdbc.Driver"
url="jdbc:mysql://localhost:3306/web_student_tracker"/>
</Context>
这里是 Servlet
@WebServlet(name = "TestServlet", value = "/TestServlet")
public class TestServlet extends HttpServlet
private static final long serialVersionUID = 1L;
// Define datasource/connection pool for Resource Injection
@Resource(name="jdbc/student_tracker")
private DataSource dataSource;
@Override
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
// Step 1: Set up the printwriter
PrintWriter out = response.getWriter();
response.setContentType("text/plain");
// Step 2: Get a connection to the database
Connection myConn = null;
Statement myStmt = null;
ResultSet myRs = null;
try
myConn = dataSource.getConnection();
// Step 3: Create a SQL statements
String sql = "select * from student";
myStmt = myConn.createStatement();
// Step 4: Execute SQL query
myRs = myStmt.executeQuery(sql);
// Step 5: Process the result set
while (myRs.next())
String email = myRs.getString("email");
out.println(email);
catch (Exception exc)
exc.printStackTrace();
错误
java.sql.SQLException: Cannot create JDBC driver of class '' for connect URL 'null'
at org.apache.tomcat.dbcp.dbcp2.DriverFactory.createDriver(DriverFactory.java:74)
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createConnectionFactory(BasicDataSource.java:472)
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:538)
at org.apache.tomcat.dbcp.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:744)
at com.application.student_tracker.TestServlet.doGet(TestServlet.java:36)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:655)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:764)
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:52)
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:196)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:544)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:135)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:81)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:698)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:364)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:624)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:831)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1650)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191)
at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
Caused by: java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(DriverManager.java:315)
at org.apache.tomcat.dbcp.dbcp2.DriverFactory.createDriver(DriverFactory.java:59)
【问题讨论】:
好像您的上下文文件没有部署到 Tomcat。您是如何将其添加到您的项目中的?它位于何处? 【参考方案1】:如果您将 JNDI 用于 DataSource,您必须在 $CATALINA_HOME/lib.
中拥有 JDBC 驱动程序,请将 mysql-connector
复制到 $CATALINA_HOME/lib
也将其从您的 WEB-INF/lib
中删除
参考号:JNDI Datasource HOW-TO
【讨论】:
以上是关于java.sql.SQLException:无法在 IntelliJ Web 应用程序中为连接 URL 'null' 创建类 '' 的 JDBC 驱动程序的主要内容,如果未能解决你的问题,请参考以下文章
java.Sql.SQLException,无法加载类 net.ucanaccess.jdbc.UcanaccessDriver
java.sql.SQLException:无法锁定表“更改”;用户当前正在使用
java.sql.SQLException:无法在 IntelliJ Web 应用程序中为连接 URL 'null' 创建类 '' 的 JDBC 驱动程序
java.sql.SQLException:无法识别服务器时区值“Hora de verano GMT”[重复]
java.sql.SQLException:无法从底层数据库获取连接! — HSQLDB
无法获得 JDBC 连接;嵌套异常是 java.sql.SQLException:无法加载 JDBC 驱动程序类 'org.hsql.jdbcDriver'