在一个Spring项目中,新建了一个外部属性文件db.properties,在xml文件中利用${}来引用db.properties文件里面的属性。

beans-properties.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
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-4.0.xsd"> <!-- 导入属性文件 -->
<context:property-placeholder location="classpath:db.properties"/> <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<!-- 使用外部化属性文件的属性 -->
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="driverClass" value="${driverclass}"></property>
<property name="jdbcUrl" value="${jdbcurl}"></property>
</bean> </beans>

db.properties:

 user=root;
password=;
driverclass=com.mysql.jdbc.Driver;
jdbcurl=jdbc:mysql:///tt

Main.java:

 package com.tt.spring.beans.properties;

 import java.sql.SQLException;

 import javax.sql.DataSource;

 import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; public class Main { public static void main(String[] args) throws SQLException{ ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-properties.xml"); DataSource dataSource = (DataSource) ctx.getBean("dataSource"); System.out.println(dataSource.getConnection()); }
}

lib目录如下:

运行Main.java代码,控制台打印信息如下:

原因分析:

如果直接在beans-properties.xml文件里对user,password,driverClass,jdbcUrl进行赋值,而不引用db.properties文件里的属性,运行正常。这说明服务器能加载到数据库驱动。

所以问题出在什么地方呢?

Spring警告: Could not load driverClass com.mysql.jdbc.Driver(待解决)的更多相关文章

  1. Could not load driverClass com.mysql.jdbc.Driver错误

    在整合spring和mybatis的时候,在spring配置文件中已经加载了db.properties并配置了c3p0数据源 但在写了一个测试类测试是否取到了数据库的连接时,报错:Could not ...

  2. 搭建jfinal框架时报 Could not load driverClass com.mysql.jdbc.Driver

    搭建jfinal框架时报 Could not load driverClass com.mysql.jdbc.Driver 没有加载MySQL的驱动,你有没有把mysql的驱动包放到你项目的WEB-I ...

  3. ssm框架找不到mysql驱动类WARN DriverManagerDataSource:107 - Could not load driverClass com.mysql.jdbc.Driver

    找了很久错误,检查了配置文件,和spring配置数据源,都没有发现问题,最后上网查询了下,发现是由于配置文件后面有空格. 去除掉配置文件后面的空格就可以正常运行了.

  4. powerdesigner连接MySQL数据库时出现Non SQL Error : Could not load class com.mysql.jdbc.Driver

    Non SQL Error : Could not load class com.mysql.jdbc.Driver 这是因为powerdesigner 无法找到驱动所产生的 解决办法是:配置系统的c ...

  5. PowerDesigner 逆向工程Non SQL Error : Could not load class com.mysql.jdbc.Driver

    建立与数据库的连接. 在菜单条上,有一个Database的选择项: 选择connect…后弹出设置对话框: 在Data source里选择第三个单选按钮,即Connection profile:后,点 ...

  6. cloudera-scm-server启动出现Error creating bean with name 'entityManagerFactoryBean'与HHH010003: JDBC Driver class not found: com.mysql.jdbc.Driver错误解决办法(图文详解)

    不多说,直接上干货! 问题详情 -- ::, INFO main:com.cloudera.server.cmf.Main: Starting SCM Server. JVM Args: [-Dlog ...

  7. 错误:“Cannot load JDBC driver class 'com.mysql.jdbc.Driver”的解决方法

    “Cannot load JDBC driver class 'com.mysql.jdbc.Driver ” 表示没有JDBC连接MySql的驱动包,因此需要手动添加驱动包到WEB-INF目录下的l ...

  8. IntelliJ IDEA web项目进行数据库连接时出现java.lang.ClassNotFoundException: com.mysql.jdbc.Driver错误解决办法

    首先看报错信息: 意思是找不到类:  com.mysql.jdbc.Driver.也就是说tomcat找不到MySQL数据库连接要用的jar包! 出现这种错误的原因是: 项目中没有导入这个jar包, ...

  9. Exception 02 : java.lang.ClassNotFoundException: Could not load requested class : com.mysql.jdbc.Driver

    异常名称 java.lang.ClassNotFoundException: Could not load requested class : com.mysql.jdbc.Driver 异常详细信息 ...

随机推荐

  1. 【Python】错误、调试和测试

    链接到错误处理 链接到调试 链接到单元测试 链接到文档测试 [错误处理] 小结:遇到错误怎么办? 1.不理它. 2.捕获它,再抛出. 3.捕获并处理错误. raise 不懂如何处理错误就直接抛出(ra ...

  2. JS 中根据iframe子页面自动iframe高度

    注意:为使页面有更好的兼容性,在使用以下代码前,请将aspx页中头部的<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional ...

  3. Jquery中val、text、html的区别

    html就是你可以添加像<a></a>.<p></p>等标记text只能写文本如果写了上面的标记则会以文本形式输出val是属性,只有有该属性的对象才能调 ...

  4. tomcat结合httpd和nginx

    httpd结合tomcat: 前提:httpd版本2.4以上,编译安装 httpd:192.168.223.136 tomcat:192.168.223.146 tomcat简单创建一个额外的weba ...

  5. char,short,int长度

    数据类型的本质就是固定内存大小的别名 char:1byte short:  2byte int:4byte 其实变量也是对连续内存的别名,相当于这段内存的句柄.钩子

  6. kali rolling 配置网络

    一. 在文件系统里找到/etc/network下的interfaces文件,打开后可以看到eth0为dhcp,将其修改为如下形式: # This file describes the network ...

  7. Swift学习笔记 - OC中关于NSClassFromString获取不到Swift类的解决方案

    在OC和Swift混编的过程中发现在OC中通过NSClassFromString获取不到Swift中的类,调研了一下发现问题所在,下面是我的解决方案: 问题的发现过程 UIViewController ...

  8. linux中的&&和&,|和||

    在linux中,&和&&,|和||介绍如下: &  表示任务在后台执行,如要在后台运行redis-server,则有  python a.py & && ...

  9. js计算之递归

    看以下js代码: var w = [{ label: '你好', node: [ { label: '你好啊', node: [{ label: "还好", node: [] }, ...

  10. 20165332实验四 Android开发基础

    20165332 实验四 Android程序设计 一.实验报告封面 课程:Java程序设计 班级:1653班 姓名:延亿卓 学号:20165332 指导教师:娄嘉鹏 实验日期:2018年5月14日 实 ...