简体   繁体   English

无法获得 JDBC 连接; 嵌套异常是 java.sql.SQLException - junit eclipse

[英]Could not get JDBC Connection; nested exception is java.sql.SQLException - junit eclipse

When doing junit test for my spring project in eclipse Im getting a failure as,在 eclipse 中为我的 spring 项目做 junit 测试时,我失败了,

Could not get JDBC Connection;无法获得 JDBC 连接; nested exception is java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)嵌套异常是 java.sql.SQLException: Access denied for user 'root'@'localhost' (using password: YES)

I have already added the jar file to libraries and set the environment variables path .我已经将jar 文件添加到库中并设置了环境变量 path But still it gives the error.但它仍然给出了错误。

The tested function is as below.测试功能如下。

@Test
void testSave() {
    dataSource = new DriverManagerDataSource();
    dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
    dataSource.setUrl("jdbc:mysql://localhost:3306/contactdb");
    dataSource.setUsername("root");
    dataSource.setPassword("root");

    dao = new ContactDAOImpl(dataSource);

    Contact contact = new Contact("Steve Jobs", "steve@apple.com", "California, USA", "0325698745");
    int result = dao.save(contact);

    assertTrue(result > 0);
}

My pom.xml file is also attached.我的pom.xml文件也附上了。

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>net.codejava.contact</groupId>
  <artifactId>ContactManager</artifactId>
  <version>0.0.1-SNAPSHOT</version>
  <packaging>war</packaging>
  <build>
    <sourceDirectory>src</sourceDirectory>
    <plugins>
      <plugin>
        <artifactId>maven-compiler-plugin</artifactId>
        <version>3.8.0</version>
        <configuration>
          <release>13</release>
        </configuration>
      </plugin>
      <plugin>
        <artifactId>maven-war-plugin</artifactId>
        <version>3.2.3</version>
        <configuration>
          <warSourceDirectory>WebContent</warSourceDirectory>
        </configuration>
      </plugin>
    </plugins>
  </build>

  <properties>
    <spring.version>5.9.1.RELEASE</spring.version>
  </properties>

  <dependencies>
    <dependency>
        <groupId>org.springframework</groupId> 
        <artifactId>spring-webmvc</artifactId> 
        <version>4.2.2.RELEASE</version> 
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId> 
        <artifactId>spring-orm</artifactId> 
        <version>4.2.2.RELEASE</version> 
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId> 
        <artifactId>javax.servlet-api</artifactId> 
        <version>4.0.1</version> 
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId> 
        <artifactId>jstl</artifactId> 
        <version>1.2</version> 
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.19</version>
    </dependency>
    <dependency>
        <groupId>junit</groupId>
        <artifactId>junit</artifactId>
        <version>4.13.0</version>
    </dependency>
  </dependencies>
</project>

You do not need the following line to load the driver.您不需要以下行来加载驱动程序。

dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");

Check this for more information.检查这个以获取更多信息。

Apart from this, reset your database root password and try again.除此之外,请重置您的数据库root密码并重试。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法获得JDBC连接。 嵌套异常是java.sql.SQLException Mule ESB - Could not get JDBC Connection; nested exception is java.sql.SQLException Mule ESB 异常无法获取JDBC连接; 嵌套异常是java.sql.SQLException:没有为jdbc找到合适的驱动程序:oracle:thin:@localhost:1521:xe - Exception Could not get JDBC Connection; nested exception is java.sql.SQLException: No suitable driver found for jdbc:oracle:thin:@localhost:1521:xe 无法获得JDBC连接。 嵌套异常是java.sql.SQLException:无法加载JDBC驱动程序类&#39;org.hsql.jdbcDriver&#39; - Could not get JDBC Connection; nested exception is java.sql.SQLException: Cannot load JDBC driver class 'org.hsql.jdbcDriver' 无法获得JDBC连接。 嵌套异常为java.sql.SQLException:ORA-28040:Spring Batch中没有匹配的身份验证协议 - Could not get JDBC Connection; nested exception is java.sql.SQLException: ORA-28040: No matching authentication protocol in Spring Batch 获取JDBC连接失败; 嵌套异常是 java.sql.SQLException: null - Failed to obtain JDBC Connection; nested exception is java.sql.SQLException: null java.sql.SQLException:Io异常:在与oracle的JDBC连接期间从读取调用中减去了一个 - java.sql.SQLException: Io exception: Got minus one from a read call during JDBC connection with oracle org.springframework.jdbc.CannotGetJdbcConnectionException,嵌套异常是java.sql.SQLException - org.springframework.jdbc.CannotGetJdbcConnectionException, nested exception is java.sql.SQLException 例外:java.sql.SQLException:没有为jdbc找到合适的驱动程序:ucanaccess - Exception: java.sql.SQLException: No suitable driver found for jdbc:ucanaccess java.sql.SQLException: IO 异常:网络适配器无法建立连接? - java.sql.SQLException: IO Exception : The Network adapter could not establish the connection? java.sql.SQLException:Io异常:网络适配器无法建立连接 - java.sql.SQLException: Io exception: The Network Adapter could not establish the connection
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM