Springboot 与 Sybase 服务器 - 无法加载驱动程序类:com.sybase.jdbc4.jdbc.SybDriver

Posted

技术标签:

【中文标题】Springboot 与 Sybase 服务器 - 无法加载驱动程序类:com.sybase.jdbc4.jdbc.SybDriver【英文标题】:Springboot with Sybase server - Cannot load driver class: com.sybase.jdbc4.jdbc.SybDriver 【发布时间】:2018-04-04 19:16:02 【问题描述】:

在使用新的 Spring 引导应用程序配置 Sybase 服务器时遇到困难。不确定如何使用 spring boot 成功配置 Sybase 服务器。出现错误“无法加载驱动程序类:com.sybase.jdbc4.jdbc.SybDriver”。

基本上,我希望从 Sybase 数据库中获取一些数据并以 JSON 格式作为 RESTful 服务发布。任何帮助,将不胜感激。

pom.xml

<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>com.rbccm.poc</groupId>
    <artifactId>springbootpoc1</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>jar</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.0.0.RELEASE</version>
    </parent>

    <name>springbootpoc1</name>
    <url>http://maven.apache.org</url>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    </properties>

    <dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>com.sybase.jconnect</groupId>
            <artifactId>jconn4</artifactId>
            <version>7.07</version>
            <scope>provided</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>com.github.derjust</groupId>
            <artifactId>spring-data-dynamodb</artifactId>
            <version>5.0.2</version>
        </dependency>

    </dependencies>

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

application.properties

server.port=8090

spring.jpa.hibernate.ddl-auto=none
spring.jpa.hibernate.naming.physical-strategy=org.hibernate.boot.model.naming.PhysicalNamingStrategyStandardImpl
spring.jpa.databasePlatform=org.hibernate.dialect.SybaseDialect
spring.jpa.show-sql=true

# hikariCP
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.datasource.url=jdbc:sybase:Tds:server:port/dbname
spring.datasource.username=u_****
spring.datasource.password=****
spring.datasource.connectionTestQuery=SELECT 1
spring.datasource.poolName=SpringBootHikariCP
spring.datasource.maximumPoolSize=10
spring.datasource.connectionTimeout=60000
spring.datasource.driverClassName=com.sybase.jdbc4.jdbc.SybDriver

# Number of ms to wait before throwing an exception if no connection is available.
spring.datasource.tomcat.max-wait=10000
# Maximum number of active connections that can be allocated from this pool at the same time.
spring.datasource.tomcat.max-active=50
# Validate the connection before borrowing it from the pool.
spring.datasource.tomcat.test-on-borrow=true

#JDBC customize
spring.jdbc.template.max-rows=500

Maven 构建日志

2018-04-04 14:48:25.206 ERROR 21768 --- [           main] o.s.boot.SpringApplication               : Application run failed

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaConfiguration': Unsatisfied dependency expressed through constructor parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'dataSource' defined in class path resource [org/springframework/boot/autoconfigure/jdbc/DataSourceConfiguration$Hikari.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.zaxxer.hikari.HikariDataSource]: Factory method 'dataSource' threw exception; nested exception is java.lang.IllegalStateException: Cannot load driver class: com.sybase.jdbc4.jdbc.SybDriver
    at org.springframework.beans.factory.support.ConstructorResolver.createArgumentArray(ConstructorResolver.java:729) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]
    at org.springframework.beans.factory.support.ConstructorResolver.autowireConstructor(ConstructorResolver.java:192) ~[spring-beans-5.0.4.RELEASE.jar:5.0.4.RELEASE]

【问题讨论】:

为什么sybase驱动的作用域设置为provided 【参考方案1】:

您可以按照以下步骤将 spring boot jpa 与 sybase 一起使用:

1.-从the sap marketplace下载jconn4.jar

2.-在本地仓库中安装jar:

mvn install:install-file -Dfile="path-to\jconn4.jar" -DgroupId=sybase -DartifactId=jconn -Dversion=4_RELEASE -Dpackaging=jar

3.-将依赖项添加到您的 pom:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>sybase</groupId>
            <artifactId>jconn</artifactId>
            <version>4_RELEASE</version>
        </dependency>

4.-将数据库配置添加到你的application.properties:

spring.datasource.url=jdbc:sybase:Tds:HOST:PORT/DB_NAME
spring.datasource.username=USER
spring.datasource.password=PASS
spring.jpa.properties.hibernate.default_schema=SCHEMA

