Exception in thread “main“ org.springframework.jdbc.CannotGetJdbcConnectionException: Failed to obta

本文介绍了一种常见的Spring框架中使用JDBC进行数据库操作时出现的连接异常问题及其解决方案。通过修改数据库连接字符串,确保IP地址、端口号、数据库名称、用户名及密码正确无误。

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&amp;useUnicode=true&amp;characterEncoding=UTF-8&amp;serverTimezone=Asia/Shanghai&amp;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);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值