diff --git a/src/test/java/com/microsoft/sqlserver/jdbc/databasemetadata/DatabaseMetaDataTest.java b/src/test/java/com/microsoft/sqlserver/jdbc/databasemetadata/DatabaseMetaDataTest.java index e4f2e9f1a..afb0547b2 100644 --- a/src/test/java/com/microsoft/sqlserver/jdbc/databasemetadata/DatabaseMetaDataTest.java +++ b/src/test/java/com/microsoft/sqlserver/jdbc/databasemetadata/DatabaseMetaDataTest.java @@ -190,13 +190,12 @@ public void testDBSchemasForDashedCatalogName() throws SQLException { UUID id = UUID.randomUUID(); String testCatalog = "dash-catalog" + id; String testSchema = "some-schema" + id; - String dropDBIfExists = "IF EXISTS (SELECT name FROM sys.databases WHERE name = N'" + testCatalog + "') " + "DROP DATABASE [" + testCatalog - + "]"; + Statement stmt = connection.createStatement(); try (Connection dashConn = DriverManager.getConnection(connectionString); Statement dashStatement = dashConn.createStatement()) { - connection.createStatement().execute(dropDBIfExists); - connection.createStatement().execute(String.format("CREATE DATABASE [%s]", testCatalog)); + Utils.dropDatabaseIfExists(testCatalog, stmt); + stmt.execute(String.format("CREATE DATABASE [%s]", testCatalog)); dashStatement.execute(String.format("USE [%s]", testCatalog)); dashStatement.execute(String.format("CREATE SCHEMA [%s]", testSchema)); @@ -230,8 +229,9 @@ public void testDBSchemasForDashedCatalogName() throws SQLException { assertTrue(hasDashCatalogSchema, dashCatalogFormat.format(new Object[] {testSchema})); } finally { - connection.createStatement().execute(dropDBIfExists); + Utils.dropDatabaseIfExists(testCatalog, stmt); } + stmt.close(); } /** @@ -244,13 +244,12 @@ public void testDBSchemasForDashedCatalogNameWithPattern() throws SQLException { UUID id = UUID.randomUUID(); String testCatalog = "dash-catalog" + id; String testSchema = "some-schema" + id; - String dropDBIfExists = "IF EXISTS (SELECT name FROM sys.databases WHERE name = N'" + testCatalog + "') " + "DROP DATABASE [" + testCatalog - + "]"; + Statement stmt = connection.createStatement(); try (Connection dashConn = DriverManager.getConnection(connectionString); Statement dashStatement = dashConn.createStatement()) { - connection.createStatement().execute(dropDBIfExists); - connection.createStatement().execute(String.format("CREATE DATABASE [%s]", testCatalog)); + Utils.dropDatabaseIfExists(testCatalog, stmt); + stmt.execute(String.format("CREATE DATABASE [%s]", testCatalog)); dashStatement.execute(String.format("USE [%s]", testCatalog)); dashStatement.execute(String.format("CREATE SCHEMA [%s]", testSchema)); @@ -277,8 +276,9 @@ public void testDBSchemasForDashedCatalogNameWithPattern() throws SQLException { assertTrue(hasResults, atLeastOneFoundFormat.format(schemaMsgArgs)); } finally { - connection.createStatement().execute(dropDBIfExists); + Utils.dropDatabaseIfExists(testCatalog, stmt); } + stmt.close(); } /**