gpt4 book ai didi

java - 无法在 SQL Server 中通过 JDBC 调用 CREATE SCHEMA

转载 作者:搜寻专家 更新时间:2023-11-01 03:17:22 25 4
gpt4 key购买 nike

我使用的是官方 SQL Server JDBC 驱动程序:

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

在这里运行这段代码:

try (Connection c = new com.microsoft.sqlserver.jdbc.SQLServerDriver().connect(u, p)) {
try (PreparedStatement s1 = c.prepareStatement("create schema x");
PreparedStatement s2 = c.prepareStatement("drop schema x")) {
System.out.println(s1.execute());
System.out.println(s2.execute());
}
}

但是我收到了这个错误:

com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near the keyword 'schema'.
at com.microsoft.sqlserver.jdbc.SQLServerException.makeFromDatabaseError(SQLServerException.java:258)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.getNextResult(SQLServerStatement.java:1547)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.doExecutePreparedStatement(SQLServerPreparedStatement.java:528)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement$PrepStmtExecCmd.doExecute(SQLServerPreparedStatement.java:461)
at com.microsoft.sqlserver.jdbc.TDSCommand.execute(IOBuffer.java:7151)
at com.microsoft.sqlserver.jdbc.SQLServerConnection.executeCommand(SQLServerConnection.java:2689)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeCommand(SQLServerStatement.java:224)
at com.microsoft.sqlserver.jdbc.SQLServerStatement.executeStatement(SQLServerStatement.java:204)
at com.microsoft.sqlserver.jdbc.SQLServerPreparedStatement.execute(SQLServerPreparedStatement.java:445)

显然,这些语句是正确的,它们作为静态语句工作:

try (Statement s1 = c.createStatement();
Statement s2 = c.createStatement()) {
System.out.println(s1.execute("create schema x"));
System.out.println(s2.execute("drop schema x"));
}

这是故意的,还是 JDBC 驱动程序中的错误?

最佳答案

这似乎是 6.2.0 版本的回归。它曾经在版本 6.1.0 中工作。我已将此问题报告给 Microsoft:https://github.com/Microsoft/mssql-jdbc/issues/370

CREATE VIEW 语句也受到影响:

// Works on both versions 6.2.0 and 6.1.0
try (Statement s1 = c.createStatement();
Statement s2 = c.createStatement()) {
System.out.println(s1.execute("create view x as select 1 a"));
System.out.println(s2.execute("drop view x"));
}

// Works only on version 6.1.0
try (PreparedStatement s1 = c.prepareStatement("create view x as select 1 a");
PreparedStatement s2 = c.prepareStatement("drop view x")) {
System.out.println(s1.execute());
System.out.println(s2.execute());
}

(我在 Stack Overflow 上记录了这一点,因为一些运行 DDL 的工具,包括 jOOQ、Hibernate、MyBatis、Flyway 等可能会受到影响)

关于java - 无法在 SQL Server 中通过 JDBC 调用 CREATE SCHEMA,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44950970/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com