You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

求助:使用Maven引入db2jcc.jar连接DB2时遇类找不到错误

Fixing ClassNotFoundException for DB2 Driver When Using Maven

It looks like your issue comes down to Maven not being able to properly fetch DB2's JDBC driver dependencies—IBM's DB2 drivers aren't hosted in the default Maven Central Repository, so your current setup can't pull the correct JARs. Here's how to fix this step by step:

1. Add IBM's Official Maven Repository

First, configure Maven to pull DB2 dependencies from IBM's dedicated repository. Add this to your project's pom.xml:

<repositories>
    <repository>
        <id>ibm-db2-jcc-repo</id>
        <url>https://public.dhe.ibm.com/ibmdl/export/pub/software/data/db2/drivers/jcc/maven/</url>
    </repository>
</repositories>

Alternatively, you can add this repository to your global settings.xml if you want it to apply to all your Maven projects.

2. Update Dependency Coordinates

The group ID for DB2 JDBC drivers has been updated in newer versions. Your current coordinates use com.ibm.db2.jcc, which might be outdated. Try using these updated coordinates (adjust the version to match your DB2 server version):

<dependency>
    <groupId>com.ibm.db2</groupId>
    <artifactId>jcc</artifactId>
    <version>11.5.9.0</version>
</dependency>
<dependency>
    <groupId>com.ibm.db2</groupId>
    <artifactId>db2jcc_license_cu</artifactId>
    <version>11.5.9.0</version>
</dependency>

3. Clean and Refresh Maven Dependencies

Force Maven to clear cached dependencies and re-download them with the new repository configuration:

mvn clean install -U

The -U flag ensures Maven updates snapshots and resolves dependencies from the newly added repository.

4. Manual Installation (Fallback Option)

If the repository setup doesn't work, you can manually install your local db2jcc.jar and db2jcc_license_cu.jar into your local Maven repository using these commands:

# Install db2jcc.jar
mvn install:install-file -Dfile=./db2jcc.jar -DgroupId=com.ibm.db2 -DartifactId=jcc -Dversion=3.8.47 -Dpackaging=jar

# Install license file
mvn install:install-file -Dfile=./db2jcc_license_cu.jar -DgroupId=com.ibm.db2 -DartifactId=db2jcc_license_cu -Dversion=3.8.47 -Dpackaging=jar

Just replace ./db2jcc.jar with the actual file path to your local JARs.

5. Double-Check Driver Class Name

Your code uses com.ibm.db2.jcc.DB2Driver, which is correct for most DB2 JDBC driver versions. You can confirm this matches the driver class in your JAR by checking the manifest file or inspecting the JAR's class structure if needed.


内容的提问来源于stack exchange,提问作者santosh

火山引擎 最新活动