繁体   English   中英

如何使用 Vert.x JDBC 客户端管理 Microsoft SQL Server?

[英]How to manage Microsoft SQL Server with Vert.x JDBC client?

在 Vert.x 应用程序(在 Windows 10 上运行)中,我尝试使用 JDBCClient 来连接到 Microsoft SQL Server 2008 R2。

这些是我到目前为止完成的步骤:

  1. https://www.microsoft.com/en-us/download下载 JDBC 驱动程序。
  2. 设置环境变量CLASSPATH=<path>/mssql-jdbc7.0.0.jre8.jar
  3. 将 JDBC Client 和 jdbc 驱动程序的依赖项添加到 pom.xml 文件中。
  4. 使用命令编译应用程序: mvn clean package from project home
  5. 使用java -jar ./target/<app-name>-SNAPSHOT-fat.jar运行应用程序

这是应用程序 pom.xml 依赖项:

<dependency>
 <groupId>io.vertx</groupId>
 <artifactId>vertx-jdbc-client</artifactId>
</dependency>
<dependency>
    <groupId>com.microsoft.sqlserver</groupId>
    <artifactId>mssql-jdbc</artifactId>
    <version>7.0.0.jre8</version>
</dependency>

以及创建客户端和获取连接的代码片段:

JDBCClient client = JDBCClient.createShared(
       vertx,
       new io.vertx.core.json.JsonObject()
           .put("url", "jdbc:sqlserver://192.168.1.180;databaseName=<dbname>;user=<usr>;password=<psw>")
           .put("driver_class", "com.microsoft.sqlserver.jdbc.SQLServerDriver")
   );
   client.getConnection(res -> {
       if (res.succeeded())
           System.out.println("Connection opened");
       else {
           try {
               System.out.println("Connection failed");
               throw res.cause();
           } catch (Throwable e) {System.out.println(e.getMessage());}
       }
   });

运行应用程序后,我收到以下错误:

>java -jar .\target\mssql-1.0.0-SNAPSHOT-fat.jar
Error: A JNI error has occurred, please check your installation and try again
Exception in thread "main" java.lang.SecurityException: Invalid signature file digest for Manifest main attributes
        at sun.security.util.SignatureFileVerifier.processImpl(Unknown Source)
        at sun.security.util.SignatureFileVerifier.process(Unknown Source)
        at java.util.jar.JarVerifier.processEntry(Unknown Source)
        at java.util.jar.JarVerifier.update(Unknown Source)
        at java.util.jar.JarFile.initializeVerifier(Unknown Source)
        at java.util.jar.JarFile.ensureInitialization(Unknown Source)
        at java.util.jar.JavaUtilJarAccessImpl.ensureInitialization(Unknown Source)
        at sun.misc.URLClassPath$JarLoader$2.getManifest(Unknown Source)
        at java.net.URLClassLoader.defineClass(Unknown Source)
        at java.net.URLClassLoader.access$100(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.net.URLClassLoader$1.run(Unknown Source)
        at java.security.AccessController.doPrivileged(Native Method)
        at java.net.URLClassLoader.findClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
        at java.lang.ClassLoader.loadClass(Unknown Source)
        at sun.launcher.LauncherHelper.checkAndLoadMain(Unknown Source)

有人可以告诉我我错了吗?

我解决了这些问题,这里是将外部和签名的 jar 集成到使用 maven 编译和运行的 vertx 项目的分步一般过程:

  1. 从项目主目录创建一个不同的本地 Maven 存储库:
mvn deploy:deploy-file -Dfile=<path-to-.jar-file> -DgroupId=<group-id> -DartifactId=<artifact-id> -Dversion=<version> -Dpackaging=jar -Durl=file:./extlib/ -DrepositoryId=extlibid -DupdateReleaseInfo=true
  1. 然后将以下存储库添加到 pom.xml 中:
<repositories>
    <repository>
        <id>extlibid</id>
        <url>file:///${project.basedir}/extlib</url>
    </repository>
</repositories>
  1. 现在为外部 jar 添加依赖项:
...
<dependency>
    <groupId>com.sample.example</groupId>
    <artifactId>example-app</artifactId>
    <version>1.0</version>
</dependency>
...
  1. 最后,也是最重要的,如果外部 jar 是自行签名的,则排除不同的签名
...
<plugin>
 <artifactId>maven-shade-plugin</artifactId>
 <version>${maven-shade-plugin.version}</version>
 <executions>
  <execution>
  <phase>package</phase>
  <goals>
   <goal>shade</goal>
  </goals>
 <configuration>
...
<filters>
 <filter>
  <artifact>*:*</artifact>
  <excludes>
   <excludes>META-INF/*.SF</excludes>
   <excludes>META-INF/*.DSA</excludes>
   <excludes>META-INF/*.RSA</excludes>
  </excludes>
 </filter>
</filters>
...

AFAIK jdbc 是一个阻塞驱动程序。 您将失去使用异步 Web 服务器的所有好处。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM