diff --git a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendConnection.java b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendConnection.java index fc88bb1..4870794 100644 --- a/databend-jdbc/src/main/java/com/databend/jdbc/DatabendConnection.java +++ b/databend-jdbc/src/main/java/com/databend/jdbc/DatabendConnection.java @@ -77,9 +77,9 @@ public class DatabendConnection implements Connection, FileTransferAPI, Consumer DatabendConnection(DatabendDriverUri uri, OkHttpClient httpClient) throws SQLException { requireNonNull(uri, "uri is null"); this.httpUri = uri.getUri(); - this.setSchema(uri.getDatabase()); this.httpClient = httpClient; this.driverUri = uri; + this.setSchema(uri.getDatabase()); DatabendSession session = new DatabendSession.Builder().setHost(this.getURI()).setDatabase(this.getSchema()).build(); this.setSession(session); } @@ -470,6 +470,7 @@ public void setSchema(String schema) throws SQLException { checkOpen(); this.schema.set(schema); + this.startQuery("use " + schema); } @Override diff --git a/databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendDriverUri.java b/databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendDriverUri.java index ffc17f7..39308be 100644 --- a/databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendDriverUri.java +++ b/databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendDriverUri.java @@ -4,6 +4,7 @@ import org.testng.Assert; import org.testng.annotations.Test; +import java.sql.DriverManager; import java.sql.SQLException; import java.util.Properties; @@ -207,4 +208,19 @@ public void testFull() throws SQLException { Assert.assertEquals("null", uri.nullDisplay().toString()); Assert.assertEquals(false, uri.getStrNullAsNull()); } + @Test + public void TestSetSchema() throws SQLException { + String url = "jdbc:databend://databend:databend@localhost:8000/"; + Properties p = new Properties(); + DatabendConnection connection = (DatabendConnection) DriverManager.getConnection(url, p); + try { + connection.createStatement().execute("create or replace database test2"); + connection.createStatement().execute("create or replace table test2.test2(id int)"); + }catch (SQLException e){ + throw new RuntimeException(e); + } + connection.setSchema("test2"); + Assert.assertEquals(connection.getSchema(), "test2"); + connection.createStatement().execute("insert into test2 values (1)"); + } }