这就是我要尝试的操作,我想连接到默认的管理数据库,创建一个新数据库,然后立即切换到该数据库。
下面是我的伪代码:
// Create a new datasource
dataSource = PGSimpleDataSource()
dataSource.setURL("jdbc:postgresql://mydbserver:5432/admin")
dataSource.setUser("test")
dataSource.setPassword("testing")
// get connection to admin db and create a new database named 'mynewdb'
connection = dataSource.getConnection()
cs = connection.createStatement()
cs.executeUpdate("create database mynewdb")
cs.close()
connection.close()
// switch to the newly created database using the **same datasource**
dataSource.setDatabaseName(mynewdb)
connection_to_new_db = dataSource.getConnection()
ps = connection_to_new_db.preparedStatement()
....start doing transactions on the newly created database...
....start doing transactions on the newly created database...
上面的代码似乎可以正常工作。
问题: