java 如何解决 org.apache.commons.dbcp.SQLNestedException
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/5073955/
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
how to resolve org.apache.commons.dbcp.SQLNestedException
提问by Chitresh
I am using hsqldb standalone as my database. i have a hsqldb.jar(hsqldb-2.0.0)
which i added on my project build path so my project will find out where is my hsqldb.jar
. i am using spring with these. my spring bean is:
我使用独立的 hsqldb 作为我的数据库。我hsqldb.jar(hsqldb-2.0.0)
在我的项目构建路径中添加了一个,这样我的项目就会找出我的hsqldb.jar
. 我正在使用这些弹簧。我的春豆是:
<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">
<bean id="sqlMapClient"
class="org.springframework.orm.ibatis.SqlMapClientFactoryBean">
<property name="dataSource">
<ref local="adapterDataSource" />
</property>
<property name="configLocation"
value="classpath:/com/hsqldb/example/dao/ibatis/SqlMapConfig.xml" />
</bean>
<bean id="adapterDataSource"
class="org.apache.commons.dbcp.BasicDataSource"
destroy-method="close">
<property name="driverClassName" value="org.hsqldb.jdbcDriver" />
<property name="url" value="data/db/hsqldb.jar" />
<property name="username" value="SA" />
<property name="password" value="password" />
</bean>
<bean id="testingDao" class="com.hsqldb.example.dao.TestingDao"
init-method="setTestDao" depends-on="moodleAuthenticationDetails">
<property name="sqlMapClient">
<ref local="sqlMapClient" />
</property>
</bean>
<bean id="moodleAuthenticationDetails"
class="com.hsqldb.example.HsqlDBAuthentication"></bean>
</beans>
and i have a method which will return me a data source which is as below :
我有一个方法可以返回一个数据源,如下所示:
public static DataSource getDataSource(String filePath){
String url;
//url="jdbc:hsqldb:file:"+filePath;
url = "jdbc:hsqldb:file:D:/EclipseWorskpace/ew-pg/lmexadapter/hsqldb-example/src/main/webapp/WEB-INF/data/db/hsqldb.jar";
BasicDataSource basicDataSource = new BasicDataSource();
basicDataSource.setUsername("SA");
basicDataSource.setPassword("password");
basicDataSource.setDriverClassName("org.hsqldb.jdbc.JDBCDriver");
// basicDataSource.setUrl("jdbc:mysql://IP/moodle");
basicDataSource.setUrl(url);
System.out.println("$$$$$$$$ URL is : " + url);
return basicDataSource;
}
but while running my test case by junit test it's giving me an exception :
但是在通过 junit test 运行我的测试用例时,它给了我一个例外:
org.apache.commons.dbcp.SQLNestedException: Cannot create JDBC driver of class 'org.hsqldb.jdbcDriver' for connect URL 'data/db/hsqldb.jar'
at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1273)
at org.apache.commons.dbcp.BasicDataSource.createDataSource(BasicDataSource.java:1192)
at org.apache.commons.dbcp.BasicDataSource.getConnection(BasicDataSource.java:884)
at org.springframework.jdbc.datasource.DataSourceUtils.doGetConnection(DataSourceUtils.java:113)
at org.springframework.jdbc.datasource.TransactionAwareDataSourceProxy$TransactionAwareInvocationHandler.invoke(TransactionAwareDataSourceProxy.java:213)
... 35 more
Caused by: java.sql.SQLException: No suitable driver
at java.sql.DriverManager.getDriver(DriverManager.java:264)
at org.apache.commons.dbcp.BasicDataSource.createConnectionFactory(BasicDataSource.java:1266)
... 39 more
please help to resolve this.
请帮助解决这个问题。
Thank you
谢谢
采纳答案by dfichter
The BasicDataSource
url
property in your Spring config should not be the path to your jar. See some examples of working Spring configs here.
BasicDataSource
url
Spring 配置中的属性不应是 jar 的路径。在此处查看一些工作 Spring 配置的示例。
(The DataSource
class you've written won't do anything unless you make Spring aware of it. But you don't have to write such a class -- just fix the url
property in your config.)
(DataSource
您编写的类不会做任何事情,除非您让 Spring 意识到它。但是您不必编写这样的类——只需url
在您的配置中修复该属性即可。)