IDEA连接数据库时报错org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection

2年前 (2022) 程序员胖胖胖虎阿
174 0 0

IDEA连接数据库的记录
各种看不懂的错

org.springframework.test.context.support.DefaultTestContextBootstrapper getTestExecutionListeners
信息: Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@1a968a59, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@4667ae56, org.springframework.test.context.support.DirtiesContextTestExecutionListener@77cd7a0]
org.springframework.test.context.support.DefaultTestContextBootstrapper getTestExecutionListeners
信息: Using TestExecutionListeners: [org.springframework.test.context.support.DirtiesContextBeforeModesTestExecutionListener@4667ae56, org.springframework.test.context.support.DependencyInjectionTestExecutionListener@77cd7a0, org.springframework.test.context.support.DirtiesContextTestExecutionListener@204f30ec]
Failed to obtain JDBC Connection
org/springframework/util/function/SingletonSupplier

这里是小孙同学看着崔岩老师在canvas上连数据库的视频,根据自己电脑上下载的MySQL写的报错笔记
我在测试能不能连上数据库时,用了去年罗菁老师给的db模板代码。

报错信息

org.springframework.jdbc.CannotGetJdbcConnectionException: Could not get JDBC Connection;
太多了,头一行就这个主要信息

罗菁老师的db.java文件

import java.sql.Connection;
import java.sql.DriverManager;
public class db {

private String bdUrl="jdbc:mysql://localhost:3306/db_book";//数据库连接地址
private String dbUserName="root";//用户名
private String dbpassword="123456";//密码
private String jdbcName="com.mysql.jdbc.Driver";//驱动名称
 
/**
 * 数据库连接
 */
Connection getCon()throws Exception{
	Class.forName(jdbcName);
	Connection con= DriverManager.getConnection(bdUrl, dbUserName, dbpassword);
	return con;    	
}
/**
 * 关闭数据库连接
 */
void closeCon(Connection con)throws Exception{
	if(con!=null){
		con.close();
	}
}
 public static void main(String[] args) {
		db dbutil=new db();
		try {
			dbutil.getCon();
			System.out.println("数据库连接成功!");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			System.out.println("数据库连接失败!");
		}
	}

}

mysql.properties文件

url那一行,?问好往后的,老师视频里没有加,但是我试了试,好像加上在能连上数据库,你们可以试试
1.老师的版本:

driverClassName = com.mysql.jdbc.Driver
mysql.url = jdbc:mysql://localhost:3306/javaee
mysql.userName = root
mysql.password = 123456

2.我的版本

driverClass = com.mysql.jdbc.Driver
mysql.url = jdbc:mysql://localhost:3306/javaee?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=UTC&rewriteBatchedStatements=true
mysql.userName = root
mysql.password = 123456

resources下的xml文件

SQL部分


        <!-- 配置集合对象-->
        <bean id="list" class="java.util.ArrayList" scope="prototype"/>

        <!--加载数据库访问配置-->
        <context:property-placeholder location="mysql.properties"/>

        <!--配置数据资源-->
        <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource"
              destroy-method="close"
              p:driverClassName="${driverClass}"<!--老师这里那个{}里边是driverClassName,我改成了driverClass,把那个Name删了,这里影响不大-->
              p:url="${mysql.url}"
              p:username="${mysql.userName}"
              p:password="${mysql.password}"
        />

        <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate"
              p:dataSource-ref="dataSource"/>

pom依赖

我这里添加了三个SQL的依赖我最开始用的jdbc的依赖的版本是8.多,

<!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>8.0.21</version>
        </dependency>

太新了,改成5.1.14后运行成功

<!--commons-dbcp and  mysql  spring-jdbc是为数据库添加的依赖-->
        <!-- https://mvnrepository.com/artifact/commons-dbcp/commons-dbcp -->
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.4</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/mysql/mysql-connector-java -->
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.14</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework/spring-jdbc -->
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>5.3.3</version>
        </dependency>

我的仓库是

https://gitee.com/hello-nohair-world/java-ee_-learing2022.git

下边这个版本是我可运行的完整代码地址

https://gitee.com/hello-nohair-world/java-ee_-learing2022/tree/d5b0d1f5fd3a1bf616bd594639513b9b7992b75a

手残党建仓库打错了一个字母应该没人注意到

相关文章

暂无评论

暂无评论...