使用Spring Boot和ibm JDBC驱动程序

时间:2017-04-03 15:24:03

标签: java spring maven jdbc spring-boot

我正在尝试设置一个spring-boot应用程序来连接IBM i机器上的DB2数据库。我已经向IBM jdbc驱动程序com.ibm.as400.access.AS400JDBCDriver添加了maven依赖项,并将spring.datasource.driver-class-name="com.ibm.as400.access.AS400JDBCDriver"添加到a​​pplication.properties文件中。

运行应用程序时,出现错误Cannot load driver class: "com.ibm.as400.access.AS400JDBCDriver。错误发生在org.springframework.boot.autoconfigure.jdbc中的spring-boot的DataSourceProperties类中的ClassUtils.forName行下面

private boolean driverClassIsLoadable() {
    try {
        ClassUtils.forName(this.driverClassName, null); <----- Exception happens here.  
        return true;
    }
    catch (UnsupportedClassVersionError ex) {
        // Driver library has been compiled with a later JDK, propagate error
        throw ex;
    }
    catch (Throwable ex) {
        return false;
    }
}

this.driverClassName等于“com.ibm.as400.access.AS400JDBCDriver”。有趣的是,在我的main方法中,我可以使用相同的driverClassName调用相同的函数,并且没有异常。

@SpringBootApplication
public class MinimumExampleApplication {

public static void main(String[] args) throws ClassNotFoundException, LinkageError {
    ClassUtils.forName("com.ibm.as400.access.AS400JDBCDriver", null); <---- No exception here?
    SpringApplication.run(MinimumExampleApplication.class, args);
  }
}

我的问题是,如果我自己加载驱动程序的话,为什么spring-boot无法找到驱动程序?

完整计划

MinimumExample.java

package org.mystuff.annual.meeting.signup;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.util.ClassUtils;

@SpringBootApplication
public class MinimumExampleApplication {

  public static void main(String[] args) throws ClassNotFoundException,    LinkageError {
    ClassUtils.forName("com.ibm.as400.access.AS400JDBCDriver", null);
    SpringApplication.run(MinimumExampleApplication.class, args);
  }
}

application.properties

spring.datasource.driver-class-name="com.ibm.as400.access.AS400JDBCDriver"
spring.datasource.url="jdbc:db2://myserver/myDb"
spring.datasource.dbcp2.initial-size=2
spring.datasource.dbcp2.max-total=5

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>org.mystuff</groupId>
    <artifactId>annual-meeting-signup</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>minimum-example</name>
    <description>Playing with data repository</description>

    <parent>
        <groupId>org.mystuff</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.5.2.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.ibm.as400</groupId>
            <artifactId>jt400</artifactId>
            <version>8</version>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>


</project>

1 个答案:

答案 0 :(得分:1)

正如@Andy Wilkinson所说,问题是由我的application.properties文件中的引号引起的。我把它配置得像这样

spring.datasource.driver-class-name="com.ibm.as400.access.AS400JDBCDriver"
spring.datasource.url="jdbc:db2://myserver/myDb"
spring.datasource.dbcp2.initial-size=2
spring.datasource.dbcp2.max-total=5

如果我改为下面的话就行了

spring.datasource.driver-class-name=com.ibm.as400.access.AS400JDBCDriver
spring.datasource.url=jdbc:db2://myserver/myDb
spring.datasource.dbcp2.initial-size=2
spring.datasource.dbcp2.max-total=5