java 在命令行上确定 SQL Server JDBC 版本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32572610/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 20:25:03  来源:igfitidea点击:

Determine the SQL Server JDBC version on command line

javasql-serverjdbc

提问by Mark Filley

I have several servers which use the Microsoft SQL Server JDBC driver. The files are all named sqljdbc4.jar. I need to know what version of the driver each one contains. Time stamps and file sizes are not helpful, as I need to extract the driver version number. I need to be able to run this on the command line.

我有几台使用 Microsoft SQL Server JDBC 驱动程序的服务器。这些文件都被命名为sqljdbc4.jar. 我需要知道每个包含的驱动程序版本。时间戳和文件大小没有帮助,因为我需要提取驱动程序版本号。我需要能够在命令行上运行它。

I have seen for DB2 you can run this command and get the version:

我已经看到对于 DB2,您可以运行此命令并获取版本:

java -cp ./db2jcc.jar com.ibm.db2.jcc.DB2Jcc -version

What is the equivalent, if any, for Microsoft SQL Server?

Microsoft SQL Server 的等效项(如果有)是什么?

采纳答案by wero

It seems that there is no dedicated CLI to print out the driver version but you can ask the MS 4.x driver for its version:

似乎没有专门的 CLI 来打印驱动程序版本,但您可以询问 MS 4.x 驱动程序的版本:

import java.sql.Driver;
import com.microsoft.sqlserver.jdbc.SQLServerDriver;
...
Driver driver = new SQLServerDriver();
driver.getMajorVersion();  // -> 4
driver.getMinorVersion();  // -> 0, 1, 2, ...

You could build a simple commandline wrapper to print out this information.

您可以构建一个简单的命令行包装器来打印此信息。

回答by John Bollinger

IBM provides specific tooling in its JAR to provide for the behavior you describe. It is not a general feature of JDBC drivers or of JAR files.

IBM 在其 JAR 中提供了特定工具来提供您所描述的行为。它不是 JDBC 驱动程序或 JAR 文件的一般特性。

Microsoft documents the available mechanisms for determining the driver version on MSDN. They offer two alternatives:

Microsoft在 MSDN 上记录了用于确定驱动程序版本的可用机制。他们提供了两种选择:

  • extract the information from an [SQLServer]DatabaseMetaDataobject obtained via the driver (i.e. by a Java program); the most appropriate method for your use appears to be getDriverVersion(). Or
  • extract the information from the readme.txtfile provided in the distribution.
  • 从通过驱动程序(即通过Java程序)获得的[ SQLServer]DatabaseMetaData对象中提取信息;最适合您使用的方法似乎是getDriverVersion(). 或者
  • readme.txt分发中提供的文件中提取信息。

Unless readme.txtis packaged in the JAR (possible, but unlikely), the former approach is the only one that will work with the JAR file alone. It shouldn't be too hard to write a Java program and maybe a wrapper script to apply this approach to the task, but it doesn't look anywhere near as simple as for the DB2 driver.

除非readme.txt打包在 JAR 中(可能,但不太可能),否则前一种方法是唯一可以单独使用 JAR 文件的方法。编写 Java 程序和包装器脚本来将这种方法应用于任务应该不会太难,但它看起来不像 DB2 驱动程序那么简单。

回答by Rod Rob

I suggest using this one liner for getting the internal MSSQL driver version:

我建议使用这个 liner 来获取内部 MSSQL 驱动程序版本:

$ echo "System.out.println(new com.microsoft.sqlserver.jdbc.SQLServerDatabaseMetaData(null).getDriverVersion())" | /usr/java/jdk-11.0.2/bin/jshell -q --class-path mssql-jdbc-7.0.0.jre10.jar

jshell> System.out.println(new com.microsoft.sqlserver.jdbc.SQLServerDatabaseMetaData(null).getDriverVersion())
7.0.0.0

jshellcommand is available starting from JDK9. Alternatively you can also use jrunscripton JDK8

jshell命令从 JDK9 开始可用。或者,您也可以在JDK8上使用jrunscript

$ jrunscript -cp mssql-jdbc-7.0.0.jre10.jar -e "java.lang.System.out.println(new com.microsoft.sqlserver.jdbc.SQLServerDatabaseMetaData(null).getDriverVersion())"

Warning: Nashorn engine is planned to be removed from a future JDK release
7.0.0.0