简体   繁体   English

找不到合适的驱动程序的原因

[英]Cause of No suitable driver found for

I'm trying to unit test (JUnit) a DAO i've created. 我正在尝试单元测试(JUnit)我创建的DAO。 I'm using Spring as my framework, my DAO (JdbcPackageDAO) extends SimpleJdbcDaoSupport. 我使用Spring作为我的框架,我的DAO(JdbcPackageDAO)扩展了SimpleJdbcDaoSupport。 The testing class (JdbcPackageDAOTest) extends AbstractTransactionalDataSourceSpringContextTests. 测试类(JdbcPackageDAOTest)扩展了AbstractTransactionalDataSourceSpringContextTests。 I've overridden the configLocations as follows: 我已经覆盖了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. 我使用HSQL作为我的后端,它以独立模式运行。 My IDE of choice is eclipse. 我选择的IDE是eclipse。 When I run the class as a JUnit test here's my error (below). 当我作为JUnit测试运行类时,这是我的错误(下面)。 I have no clue as to why its happening. 我不知道为什么会发生这种情况。 hsql.jar is on my build path according to Eclipse. 根据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$1.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

In order to have HSQLDB register itself, you need to access its jdbcDriver class. 为了让HSQLDB自身注册,您需要访问其jdbcDriver类。 You can do this the same way as in this example . 您可以使用与此示例相同的方式执行此操作。

Class.forName("org.hsqldb.jdbcDriver");

It triggers static initialization of jdbcDriver class, which is: 它触发jdbcDriver类的静态初始化,即:

static {
    try {
        DriverManager.registerDriver(new jdbcDriver());
    } catch (Exception e) {}
}

“没有合适的驱动程序”通常意味着连接URL的语法不正确。

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. 这是我发现的解决方案。

  1. Move jars from .../web-inf/lib to PROJECT_ROOT/lib 将jar从... / web-inf / lib移动到PROJECT_ROOT / lib
  2. Alter build path in eclipse to reflect this change. 在eclipse中更改构建路径以反映此更改。
  3. cleaned and rebuilt my project. 清理并重建我的项目。
  4. ran the junit test and BOOM it worked! 跑了junit测试和BOOM它工作!

My guess is that it had something to do with how Ganymede reads jars in the /web-inf/lib folder. 我的猜测是它与Ganymede如何在/ web-inf / lib文件夹中读取jar有关。 But who knows... It works now. 但谁知道......它现在有效。

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之后缺少一个别名:

http://hsqldb.org/doc/guide/ch04.html http://hsqldb.org/doc/guide/ch04.html

看起来你没有指定要连接的数据库名称,应该是这样的

jdbc:hsqldb:hsql://serverName:port/DBname

great I had the similar problem. 我有类似的问题。 The advice for all is to check jdbc url sintax 对所有人的建议是检查jdbc url sintax

As some answered before, this line of code solved the problem 正如之前的一些回答,这行代码解决了这个问题

Class.forName("org.hsqldb.jdbcDriver");

But my app is running in some tomcats but only in one installation I had to add this code. 但我的应用程序运行在一些tomcats但只在一个安装我必须添加此代码。

Can you import the driver (org.hsqldb.jdbcDriver) into one of your source files? 你可以将驱动程序(org.hsqldb.jdbcDriver)导入到你的一个源文件中吗? (To test that the class is actually on your class path). (测试该类实际上是在您的类路径上)。

If you can't import it then you could try including hsqldb.jar in your build path. 如果您无法导入它,那么您可以尝试在构建路径中包含hsqldb.jar

I had the same problem with spring, commons-dbcp and oracle 10g. 我遇到了与spring,commons-dbcp和oracle 10g相同的问题。 Using this URL I got the 'no suitable driver' error: jdbc:oracle:thin@192.168.170.117:1521:kinangop 使用此URL我得到'不合适的驱动程序'错误: jdbc:oracle:thin@192.168.170.117:1521:kinangop

The above URL is missing a full colon just before the @. 上面的URL在@之前缺少一个完整的冒号。 After correcting that, the error disappeared. 纠正后,错误消失了。

when try to run datasource connectivity using static main method, first we need to run database connection. 当尝试使用静态main方法运行数据源连接时,首先我们需要运行数据库连接。 This we can achieve in eclipse as bellow. 这个我们可以在eclipse中实现如下。

1) open any IDE(Eclipse or RAD) after opening workspace by default IDE will be opened in JAVA prospective. 1)打开工作区后打开任何IDE(Eclipse或RAD)默认情况下IDE将在JAVA中打开。 Try to switch from java to database prospective in order to create datasource as well as virtual database connectivity. 尝试从java切换到数据库,以创建数据源和虚拟数据库连接。

2)in database prospective enter all the details like userName, Password and URL of the particular schema. 2)在数据库预期中输入特定模式的userName,Password和URL等所有细节。

3)then try to run main method to access database. 3)然后尝试运行main方法来访问数据库。

This will resolve the "serverName undefined". 这将解析“serverName undefined”。

It might be that 可能就是这样

hsql://localhost HSQL://本地主机

can't be resolved to a file. 无法解析为文件。 Look at the sample program here: 看看这里的示例程序:

Sample HSQLDB program 示例HSQLDB程序

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! 祝好运!

I think your HSQL URL is wrong. 我认为您的HSQL URL错误。 It should also include the database name, 它还应包括数据库名称,

so something like 所以像

jdbc:hsqldb:hsql://localhost/mydatabase 

if mydatabase is the name of your DB (file). 如果mydatabase是您的数据库(文件)的名称。 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) 不包括这个(我不确定在这里是否是这种情况)混淆了URL的解析,这可能导致DriverManagerDS认为你的驱动程序不适合(它被发现,但它认为它不是一个好的一)

I was facing similar problem and to my surprise the problem was in the version of Java. 我遇到了类似的问题,令我惊讶的是问题出现在Java版本中。 java.sql.DriverManager comes from rt.jar was unable to load my driver "COM.ibm.db2.jdbc.app.DB2Driver". java.sql.DriverManager来自rt.jar无法加载我的驱动程序“COM.ibm.db2.jdbc.app.DB2Driver”。

I upgraded from jdk 5 and jdk 6 and it worked. 我从jdk 5和jdk 6升级并且它起作用了。

在某些情况下,检查权限(所有权)。

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. 不确定它是否值得,但我有一个类似的问题,我得到一个“java.sql.SQLException:找不到合适的驱动程序”错误。 I found this thread while researching a solution. 我在研究解决方案时发现了这个问题。

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. 无论如何,只要把这个注释放在这里以防其他人因类似的问题而绊倒。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM