java - java onnect后选择一个postgres数据库

标签 java postgresql

<分区>

这段代码连接abd创建了一个数据库,如何选择创建的数据库来使用它?

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");

关于java - java onnect后选择一个postgres数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48869505/

相关文章:

java - 使用 java dom 设置命名空间和模式

sql - 如何提高按特定时间间隔计算记录的查询的性能?

postgresql - App Engine 的 Spring Boot 实例不断重新连接到 postgres google sql

mysql - 在 RDBMS 中标记已删除条目的更好方法

java - 如何从文本字段获取数字值

java - 是否可以动态生成看起来不错的属性对话框?

java - 如何在 Orientdb 中找到短路径(边而不是顶点)

java - 奇怪的 Cassandra ReadTimeoutExceptions,取决于哪个客户端正在查询

database - 通过两个不同的表获取派生属性

sql - Postgres 选择执行顺序不正确