zoukankan      html  css  js  c++  java
  • Spring警告: Could not load driverClass com.mysql.jdbc.Driver(待解决)

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

    beans-properties.xml:

     1 <?xml version="1.0" encoding="UTF-8"?>
     2 <beans xmlns="http://www.springframework.org/schema/beans"
     3     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
     4     xmlns:context="http://www.springframework.org/schema/context"
     5     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     6                         http://www.springframework.org/schema/beans/spring-beans.xsd
     7                         http://www.springframework.org/schema/context 
     8                         http://www.springframework.org/schema/context/spring-context-4.0.xsd">
     9     
    10     <!-- 导入属性文件 -->
    11     <context:property-placeholder location="classpath:db.properties"/>
    12    
    13     <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
    14           <!-- 使用外部化属性文件的属性 -->
    15           <property name="user" value="${user}"></property>        
    16           <property name="password" value="${password}"></property>
    17           <property name="driverClass" value="${driverclass}"></property>
    18           <property name="jdbcUrl" value="${jdbcurl}"></property>
    19     </bean>
    20 
    21 </beans>

    db.properties:

    1 user=root;
    2 password=1230;
    3 driverclass=com.mysql.jdbc.Driver;
    4 jdbcurl=jdbc:mysql:///tt

    Main.java:

     1 package com.tt.spring.beans.properties;
     2 
     3 import java.sql.SQLException;
     4 
     5 import javax.sql.DataSource;
     6 
     7 import org.springframework.context.ApplicationContext;
     8 import org.springframework.context.support.ClassPathXmlApplicationContext;
     9 
    10 
    11 public class Main {
    12 
    13     public static void main(String[] args) throws SQLException{ 
    14         
    15         ApplicationContext ctx = new ClassPathXmlApplicationContext("beans-properties.xml");
    16         
    17         DataSource dataSource = (DataSource) ctx.getBean("dataSource");
    18         
    19         System.out.println(dataSource.getConnection());
    20     
    21     }
    22 }

    lib目录如下:

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

    原因分析:

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

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

  • 相关阅读:
    each和foreach的区别
    apply和call的用法
    js小知识点
    关于react的一些疑问点
    c语言字符动画的实现
    解决'chromedriver' executable needs to be in PATH问题!
    二叉树的创建和遍历
    dns和dhcp
    编写一个application程序实现如下功能:接受命令行中给出的一个字符串,先将字符串原样输出,然后判断该穿的第一个字母是否为大写,若是大写则统计该串中大写字母的个数,并将所有大写字母输出。
    学生成绩管理系统究极版
  • 原文地址:https://www.cnblogs.com/TTTTT/p/6402145.html
Copyright © 2011-2022 走看看