MySQLNonTransientConnectionException 无法创建与数据库服务器的连接。Caused by: java.lang.NullPointerException

2022-09-02 19:18:03

我有一个本地MySQL社区服务器(8.0.11)在本地主机上运行。127.0.0.1:3306

(我创建了一个数据库并为其注册了一个用户)。我可以使用以下终端查询来访问数据库:./mysql -u testuser -p

并且我能够访问服务器。

我使用Spring和JDBC使用以下代码做同样的事情:

private final String URL= "jdbc:mysql://127.0.0.1:3306/db_example?useSSL=false";
private final String DB_USERNAME = "testuser";
private final String DB_PASSWORD = "pass";


public void connectAndInsertToDB() {
    try {
        Class.forName("com.mysql.jdbc.Driver").newInstance();
    } catch (Exception e) {
        System.out.println("JAVA: Class.forName() error");
        e.printStackTrace();
    }
    try {

        conn = DriverManager.getConnection(URL, DB_USERNAME, DB_PASSWORD);
    } catch (SQLException e) {
        System.out.println("Error in initializing a connection to MYSQL DB");
        e.printStackTrace();

    }
}

但后来我得到以下错误:

com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server.
23:03:28,603 ERROR [stderr] (default task-3)    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
23:03:28,603 ERROR [stderr] (default task-3)    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
23:03:28,604 ERROR [stderr] (default task-3)    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
23:03:28,607 ERROR [stderr] (default task-3)    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
23:03:28,608 ERROR [stderr] (default task-3)    at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
23:03:28,608 ERROR [stderr] (default task-3)    at com.mysql.jdbc.Util.getInstance(Util.java:387)
23:03:28,608 ERROR [stderr] (default task-3)    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:917)
23:03:28,609 ERROR [stderr] (default task-3)    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:896)
23:03:28,609 ERROR [stderr] (default task-3)    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:885)
23:03:28,609 ERROR [stderr] (default task-3)    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:860)
23:03:28,610 ERROR [stderr] (default task-3)    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2332)
23:03:28,611 ERROR [stderr] (default task-3)    at com.mysql.jdbc.ConnectionImpl.createNewIO(ConnectionImpl.java:2085)
23:03:28,611 ERROR [stderr] (default task-3)    at com.mysql.jdbc.ConnectionImpl.<init>(ConnectionImpl.java:795)
23:03:28,611 ERROR [stderr] (default task-3)    at com.mysql.jdbc.JDBC4Connection.<init>(JDBC4Connection.java:44)
23:03:28,611 ERROR [stderr] (default task-3)    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
23:03:28,612 ERROR [stderr] (default task-3)    at sun.reflect.NativeConstructorAccessorImpl.newInstance(NativeConstructorAccessorImpl.java:62)
23:03:28,612 ERROR [stderr] (default task-3)    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(DelegatingConstructorAccessorImpl.java:45)
23:03:28,612 ERROR [stderr] (default task-3)    at java.lang.reflect.Constructor.newInstance(Constructor.java:422)
23:03:28,612 ERROR [stderr] (default task-3)    at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
23:03:28,613 ERROR [stderr] (default task-3)    at com.mysql.jdbc.ConnectionImpl.getInstance(ConnectionImpl.java:400)
23:03:28,613 ERROR [stderr] (default task-3)    at com.mysql.jdbc.NonRegisteringDriver.connect(NonRegisteringDriver.java:327)
23:03:28,613 ERROR [stderr] (default task-3)    at java.sql.DriverManager.getConnection(DriverManager.java:664)
23:03:28,613 ERROR [stderr] (default task-3)    at java.sql.DriverManager.getConnection(DriverManager.java:247)
23:03:28,613 ERROR [stderr] (default task-3)    at com.voice4.spring.security.config.core.Inventory.connectAndInsertToDB(Inventory.java:41)
23:03:28,614 ERROR [stderr] (default task-3)    at com.voice4.spring.security.config.core.KomalController.indexPage(KomalController.java:36)
23:03:28,614 ERROR [stderr] (default task-3)    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
23:03:28,614 ERROR [stderr] (default task-3)    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
23:03:28,614 ERROR [stderr] (default task-3)    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
23:03:28,614 ERROR [stderr] (default task-3)    at java.lang.reflect.Method.invoke(Method.java:497)
23:03:28,615 ERROR [stderr] (default task-3)    at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:221)
23:03:28,615 ERROR [stderr] (default task-3)    at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136)
23:03:28,615 ERROR [stderr] (default task-3)    at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:114)
23:03:28,616 ERROR [stderr] (default task-3)    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
23:03:28,616 ERROR [stderr] (default task-3)    at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
23:03:28,616 ERROR [stderr] (default task-3)    at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
23:03:28,616 ERROR [stderr] (default task-3)    at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
23:03:28,617 ERROR [stderr] (default task-3)    at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
23:03:28,617 ERROR [stderr] (default task-3)    at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
23:03:28,617 ERROR [stderr] (default task-3)    at org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:861)
23:03:28,617 ERROR [stderr] (default task-3)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:687)
23:03:28,618 ERROR [stderr] (default task-3)    at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
23:03:28,618 ERROR [stderr] (default task-3)    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
23:03:28,618 ERROR [stderr] (default task-3)    at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
23:03:28,619 ERROR [stderr] (default task-3)    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)
23:03:28,619 ERROR [stderr] (default task-3)    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:317)
23:03:28,620 ERROR [stderr] (default task-3)    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.invoke(FilterSecurityInterceptor.java:127)
23:03:28,621 ERROR [stderr] (default task-3)    at org.springframework.security.web.access.intercept.FilterSecurityInterceptor.doFilter(FilterSecurityInterceptor.java:91)
23:03:28,621 ERROR [stderr] (default task-3)    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
23:03:28,622 ERROR [stderr] (default task-3)    at org.springframework.security.web.access.ExceptionTranslationFilter.doFilter(ExceptionTranslationFilter.java:115)
23:03:28,622 ERROR [stderr] (default task-3)    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
23:03:28,622 ERROR [stderr] (default task-3)    at org.springframework.security.web.session.SessionManagementFilter.doFilter(SessionManagementFilter.java:137)
23:03:28,623 ERROR [stderr] (default task-3)    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
23:03:28,623 ERROR [stderr] (default task-3)    at org.springframework.security.web.authentication.AnonymousAuthenticationFilter.doFilter(AnonymousAuthenticationFilter.java:111)
23:03:28,624 ERROR [stderr] (default task-3)    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
23:03:28,624 ERROR [stderr] (default task-3)    at org.springframework.security.web.servletapi.SecurityContextHolderAwareRequestFilter.doFilter(SecurityContextHolderAwareRequestFilter.java:169)
23:03:28,625 ERROR [stderr] (default task-3)    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
23:03:28,625 ERROR [stderr] (default task-3)    at org.springframework.security.web.savedrequest.RequestCacheAwareFilter.doFilter(RequestCacheAwareFilter.java:63)
23:03:28,625 ERROR [stderr] (default task-3)    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
23:03:28,625 ERROR [stderr] (default task-3)    at org.springframework.security.web.authentication.AbstractAuthenticationProcessingFilter.doFilter(AbstractAuthenticationProcessingFilter.java:200)
23:03:28,625 ERROR [stderr] (default task-3)    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
23:03:28,626 ERROR [stderr] (default task-3)    at org.springframework.security.web.authentication.logout.LogoutFilter.doFilter(LogoutFilter.java:121)
23:03:28,626 ERROR [stderr] (default task-3)    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
23:03:28,626 ERROR [stderr] (default task-3)    at org.springframework.security.web.csrf.CsrfFilter.doFilterInternal(CsrfFilter.java:100)
23:03:28,626 ERROR [stderr] (default task-3)    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
23:03:28,627 ERROR [stderr] (default task-3)    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
23:03:28,627 ERROR [stderr] (default task-3)    at org.springframework.security.web.header.HeaderWriterFilter.doFilterInternal(HeaderWriterFilter.java:66)
23:03:28,627 ERROR [stderr] (default task-3)    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
23:03:28,627 ERROR [stderr] (default task-3)    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
23:03:28,627 ERROR [stderr] (default task-3)    at org.springframework.security.web.context.SecurityContextPersistenceFilter.doFilter(SecurityContextPersistenceFilter.java:105)
23:03:28,627 ERROR [stderr] (default task-3)    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
23:03:28,628 ERROR [stderr] (default task-3)    at org.springframework.security.web.context.request.async.WebAsyncManagerIntegrationFilter.doFilterInternal(WebAsyncManagerIntegrationFilter.java:56)
23:03:28,628 ERROR [stderr] (default task-3)    at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
23:03:28,628 ERROR [stderr] (default task-3)    at org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:331)
23:03:28,628 ERROR [stderr] (default task-3)    at org.springframework.security.web.FilterChainProxy.doFilterInternal(FilterChainProxy.java:214)
23:03:28,628 ERROR [stderr] (default task-3)    at org.springframework.security.web.FilterChainProxy.doFilter(FilterChainProxy.java:177)
23:03:28,629 ERROR [stderr] (default task-3)    at org.springframework.web.filter.DelegatingFilterProxy.invokeDelegate(DelegatingFilterProxy.java:346)
23:03:28,629 ERROR [stderr] (default task-3)    at org.springframework.web.filter.DelegatingFilterProxy.doFilter(DelegatingFilterProxy.java:262)
23:03:28,629 ERROR [stderr] (default task-3)    at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
23:03:28,629 ERROR [stderr] (default task-3)    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
23:03:28,629 ERROR [stderr] (default task-3)    at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
23:03:28,630 ERROR [stderr] (default task-3)    at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
23:03:28,630 ERROR [stderr] (default task-3)    at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
23:03:28,630 ERROR [stderr] (default task-3)    at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
23:03:28,630 ERROR [stderr] (default task-3)    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
23:03:28,630 ERROR [stderr] (default task-3)    at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
23:03:28,631 ERROR [stderr] (default task-3)    at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
23:03:28,631 ERROR [stderr] (default task-3)    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
23:03:28,631 ERROR [stderr] (default task-3)    at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
23:03:28,631 ERROR [stderr] (default task-3)    at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
23:03:28,632 ERROR [stderr] (default task-3)    at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
23:03:28,632 ERROR [stderr] (default task-3)    at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
23:03:28,633 ERROR [stderr] (default task-3)    at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
23:03:28,633 ERROR [stderr] (default task-3)    at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
23:03:28,633 ERROR [stderr] (default task-3)    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
23:03:28,634 ERROR [stderr] (default task-3)    at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
23:03:28,634 ERROR [stderr] (default task-3)    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
23:03:28,634 ERROR [stderr] (default task-3)    at org.wildfly.extension.undertow.deployment.GlobalRequestControllerHandler.handleRequest(GlobalRequestControllerHandler.java:68)
23:03:28,634 ERROR [stderr] (default task-3)    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
23:03:28,635 ERROR [stderr] (default task-3)    at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
23:03:28,635 ERROR [stderr] (default task-3)    at io.undertow.servlet.handlers.ServletInitialHandler.access$100(ServletInitialHandler.java:81)
23:03:28,636 ERROR [stderr] (default task-3)    at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:138)
23:03:28,636 ERROR [stderr] (default task-3)    at io.undertow.servlet.handlers.ServletInitialHandler$2.call(ServletInitialHandler.java:135)
23:03:28,636 ERROR [stderr] (default task-3)    at io.undertow.servlet.core.ServletRequestContextThreadSetupAction$1.call(ServletRequestContextThreadSetupAction.java:48)
23:03:28,636 ERROR [stderr] (default task-3)    at io.undertow.servlet.core.ContextClassLoaderSetupAction$1.call(ContextClassLoaderSetupAction.java:43)
23:03:28,637 ERROR [stderr] (default task-3)    at org.wildfly.extension.undertow.security.SecurityContextThreadSetupAction.lambda$create$0(SecurityContextThreadSetupAction.java:105)
23:03:28,637 ERROR [stderr] (default task-3)    at org.wildfly.extension.undertow.security.SecurityContextThreadSetupAction$$Lambda$887/252834873.call(Unknown Source)
23:03:28,638 ERROR [stderr] (default task-3)    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
23:03:28,639 ERROR [stderr] (default task-3)    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction$$Lambda$888/1261718761.call(Unknown Source)
23:03:28,639 ERROR [stderr] (default task-3)    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
23:03:28,639 ERROR [stderr] (default task-3)    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction$$Lambda$888/1261718761.call(Unknown Source)
23:03:28,640 ERROR [stderr] (default task-3)    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
23:03:28,640 ERROR [stderr] (default task-3)    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction$$Lambda$888/1261718761.call(Unknown Source)
23:03:28,640 ERROR [stderr] (default task-3)    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create$0(UndertowDeploymentInfoService.java:1508)
23:03:28,641 ERROR [stderr] (default task-3)    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction$$Lambda$888/1261718761.call(Unknown Source)
23:03:28,641 ERROR [stderr] (default task-3)    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
23:03:28,641 ERROR [stderr] (default task-3)    at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:81)
23:03:28,642 ERROR [stderr] (default task-3)    at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:104)
23:03:28,642 ERROR [stderr] (default task-3)    at io.undertow.server.Connectors.executeRootHandler(Connectors.java:326)
23:03:28,642 ERROR [stderr] (default task-3)    at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:812)
23:03:28,642 ERROR [stderr] (default task-3)    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
23:03:28,643 ERROR [stderr] (default task-3)    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
23:03:28,643 ERROR [stderr] (default task-3)    at java.lang.Thread.run(Thread.java:745)
23:03:28,644 ERROR [stderr] (default task-3) Caused by: java.lang.NullPointerException
23:03:28,644 ERROR [stderr] (default task-3)    at com.mysql.jdbc.ConnectionImpl.getServerCharset(ConnectionImpl.java:3005)
23:03:28,644 ERROR [stderr] (default task-3)    at com.mysql.jdbc.MysqlIO.sendConnectionAttributes(MysqlIO.java:1916)
23:03:28,644 ERROR [stderr] (default task-3)    at com.mysql.jdbc.MysqlIO.proceedHandshakeWithPluggableAuthentication(MysqlIO.java:1845)
23:03:28,645 ERROR [stderr] (default task-3)    at com.mysql.jdbc.MysqlIO.doHandshake(MysqlIO.java:1215)
23:03:28,645 ERROR [stderr] (default task-3)    at com.mysql.jdbc.ConnectionImpl.coreConnect(ConnectionImpl.java:2255)
23:03:28,645 ERROR [stderr] (default task-3)    at com.mysql.jdbc.ConnectionImpl.connectOneTryOnly(ConnectionImpl.java:2286)
23:03:28,645 ERROR [stderr] (default task-3)    ... 111 more

在错误消息的底部,我得到一个提示,它是由空指针异常引起的,但我就是无法弄清楚我的查询/代码中什么可以是空的?终端一切都很完美,只是JDBC不然。


答案 1

您的程序上有哪个版本的mysql-connector-java?我在PC上运行您的代码,没有问题。但是我将mysql-connector-java更改为版本5.1.26,我得到了同样的错误。您使用的是 mysql 8.0.11,因此您可以尝试使用 mysql-connector-java 8.0.11。

顺便说一句,在版本8中,您应该这样写URL

String URL= "jdbc:mysql://127.0.0.1:3306/db_example?useUnicode=true&characterEncoding=UTF-8&zeroDateTimeBehavior=CONVERT_TO_NULL&serverTimezone=GMT";

答案 2

我遇到了同样的问题,我所做的是我刚刚更新到以前的是.mysql-connector-java-8.0.11mysql-connector-java-5.1.38

改变

Class.forName("com.mysql.jdbc.Driver"); 

Class.forName("com.mysql.cj.jdbc.Driver");  

使用 mysql-8.0.17.0


推荐