Spring数据库不能得到连接——Error querying database——CannotGetJdbcConnectionException—dbcp.SQLNestedException

今天sm整合的时候遇到了   Error querying database.

发现是报数据库连接不上的错,可以根据以下步骤查找问题。

1.原来只是data source的properties引入文件,el表达式前必须加jdbc(常出问题的地方

原因是:

Spring4.0.4 官方文档数据源配置:

<?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
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  
  <!-- Scans within the base package of the application for @Components to configure as beans -->
  <context:component-scan base-package="org.springframework.docs.test" />
  
  <bean id="dataSource" class="org.apache.commons.dbcp.BasicDataSource" destroy-method="close">
    <property name="driverClassName" value="${jdbc.driverClassName}"/>
    <property name="url" value="${jdbc.url}"/>
    <property name="username" value="${jdbc.username}"/>
    <property name="password" value="${jdbc.password}"/>
  </bean>

  <context:property-placeholder location="jdbc.properties"/>

</beans>

所以在你的db.properties或者applicationContext.xml中配置数据源,必须要加上jdbc. 形如jdbc.username。

然后确实无误后,仍保存,则

1.你的db.properties文件是否配置正确 useUnicode=true&amp;characterEncoding=UTF-8表示你设置的编码为utf-8保证与数据库的一致   防止乱码

2.检查你的spring/applicationContext-dao.xml是否配置了数据源   而且是否配置正确

3.你的连接名是否是正确的和你在db.properties中配置的是否一样

4.你电脑中的服务是否打开

5.检查下的你数据库名是否存在,或者表是否存在



原文解决参考: https://www.cnblogs.com/zhoumingming/p/5401144.html 

原文解决参考: https://www.cnblogs.com/panxuejun/p/5850599.html

 

 

 




 

猜你喜欢

转载自blog.csdn.net/qq_39213749/article/details/80036439