Java 找不到合适的驱动程序的原因
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/160611/
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
Cause of No suitable driver found for
提问by IaCoder
I'm trying to unit test (JUnit) a DAO i've created. I'm using Spring as my framework, my DAO (JdbcPackageDAO) extends SimpleJdbcDaoSupport. The testing class (JdbcPackageDAOTest) extends AbstractTransactionalDataSourceSpringContextTests. I've overridden the configLocations as follows:
我正在尝试对我创建的 DAO 进行单元测试 (JUnit)。我使用 Spring 作为我的框架,我的 DAO (JdbcPackageDAO) 扩展了 SimpleJdbcDaoSupport。测试类 (JdbcPackageDAOTest) 扩展了 AbstractTransactionalDataSourceSpringContextTests。我已经覆盖了 configLocations 如下:
protected String[] getConfigLocations(){
return new String[] {"classpath:company/dc/test-context.xml"};
}
My test-context.xml file is defined as follows:
我的 test-context.xml 文件定义如下:
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="dataPackageDao" class="company.data.dao.JdbcPackageDAO">
<property name="dataSource" ref="dataSource" />
</bean>
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="org.hsqldb.jdbcDriver"/>
<property name="url" value="jdbc:hsqldb:hsql://localhost"/>
<property name="username" value="sa" />
<property name="password" value="" />
</bean>
<bean id="propertyConfigurer"
class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
<property name="locations">
<list>
<value>company/data/dao/jdbc.properties</value>
</list>
</property>
</bean>
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource" />
</bean>
</beans>
I'm using HSQL as my backend, it's running in standalone mode. My IDE of choice is eclipse. When I run the class as a JUnit test here's my error (below). I have no clue as to why its happening. hsql.jar is on my build path according to Eclipse.
我使用 HSQL 作为我的后端,它以独立模式运行。我选择的 IDE 是 eclipse。当我将类作为 JUnit 测试运行时,这是我的错误(如下)。我不知道为什么会发生这种情况。根据 Eclipse,hsql.jar 在我的构建路径上。
org.springframework.transaction.CannotCreateTransactionException: Could not open JDBC Connection for transaction; nested exception is java.sql.SQLException: No suitable driver found for jdbc:hsqldb:hsql://localhost at org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(DataSourceTransactionManager.java:219) at org.springframework.transaction.support.AbstractPlatformTransactionManager.getTransaction(AbstractPlatformTransactionManager.java:377) at org.springframework.test.AbstractTransactionalSpringContextTests.startNewTransaction(AbstractTransactionalSpringContextTests.java:387) at org.springframework.test.AbstractTransactionalSpringContextTests.onSetUp(AbstractTransactionalSpringContextTests.java:217) at org.springframework.test.AbstractSingleSpringContextTests.setUp(AbstractSingleSpringContextTests.java:101) at junit.framework.TestCase.runBare(TestCase.java:128) at org.springframework.test.ConditionalTestCase.runBare(ConditionalTestCase.java:76) at junit.framework.TestResult.protect(TestResult.java:106) at junit.framework.TestResult.runProtected(TestResult.java:124) at junit.framework.TestResult.run(TestResult.java:109) at junit.framework.TestCase.run(TestCase.java:120) at junit.framework.TestSuite.runTest(TestSuite.java:230) at junit.framework.TestSuite.run(TestSuite.java:225) at org.eclipse.jdt.internal.junit.runner.junit3.JUnit3TestReference.run(JUnit3TestReference.java:130) at org.eclipse.jdt.internal.junit.runner.TestExecution.run(TestExecution.java:38) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:460) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.runTests(RemoteTestRunner.java:673) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.run(RemoteTestRunner.java:386) at org.eclipse.jdt.internal.junit.runner.RemoteTestRunner.main(RemoteTestRunner.java:196) Caused by: java.sql.SQLException: No suitable driver found for jdbc:hsqldb:hsql://localhost at java.sql.DriverManager.getConnection(Unknown Source) at java.sql.DriverManager.getConnection(Unknown Source) at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:291) at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:277) at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnectionFromDriverManager(DriverManagerDataSource.java:259) at org.springframework.jdbc.datasource.DriverManagerDataSource.getConnection(DriverManagerDataSource.java:241) at org.springframework.jdbc.datasource.DataSourceTransactionManager.doBegin(DataSourceTransactionManager.java:182) ... 18 more
回答by Max Stewart
It looks like you're not specifying a database name to connect to, should go something like
看起来您没有指定要连接的数据库名称,应该是这样的
jdbc:hsqldb:hsql://serverName:port/DBname
回答by Max Stewart
Can you import the driver (org.hsqldb.jdbcDriver) into one of your source files? (To test that the class is actually on your class path).
您可以将驱动程序 (org.hsqldb.jdbcDriver) 导入您的源文件之一吗?(为了测试该类实际上是否在您的类路径上)。
If you can't import it then you could try including hsqldb.jarin your build path.
如果您无法导入它,那么您可以尝试在构建路径中包含 hsqldb.jar。
回答by MetroidFan2002
It might be that
可能是这样
hsql://localhost
hsql://本地主机
can't be resolved to a file. Look at the sample program here:
无法解析为文件。在此处查看示例程序:
See if you can get that working first, and then see if you can take that configuration information and use it in the Spring bean configuration.
看看你是否可以先让它工作,然后看看你是否可以获取该配置信息并在 Spring bean 配置中使用它。
Good luck!
祝你好运!
回答by NR.
I think your HSQL URL is wrong. It should also include the database name,
我认为您的 HSQL URL 是错误的。它还应该包括数据库名称,
so something like
所以像
jdbc:hsqldb:hsql://localhost/mydatabase
if mydatabase is the name of your DB (file). Not including this can (I'm not sure if it is the case here) confuse the parsing of the URL, which may lead to the DriverManagerDS thinking that your driver is not suitable (it is found, but it thinks it is not a good one)
如果 mydatabase 是您的数据库(文件)的名称。不包括这个可以(我不确定这里是不是这种情况)混淆URL的解析,这可能会导致DriverManagerDS认为你的驱动不合适(找到了,但它认为它不是一个好的一)
回答by IaCoder
Okay so here's the solution. Most everyone made really good points but none solved the problem (THANKS for the help). Here is the solution I found to work.
好的,这是解决方案。大多数人都提出了很好的观点,但没有人解决问题(感谢帮助)。这是我发现有效的解决方案。
- Move jars from .../web-inf/lib to PROJECT_ROOT/lib
- Alter build path in eclipse to reflect this change.
- cleaned and rebuilt my project.
- ran the junit test and BOOM it worked!
- 将 jars 从 .../web-inf/lib 移动到 PROJECT_ROOT/lib
- 更改 eclipse 中的构建路径以反映此更改。
- 清理并重建了我的项目。
- 运行 junit 测试和 BOOM 它起作用了!
My guess is that it had something to do with how Ganymede reads jars in the /web-inf/lib folder. But who knows... It works now.
我的猜测是它与 Ganymede 如何读取 /web-inf/lib 文件夹中的 jar 文件有关。但谁知道...它现在起作用了。
回答by IaCoder
Not sure if it's worth anything, but I had a similar problem where I was getting a "java.sql.SQLException: No suitable driver found" error. I found this thread while researching a solution.
不确定它是否值得,但我遇到了类似的问题,我收到了“java.sql.SQLException:找不到合适的驱动程序”错误。我在研究解决方案时发现了这个线程。
The way I ended up solving my problem was to forgo using java.sql.DriverManager to get a connection and instead built up an instance of org.hsqldb.jdbc.jdbcDataSource and used that.
我最终解决我的问题的方法是放弃使用 java.sql.DriverManager 来获取连接,而是建立一个 org.hsqldb.jdbc.jdbcDataSource 的实例并使用它。
The root cause of my problem (I believe) had to do with the classloader hierarchy and the fact that the JRE was running Java 5. Even though I could successfully load the jdbcDriver class, the classloader behind java.sql.DriverManager was higher up, to the point that it couldn't see the hsqldb.jar I needed.
我的问题的根本原因(我相信)与类加载器层次结构以及 JRE 运行 Java 5 的事实有关。即使我可以成功加载 jdbcDriver 类,java.sql.DriverManager 背后的类加载器更高,以至于它看不到我需要的 hsqldb.jar。
Anyway, just putting this note here in case someone else stumbles by with a similar problem.
不管怎样,只要把这个笔记放在这里,以防其他人遇到类似的问题。
回答by duffymo
"no suitable driver" usually means that the syntax for the connection URL is incorrect.
“没有合适的驱动程序”通常意味着连接 URL 的语法不正确。
回答by duffymo
If you look at your original connection string:
如果您查看原始连接字符串:
<property name="url" value="jdbc:hsqldb:hsql://localhost"/>
The Hypersonic docs suggest that you're missing an alias after localhost:
Hypersonic 文档建议您在 localhost 之后缺少别名:
回答by duffymo
I had the same problem with spring, commons-dbcp and oracle 10g. Using this URL I got the 'no suitable driver' error: jdbc:oracle:[email protected]:1521:kinangop
我在 spring、commons-dbcp 和 oracle 10g 上遇到了同样的问题。使用这个 URL 我得到了“没有合适的驱动程序”错误:jdbc:oracle:[email protected]:1521:kinangop
The above URL is missing a full colon just before the @. After correcting that, the error disappeared.
上面的 URL 在 @ 之前缺少一个完整的冒号。更正后,错误消失了。
回答by Ivan Koblik
In order to have HSQLDB register itself, you need to access its jdbcDriver class. You can do this the same way as in this example.
为了让 HSQLDB 自己注册,你需要访问它的 jdbcDriver 类。您可以按照与本示例相同的方式执行此操作。
Class.forName("org.hsqldb.jdbcDriver");
It triggers static initialization of jdbcDriver class, which is:
它触发 jdbcDriver 类的静态初始化,即:
static {
try {
DriverManager.registerDriver(new jdbcDriver());
} catch (Exception e) {}
}