【发布时间】:2018-10-28 11:54:49
【问题描述】:
我使用表“user”创建了模式“customer1”,并尝试使用 Connection.setSchema() 从 JDBC 连接它:
String url = "jdbc:postgresql://localhost/project";
Properties props = new Properties();
props.setProperty("user", "postgres");
props.setProperty("password", "postgres");
try (Connection conn = DriverManager.getConnection(url, props)) {
conn.setSchema("customer1");
try (Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SHOW search_path")) {
rs.next();
System.out.println("search_path: " + rs.getString(1));
}
try (Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT name FROM user LIMIT 1")) {
if (rs.next()) {
System.out.println("user name: " + rs.getString("name"));
}
}
}
此代码打印:
search_path: customer1
然后它会抛出带有消息的 PSQLException:
ERROR: column "name" does not exist
如果我在 SELECT 查询中限定“用户”表:
try (Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery("SELECT name FROM customer1.user LIMIT 1")) {
if (rs.next()) {
System.out.println("user name: " + rs.getString("name"));
}
}
然后打印:
search_path: customer1
user name: name1
并且没有发生错误。我正在使用 JDBC 驱动程序 42.2.2 和 PostgreSQL 服务器 10.4。为什么设置架构不起作用?
【问题讨论】:
标签: java postgresql jdbc schema