15

我正在尝试建立与数据库的连接。这是一个使用 maven 的简单项目。

我有问题sqljdbc_auth.dll

我添加了 mssql jdbc 驱动程序并添加了一个依赖项pom.xml

<dependency>
    <groupId>com.microsoft</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>4.0.0</version>
</dependency>

这是我的尝试块

try {
    // Establish the connection. 
    SQLServerDataSource ds = new SQLServerDataSource();
    ds.setIntegratedSecurity(true);
    ds.setServerName("BUILDSRV");
    ds.setDatabaseName("master");
    ds.setIntegratedSecurity(true);
    con = ds.getConnection();       
}

我得到这个错误

    21.11.2012 18:07:04 com.microsoft.sqlserver.jdbc.AuthenticationJNI <clinit>
    WARNING: Failed to load the sqljdbc_auth.dll cause :- no sqljdbc_auth in       java.library.path
    com.microsoft.sqlserver.jdbc.SQLServerException:

我有我的sqljdbc_auth.dll,但我不需要把它放到我的C:\windows\... 我需要从 maven 将它添加到我的项目中。我怎样才能做到这一点?

我试图将它添加到pom.xml但它不起作用

<plugin>
    <groupId>org.codehaus.mojo</groupId>
    <artifactId>build-helper-maven-plugin</artifactId>
    <version>1.1</version>
    <executions>
    <execution>
        <id>attach-artifacts</id>
        <goals>
            <goal>attach-artifact</goal>
        </goals>
        <configuration>
            <artifacts>
                <file>target</file>
                <type>dll</type>
            </artifacts>
        </configuration>
    </execution>
    </executions>
</plugin>

我在构建时遇到了另一个错误

Failed to execute goal org.codehaus.mojo:build-helper-maven-plugin:1.1:attach-artifact (attach-artifacts) on project mavenproject1dbconnect: Unable to parse configuration of mojo org.codehaus.mojo:build-helper-maven-plugin:1.1:attach-artifact for parameter file: Cannot configure instance of org.codehaus.mojo.buildhelper.Artifact from target -> [Help 1]
4

3 回答 3

0

我不认为你需要在maven-helper-plugin这里使用。这里的文档说您需要安装dll 或在系统变量中指定其路径java.library.path

看看 http://msdn.microsoft.com/en-us/library/ms378428.aspx#Connectingintegrated

更新:确保 dll 与您的 jar 一起分发。如果您使用的是 maven,请将 dll 文件放在您的 src/main/resources 文件夹中。然后通过将其从包中排除来确保 dll 最终在 jar 之外。按照类似于此处概述的步骤进行操作。target在此之后,您应该在目录中拥有您的 dll 以及您构建的 jar 文件

然后,当您运行应用程序时,将系统属性作为命令行参数传递java -Djava.library.path=.

如果您不想打扰通过命令行传递参数 - 您可以使用提取当前工作目录System.getProperty("user.dir"))并按照此处所述的步骤在您的方法java.library.path中以编程方式将您设置为当前目录main

于 2012-11-21T15:58:55.327 回答
0

根据此答案java.library.path中找到的信息,您可以通过在运行时注入 dll 来实现。

我已经在生产环境中运行了几年,在 Java <= 8 上没有问题。不过,从来没有真正使用过新版本。快速测试适用于 Java 11,而不是 Java 15

这是我的做法:

  1. 将 dll 存储在您的应用程序路径中。例如:
project_root
├─src (or any other source path structure)
│ └─...packages...
└─extlib
  └─SqlJdbc
    └─auth
      ├─x64
      │ └─sqljdbc_auth.dll
      └─x86
        └─sqljdbc_auth.dll
  1. 在建立 JDBC 连接的类中使用以下静态初始化代码(java <= 11 版本)
static {
    File file;
    if ("64".equals(System.getProperty("sun.arch.data.model")))
        file = new File("./extlib/SqlJdbc/auth/x64");
    else
        file = new File("./extlib/SqlJdbc/auth/x86");
    String dllPath;
    try {
        dllPath = file.getCanonicalPath();
        synchronized(Runtime.getRuntime()) {
            final Field usrPathsField = ClassLoader.class.getDeclaredField("usr_paths");
            usrPathsField.setAccessible(true);
            Object o = usrPathsField.get(null);
            String[] paths = o == null ? new String[0] : (String[]) o;
            if (!Arrays.stream(paths).anyMatch(s -> dllPath.equals(s))) {
                String[] usr_paths = Arrays.copyOf(paths, paths.length + 1);
                usr_paths[paths.length] = file.getCanonicalPath();
                usrPathsField.set(null, usr_paths);
            }
        }
        DriverManager.registerDriver(new SQLServerDriver());
    } catch (Exception e) {
        e.printStackTrace();
        throw new ExceptionInInitializerError(e);
    }
}

根据这个答案,您应该能够将其推送到至少 Java <= 13 使用MethodHandles.privateLookupIn而不是ClassLoader.class.getDeclaredField. 例如:

Lookup cl = MethodHandles.privateLookupIn(ClassLoader.class, MethodHandles.lookup());
VarHandle usrPathsField= cl.findStaticVarHandle(ClassLoader.class, "usr_paths", String[].class);
Object o = usrPathsField.get();
...
usrPathsField.set(usr_paths);

Java 15 肯定会破坏它,因为字段usr_paths不再存在ClassLoader

此时,您应该能够从 IDE 运行代码

  1. 将包含 dll(即extlib)的目录与您的 jar 一起部署。对于 Maven,您可以在 pom.xml 中使用以下内容:
<plugin>
    <artifactId>maven-resources-plugin</artifactId>
    <version>3.0.2</version>
    <executions>
        <execution>
            <id>copy-resources</id>
            <phase>prepare-package</phase>
            <goals>
                <goal>copy-resources</goal>
            </goals>
            <configuration>
                <outputDirectory>${project.build.directory}/extlib</outputDirectory>
                <resources>
                    <resource>
                        <directory>extlib</directory>
                        <filtering>false</filtering>
                    </resource>
                </resources>
            </configuration>
        </execution>
    </executions>
</plugin>

从那里开始,如果您需要将 dll 打包到 jar 中,您可以使用maven-resources-plugin,并在运行时解压缩所需的 dll,然后使用上述代码的变体将其强制到库路径中。

于 2021-04-29T12:34:47.503 回答
0

将 dll 直接放在与主 jar 相同的级别应该就足够了。我怀疑您的问题与 dll 的 x64 和 x86 版本有关。你确定你的版本是一致的吗?

我正在使用这个部门:

<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>9.1.1.jre8-preview</version>
</dependency>

和dll:

mssql-jdbc_auth-9.1.1.x64-preview.dll

对于我的 64 位项目,它工作正常

于 2021-06-11T19:40:18.317 回答