在java上连接后选择postgres数据库
此代码连接abd创建数据库,如何选择创建的数据库来使用它在java上连接后选择postgres数据库,java,postgresql,Java,Postgresql,此代码连接abd创建数据库,如何选择创建的数据库来使用它 public class Connect { public static void main(String[] args) { Connection connection = null; try { Class.forName("org.postgresql.Driver"); connection = DriverManager.getConnec
public class Connect {
public static void main(String[] args) {
Connection connection = null;
try {
Class.forName("org.postgresql.Driver");
connection = DriverManager.getConnection(
"jdbc:postgresql://localhost:5432/","postgres", "12345");
Statement statement = connection.createStatement();
statement.execute("CREATE DATABASE mydb");
//now I hav to connect to mydb
connection.close();
} catch (ClassNotFoundException ex) {
Logger.getLogger(OracleToPostgres.class.getName()).log(Level.SEVERE, null, ex);
} catch (SQLException ex) {
Logger.getLogger(OracleToPostgres.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
像这样的东西就行了
Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/mydb", "user", "password");
您是否考虑过使用新数据库创建另一个连接?为什么不关闭该连接并使用正确的数据库打开另一个连接?