Java org.apache.commons.dbcp.SQLNestedException:无法加载 JDBC 驱动程序类“${driver}”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22869909/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class '${driver}'
提问by Praveen Verma
I am a beginner in spring. Here is my beans.xml
我是春天的初学者。这是我的beans.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:p="http://www.springframework.org/schema/p"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="DB.properties" />
</bean>
<bean id="myDataSource" class="org.apache.commons.dbcp.BasicDataSource" >
<property name="driverClassName" value="${driver}"/>
<property name="url" value="${url}"/>
<property name="username" value="${uname}"/>
<property name="password" value="${pwd}"/>
</bean>
</beans>
And DB.properties is given below:
DB.properties 如下所示:
#database connection propertiess
driver=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/Payment
uname=root
pwd=renu@193
I am getting this error given below:
我收到以下错误:
Apr 04, 2014 11:07:25 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [beans.xml]
org.apache.commons.dbcp.SQLNestedException: Cannot load JDBC driver class '${driver}'
at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1429)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1371)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:1044)
at com.student.spring.test.RunClass.main(RunClass.java:22)
Caused by: java.lang.ClassNotFoundException: ${driver}
at java.net.URLClassLoader.run(URLClassLoader.java:366)
at java.net.URLClassLoader.run(URLClassLoader.java:355)
at java.security.AccessController.doPrivileged(Native Method)
at java.net.URLClassLoader.findClass(URLClassLoader.java:354)
at java.lang.ClassLoader.loadClass(ClassLoader.java:425)
at sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:308)
at java.lang.ClassLoader.loadClass(ClassLoader.java:358)
at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1420)
... 3 more
I know what is this error. This error is about class BasicDataSource. This class is not found...... but i already included the required jars in build path in my project. Jars is as given below...... a)/home/praveen/Downloads/commons-dbcp-1.4.jar b)/home/praveen/Downloads/org.apache.commons.pool.jar
我知道这是什么错误。此错误与 BasicDataSource 类有关。找不到这个类......但我已经在我的项目的构建路径中包含了所需的 jars。罐子如下...... a)/home/praveen/Downloads/commons-dbcp-1.4.jar b)/home/praveen/Downloads/org.apache.commons.pool.jar
I have read some where that may be the version of jars causing the problem... and interfering with each other.....
我已经阅读了一些可能是导致问题的 jar 版本......并且相互干扰......
The jars are already available in project build path.... Please help me to solve this problem. Any help would be appreciated by me....
jars 已经在项目构建路径中可用.... 请帮我解决这个问题。我将不胜感激任何帮助....
The RunClass.java is given below:
RunClass.java 如下所示:
package com.student.spring.test;
import java.sql.Connection;
import java.sql.SQLException;
import org.apache.commons.dbcp.BasicDataSource;
import org.springframework.beans.factory.BeanFactory;
import org.springframework.beans.factory.xml.XmlBeanFactory;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
@SuppressWarnings("deprecation")
public class RunClass {
public static void main(String[] args) {
Resource resource = new ClassPathResource("beans.xml");
BeanFactory factory = new XmlBeanFactory(resource);
BasicDataSource bds = (BasicDataSource) factory.getBean("myDataSource");
Connection connection;
try {
connection = bds.getConnection();
System.out.println(bds.getDriverClassName());
System.out.println(bds.getUrl());
System.out.println(bds.getUsername());
System.out.println(bds.getPassword());
connection.close();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
采纳答案by Ramesh Kotha
Check your jars once, is spring-expression.jar there?
检查一下你的罐子,有 spring-expression.jar 吗?
And create applicationContext like below and check
并创建如下所示的 applicationContext 并检查
ApplicationContext context = new ClassPathXmlApplicationContext("beans.xml");
BasicDataSource bds = (BasicDataSource) context.getBean("myDataSource");
回答by Sotirios Delimanolis
XmlBeanFactory
is a deprecated class since 3.1 and a BeanFactory
. A BeanFactory
is only responsible for generating beans. The task of processing (or rather registering the processes for processing) beans belongs to ApplicationContext
subclasses.
XmlBeanFactory
是自 3.1 以来已弃用的类和BeanFactory
. ABeanFactory
只负责生成bean。处理(或者更确切地说是注册处理进程)bean 的任务属于ApplicationContext
子类。
Therefore use ClassPathXmlApplicationContext
to produce your beans and retrieve them
因此用于ClassPathXmlApplicationContext
生产您的豆子并检索它们
ApplicationContext ctx = new ClassPathXmlApplicationContext("test.xml");
BasicDataSource bds = (BasicDataSource) ctx.getBean("myDataSource");
回答by Prasad
If you are not using maven, make sure if your DB.properties is in location as below:
如果您不使用 maven,请确保您的 DB.properties 位于如下位置:
src-|-package1
|-package2
.
.
.
|-packageN
|-beans.xml
|-DB.properties
else specify your full classpath for file location as:
否则将文件位置的完整类路径指定为:
<bean id="propertyPlaceholderConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="location" value="classpath:packageName.jdbc1.properties" />
</bean>
and then change your code as pointed by @Sotirios and @Ramesh:
然后按照@Sotirios 和@Ramesh 的指示更改您的代码:
ApplicationContext ctx = new ClassPathXmlApplicationContext("beans.xml");
BasicDataSource bds = (BasicDataSource) ctx.getBean("myDataSource");