我想创建显示MSSQL服务器版本的简单Java代码:
public void testMSSQLVersion() throws Exception
{
System.out.println("\nTesting SQL query for MSSQL version\n");
try
{
// Load the SQLServerDriver class, build the
// connection string, and get a connection
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String connectionUrl = "jdbc:sqlserver://localhost:1433;"
+ "database=master;"
+ "user=admin;"
+ "password=dss!Q;";
Connection con = DriverManager.getConnection(connectionUrl);
System.out.println("Connected.");
// Create and execute an SQL statement that returns some data.
String SQL = "SELECT SERVERPROPERTY('productversion'), SERVERPROPERTY('productlevel'), SERVERPROPERTY('edition')";
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery(SQL);
// Iterate through the data in the result set and display it.
while (rs.next())
{
System.out.println(rs.getString(1));
}
}
catch (Exception e)
{
e.printStackTrace();
}
}
但是当我运行代码时,我收到了这个错误:
com.microsoft.sqlserver.jdbc.SQLServerException: The "variant" data type is not supported.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDriverError(SQLServerException.java:190)
at com.microsoft.sqlserver.jdbc.TypeInfo$Builder$16.apply(dtv.java:1996)
at com.microsoft.sqlserver.jdbc.TypeInfo$Builder.build(dtv.java:2158)
这个问题有解决办法吗?
答案 0 :(得分:5)
SELECT ProductVersion = CAST(SERVERPROPERTY('ProductVersion') AS NVARCHAR(128)),
ProductLevel = CAST(SERVERPROPERTY('ProductLevel') AS NVARCHAR(128)),
Edition = CAST(SERVERPROPERTY('Edition') AS NVARCHAR(128))
答案 1 :(得分:0)
更简单的方法就是从数据库端更改数据类型。
转到表的设计,并将列的列数据类型(产品版本,产品级别和版本)更改为NVARCHAR(128)。
我希望这对您有用。编码愉快!