2

This code connects abd create a database ,how can I select the created database to use it?

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);
        }   
    }
}
2
  • have you considered creating another connection with your new database? Commented Feb 19, 2018 at 15:45
  • Why don't you simply close the connection and open another one with the correct db? Commented Feb 19, 2018 at 15:45

1 Answer 1

0

Something like this will do ...

Connection con = DriverManager.getConnection("jdbc:postgresql://localhost:5432/mydb", "user", "password");
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.