Cannot call CREATE SCHEMA through JDBC in SQL Server

Viewed 607

I'm using the official SQL Server JDBC driver:

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

To run this code here:

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());
    }
}

But I'm getting this error:

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)

Obviuosly, the statements are correct and they work as static statements:

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"));
}

Is this intended, or a bug in the JDBC driver?

1 Answers
Related