MySQLNonTransientConnectionException Could not cre

2019-05-15 22:42发布

问题:

I have a local MySQL Community server (8.0.11) running on a local host at 127.0.0.1:3306.

(I created a database and registered a user for it). I can use the following terminal query to access the database: ./mysql -u testuser -p

and the I am able to get access to the server.

I am using Spring and JDBC to do the same thing using the following code:

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();

    }
}

But then I get the following error:

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

Toward the bottom of the error message, I get a hint that it is caused by a Null Pointer Exception, but I just cannot figure out what can be null in my query/code? Everything works perfectly with terminal, just not with JDBC.

回答1:

Which version of mysql-connector-java is on your program? I run your code in my PC, and there is no problem.But I change mysql-connector-java to version 5.1.26, I got the same error. You are using mysql 8.0.11,so you can try to use mysql-connector-java 8.0.11.

By the way , the URL, you should write like this in version 8

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