Exception in thread “main”
org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to
obtain JDBC Connection; nested exception is
com.mysql.cj.jdbc.exceptions.CommunicationsException: Communications
link failure
运行时总是报错,看起来像是数据库连接的问题。检查数据库配置文件时,看到数据库连接字符串是我复制过来的,没有修改,修改后就好了。
数据库连接有5个位置需要特别注意:
IP:端口号?数据库名
数据库用户名
数据库密码
applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd">
<!-- 默认 <context:annotation-config/>-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.cj.jdbc.Driver"/>
<property name="url"
value="jdbc:mysql://localhost:3306/imooc?useSSL=false&useUnicode=true&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&allowPublicKeyRetrieval=true"/>
<property name="username" value="springJDBCtest"/>
<property name="password" value="Edao6688"/>
</bean>
<bean id="jdbdTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"/>
</bean>
<bean id="employeeDao" class="com.thdao.spring.jdbc.dao.EmployeeDao">
<property name="jdbcTemplate" ref="jdbdTemplate"></property>
</bean>
</beans>
Employee类:
package com.thdao.spring.jdbc.entity;
import java.util.Date;
public class Employee {
private Integer emo;
private String ename;
private Float salary;
@Override
public String toString() {
return "Employee{" +
"emo=" + emo +
", ename='" + ename + '\'' +
", salary=" + salary +
", dname='" + dname + '\'' +
", hiredate=" + hiredate +
'}';
}
public Integer getEmo() {
return emo;
}
public void setEmo(Integer emo) {
this.emo = emo;
}
public String getEname() {
return ename;
}
public void setEname(String ename) {
this.ename = ename;
}
public Float getSalary() {
return salary;
}
public void setSalary(Float salary) {
this.salary = salary;
}
public String getDname() {
return dname;
}
public void setDname(String dname) {
this.dname = dname;
}
public Date getHiredate() {
return hiredate;
}
public void setHiredate(Date hiredate) {
this.hiredate = hiredate;
}
private String dname;
private Date hiredate;
}
EmployeeDao类:
package com.thdao.spring.jdbc.dao;
import com.thdao.spring.jdbc.entity.Employee;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
public class EmployeeDao {
private JdbcTemplate jdbcTemplate;
public Employee findById(Integer eno){
String sql="select * from employee where eno= ? ";
Employee employee = jdbcTemplate.queryForObject(sql, new Object[]{eno}, new BeanPropertyRowMapper<Employee>(Employee.class));
return employee;
}
public JdbcTemplate getJdbcTemplate() {
return jdbcTemplate;
}
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
}
SpringApplication类:
package com.thdao.spring.jdbc;
import com.thdao.spring.jdbc.dao.EmployeeDao;
import com.thdao.spring.jdbc.entity.Employee;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class SpringApplication {
public static void main(String[] args) {
ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml");
EmployeeDao employeeDao = context.getBean("employeeDao", EmployeeDao.class);
Employee employee = employeeDao.findById(3308);
System.out.println(employee);
}
}
本文介绍了一种常见的Spring框架中使用JDBC进行数据库操作时出现的连接异常问题及其解决方案。通过修改数据库连接字符串,确保IP地址、端口号、数据库名称、用户名及密码正确无误。
1020

被折叠的 条评论
为什么被折叠?