spring.datasource.driverClassName=com.sybase.jdbc4.jdbc.SybDriver
spring.datasource.type=com.zaxxer.hikari.HikariDataSource
spring.jpa.hibernate.ddl-auto=none
spring.jpa.databasePlatform=org.hibernate.dialect.SybaseDialect
spring.datasource.connectionTestQuery=select 1
spring.jpa.show-sql=true

【讨论】:

谢谢,这有帮助!同样对于拥有不同/较新版本 Sybase 的人,不要忘记在 pom.xml 和 application.properties 中进行更改。比如对于jconn2.jars,在application.properties中应该是spring.datasource.driverClassName=com.sybase.jdbc2.jdbc.SybDriverdocs.oracle.com/cd/E19509-01/820-3497/agqkg/index.html【参考方案2】:

已解决: 在 pom 依赖中,我使用了 jdbc,而不是 jpa。并且必须在外部的类路径中添加“jconn4d.jar”。

pom.xml

<dependencies>
        <dependency>
            <groupId>junit</groupId>
            <artifactId>junit</artifactId>
            <scope>test</scope>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <!-- <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency> -->

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency> 

        <!-- <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>6.0.5</version>
        </dependency> -->

        <!-- <dependency>
            <groupId>com.sybase.jconnect</groupId>
            <artifactId>jconn4</artifactId>
            <version>7.07</version>
            <scope>provided</scope>
        </dependency> -->

        <dependency>
    <groupId>jtds</groupId>
    <artifactId>jtds</artifactId>
    <version>1.2</version>
</dependency>

application.properties

spring.datasource.url=jdbc:sybase:Tds:<server>:<port>/<dbname>
spring.datasource.username=u_****
spring.datasource.password=*****
spring.datasource.driverClassName=com.sybase.jdbc4.jdbc.SybDriver

【讨论】:

使用上面的解决方案,您不需要对jtds 的依赖,因为您没有使用它附带的 JDBC 驱动程序。你使用的是JConn的com.sybase.jdbc4.jdbc.SybDriver,而jtds自带的驱动名称是net.sourceforge.jtds.jdbc.Driver【参考方案3】:

也因为com.sybase.jdbc4.jdbc.SybDriver不是jtds提供的JDBC驱动实现的名字。它附带的正确名称是

net.sourceforge.jtds.jdbc.Driver

我相信 jconn4d.jar 不在 maven Central 上,并且此实现附带的驱动程序名称是 com.sybase.jdbc4.jdbc.SybDriver

更多详情请点击:

https://www.dbvis.com/features/sybase-ase-database-drivers/

http://jtds.sourceforge.net/faq.html#driverImplementation

【讨论】:

【参考方案4】:

这就是我所做的。 Spring JPA 为我工作

1.在资源文件夹中添加了Jconn3.jar(你甚至可以添加它的最新版本)。

2.修改application.properties中的连接测试查询

application.properties

spring.datasource.url=jdbc:sybase:Tds:<server>:<port>/<dbname>
spring.datasource.username=u_****
spring.datasource.password=*****
spring.datasource.driverClassName=com.sybase.jdbc4.jdbc.SybDriver

spring.datasource.hikari.connection-test-query=SELECT 1

【讨论】:

【参考方案5】:

这个问题有点困扰我,但它的解决方案很简单。 首先在你的 pom.XML 中添加依赖:

<dependency>
    <groupId>jtds</groupId>
    <artifactId>jtds</artifactId>
    <version>1.2</version>
</dependency>

那么您的 URL 定义应该遵循这种格式(在 Application.properties 或 Application.yml 中)

jdbc:jtds:sybase://<Host>:<Port>/<database>

这个解决方案应该很好用。

【讨论】:

以上是关于Springboot 与 Sybase 服务器 - 无法加载驱动程序类:com.sybase.jdbc4.jdbc.SybDriver的主要内容,如果未能解决你的问题,请参考以下文章

SpringBoot+Mybatis+SyBase整合详细

SpringBoot+Mybatis+SyBase整合详细

数据库连接无响应 - Sybase

sybase如何建库? 如何先建立服务?万分感谢!!!!!!!!!

linux下如何启动和停止sybase的服务

怎样导出sybase数据库的所有库结构和表结构的建库的sql语句