maven项目添加db2的jar包

安装完DB2后,SQLLIB文件夹下的java目录下有对应的jar包,我的SQLLIB文件夹位置在 D:Program FilesIBMSQLLIBjava 处。
此目录直接添加到CLASSPATH下,但是还是不能连接。
于是尝试maven手动添加jar包。首先在~目录(如果你的用户名是xiaoming,则对应C:usersxiaoming)新建一个文件夹additional_jars,然后把SQLLIBjava目录下的db2jcc.jar、db2jcc_license_cu.jar、db2jcc4.jar拷贝到该目录下。
然后命令行进入到~/additional_jars文件夹下。db2jccmvn install:install-file "-DgroupId=com.ibm.db2" "-DartifactId=db2jcc" "-Dversion=8.1" "-Dpackaging=jar" "-Dfile=db2jcc.jar"db2jcc4mvn install:install-file "-DgroupId=com.ibm.db2.jcc" "-DartifactId=db2jcc4" "-Dversion=10.1" "-Dpackaging=jar" "-Dfile=db2jcc4.jar"db2jcc_license_cumvn install:install-file "-DgroupId=com.ibm.db2" "-DartifactId=db2jcc_license_cu" "-Dversion=9.7" "-Dpackaging=jar" "-Dfile=db2jcc_license_cu.jar"pom.xml在项目的pom.xml中添加如下依赖:<!-- https://mvnrepository.com/artifact/com.ibm.db2/db2jcc --> <dependency> <groupId>com.ibm.db2</groupId> <artifactId>db2jcc</artifactId> <version>8.1</version> </dependency> <!-- https://mvnrepository.com/artifact/com.ibm.db2.jcc/db2jcc4 --> <dependency> <groupId>com.ibm.db2.jcc</groupId> <artifactId>db2jcc4</artifactId> <version>10.1</version> </dependency> <!-- https://mvnrepository.com/artifact/com.ibm.db2/db2jcc_license_cu --> <dependency> <groupId>com.ibm.db2</groupId> <artifactId>db2jcc_license_cu</artifactId> <version>9.7</version> </dependency>db2命令行创建数据库进入命令行,输入db2cmd,然后输入db2 create database zi创建一个名为zi的数据库。等候一段时间,会出现如下提示:C:Userswang>db2 create database zi DB20000I CREATE DATABASE 命令成功完成。说明创建好了。以下Java程序用于检测:
package com.anbank.datafetch;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DB2Handle {
    public static void main(String[] args) {
        String jdbcClassName="com.ibm.db2.jcc.DB2Driver";
        String url="jdbc:db2://localhost:50000/zi";
        String user="db2admin";
        String password="password";
 
        Connection connection = null;
        try {
            //Load class into memory
            Class.forName(jdbcClassName);
            //Establish connection
            connection = DriverManager.getConnection(url, user, password);
            
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } catch (SQLException e) {
            e.printStackTrace();
        }finally{
            if(connection!=null){
                System.out.println("Connected successfully.");
                try {
                    connection.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
 
    }
}

相关内容推荐