线程“main”org.springframework.jdbc.cannotgetjdbcconnectionexception中的异常

iszxjhcz  于 2021-07-09  发布在  Java
关注(0)|答案(2)|浏览(418)

请帮我纠正这个错误,我是新来的。所以当我试图运行没有springmvcweb结构的测试数据源时,我不能理解这个错误

cd F:\NetBeansProjects\SpringContactApp; "JAVA_HOME=C:\\Program Files\\Java\\jdk1.8.0_131" cmd /c "\"\"C:\\Program Files\\NetBeans 8.0.1\\java\\maven\\bin\\mvn.bat\" -Dexec.args=\"-classpath %classpath in.ezeon.capp.test.TestDataSource\" -Dexec.executable=java -Dexec.classpathScope=test -Dmaven.ext.class.path=\"C:\\Program Files\\NetBeans 8.0.1\\java\\maven-nblib\\netbeans-eventspy.jar\" -Dfile.encoding=UTF-8 org.codehaus.mojo:exec-maven-plugin:1.2.1:exec\""
        Running NetBeans Compile On Save execution. Phase execution is skipped and output directories of dependency projects (with Compile on Save turned on) will be used instead of their jar artifacts.
        Scanning for projects...

        ------------------------------------------------------------------------
        Building SpringContactApp 1.0-SNAPSHOT
        ------------------------------------------------------------------------

        --- exec-maven-plugin:1.2.1:exec (default-cli) @ SpringContactApp ---
        Feb 04, 2019 12:15:40 PM org.springframework.context.annotation.AnnotationConfigApplicationContext prepareRefresh
        INFO: Refreshing org.springframework.context.annotation.AnnotationConfigApplicationContext@531d72ca: startup date [Mon Feb 04 12:15:40 IST 2019]; root of context hierarchy
        Feb 04, 2019 12:15:40 PM org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor <init>
        INFO: JSR-330 'javax.inject.Inject' annotation found and supported for autowiring
        Exception in thread "main" org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection; nested exception is java.sql.SQLException: Cannot create PoolableConnectionFactory (Could not create connection to database server. Attempted reconnect 3 times. Giving up.)
            at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:80)
            at org.springframework.jdbc.core.JdbcTemplate.execute(JdbcTemplate.java:619)
            at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:870)
            at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:931)
            at org.springframework.jdbc.core.JdbcTemplate.update(JdbcTemplate.java:941)
            at in.ezeon.capp.test.TestDataSource.main(TestDataSource.java:23)
        Caused by: java.sql.SQLException: Cannot create PoolableConnectionFactory (Could not create connection to database server. Attempted reconnect 3 times. Giving up.)
            at org.apache.commons.dbcp2.BasicDataSource.createPoolableConnectionFactory(BasicDataSource.java:2294)
            at org.apache.commons.dbcp2.BasicDataSource.createDataSource(BasicDataSource.java:2039)
            at org.apache.commons.dbcp2.BasicDataSource.getConnection(BasicDataSource.java:1533)
            at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:111)
            at org.springframework.jdbc.datasource.DataSourceUtils.getConnection(DataSourceUtils.java:77)
            ... 5 more
        Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException: Could not create connection to database server. Attempted reconnect 3 times. Giving up.

下面的代码是springrootconfig.java类和testdatasource.java类,我使用的是netbeans8和spring框架。

package in.ezeon.capp.config;

import static java.lang.Boolean.TRUE;
import org.apache.commons.dbcp2.BasicDataSource;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;

/**
 *
 * @author admin
 */
@Configuration
@ComponentScan(basePackages = {"in.ezeon.capp.dao"})
public class SpringRootConfig {
    //TODO: Services,DAO,DataSource,Email sender or some other business layer beans
    @Bean
    public BasicDataSource getDataSource()
    {
        BasicDataSource ds=new BasicDataSource();
        ds.setDriverClassName("com.mysql.jdbc.Driver");
        //ds.setUrl("jdbc:mysql://localhost:3306/capp_db?useUnicode=true&characterEncoding=UTF-8&useSSL=false&autoReconnect=true&failOverReadOnly=false");//localhost//127.0.0.1
        ds.setUrl("jdbc:mysql://localhost:3306/capp_db");
        ds.setUsername("root");
        ds.setPassword("dbadmin@123");//dbadmin@123
        ds.setMaxTotal(2);
        ds.setInitialSize(1);
        ds.setTestOnBorrow(true);
        ds.setValidationQuery("SELECT 1");
        ds.setDefaultAutoCommit(TRUE);
        return ds;
    }
}

并找到下面这个错误的截图。
在此处输入图像描述

0tdrvxhp

0tdrvxhp1#

就你而言:

Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLNonTransientConnectionException:
 Could not create connection to database server. Attempted reconnect 3 times. Giving up.

检查您的jdbc url。
检查数据库的可用性(在该url上,服务器运行?、端口?、数据库?理想情况下,相同的驱动程序,相同的网络..例如netbeans“服务”选项卡>“数据库”)
检查网络/代理/防火墙,你在。

6yt4nkrj

6yt4nkrj2#

错误与数据库连接有关。检查你现在使用的数据。可能没有创建与您连接的数据库同名的数据库。

相关问题