java 访问 postgresql

var driver = new org.postgresql.Driver();
var databaseProperties = new Properties();
databaseProperties.setProperty("user", databaseUsername);
databaseProperties.setProperty("password", databasePassword);

try (var connection = driver.connect(databaseUrl, databaseProperties)) {
    try (var statement = connection.prepareStatement(
            "select * from t_ac_securitys_info where module_code = ? limit 1")) {
        statement.setString(1, apartName);
        var result = statement.executeQuery();
        while (result.next()) {
            var ticket = result.getString("authorization_code");
            return ticket;
        }
    }
} catch (SQLException e) {
    e.printStackTrace();
}
databaseUrl
jdbc:postgresql://192.168.x.y:5432/stgl?allowMultiQueries=true&useUnicode=true&characterEncoding=UTF-8&stringtype=unspecified

上面的默认连接public,亦可指定连接其它schema

postgresql-> 9.4 及以后的版本指定方式:

jdbc:postgresql://localhost:5432/postgresql?currentSchema=newschema

数据库中创建schema语句如下:

CREATE SCHEMA IF NOT EXISTS "myschema"
    AUTHORIZATION postgres;

 

 

 

posted on 2022-08-05 14:31  空明流光  阅读(744)  评论(0编辑  收藏  举报

导航