gpt4 book ai didi

postgresql - 列名 ... 在此结果集中找不到

转载 作者:行者123 更新时间:2023-11-29 11:30:47 27 4
gpt4 key购买 nike

我们正在使用 java jdk 1.7.0_45,postgresql jdbc 连接器 postgresql-9.3-1100.jdbc41.jar。

这是我们问题的概要,下面粘贴了尽可能多的代码。

这段代码:

        ResultSet rs = DbConn.getInstance().doQuery("Select d.deptId from Depts d");                while (rs.next()){                    System.out.println(rs.getInt("d.deptId"));    
Produces the error:
    org.postgresql.util.PSQLException: The column name d.deptId was not found in this ResultSet.    

This code:

                        ResultSet rs = DbConn.getInstance().doQuery("Select d.deptId from Depts d");                while (rs.next()){                    System.out.println(rs.getInt("deptId"));    
Produces no error.

Is there a way, besides removing the "d." from the first query, to make the first code snippet not throw the error message?

Here is the source code:

public class JoinTest {
@Test
public void test(){
boolean pass = false;
try {
ResultSet rs = DbConn.getInstance().doQuery("Select d.deptId from Depts d");
String label = rs.getMetaData().getColumnLabel(1); // What do you get?
System.out.println("label = " + label);
while (rs.next()){
System.out.println(rs.getInt("d.deptId"));
pass = true;
}
} catch (SQLException e) {
e.printStackTrace();
pass=false;
}
assertTrue(pass);
}
@Test
public void test2(){
boolean pass = false;
try {
ResultSet rs = DbConn.getInstance().doQuery("Select d.deptId from Depts d");
while (rs.next()){
System.out.println(rs.getInt("deptId"));
pass = true;
}
} catch (SQLException e) {
e.printStackTrace();
pass=false;
}
assertTrue(pass);
}
}

public class DbConn {


private static String url = "jdbc:postgresql://server:port/schema";
private static Properties props = new Properties(); {
props.setProperty("user","userid");
props.setProperty("password","passwprd");
}
private Connection conn;

private DbConn(){}
private static DbConn instance;
public static DbConn getInstance() throws SQLException{
if (instance == null){
instance = new DbConn();
instance.conn = DriverManager.getConnection(url, props);
}
return instance;
}
public ResultSet doQuery(String query) throws SQLException{
Logger.log("DbConn.doQuery: " + query);
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery(query);
return rs;
}
}

}

最佳答案

查询:

 select d.deptId from Depts d

生成一个单列结果集,其结果别名为“deptId”。没有“d.deptId”列。如果你想要一个,你可以请求它作为列别名:

 select d.deptId AS "d.deptId" from Depts d

PgJDBC 对此无能为力,因为它不知道结果集列“deptId”与选择列表中的“d.deptId”相关。教导它这将迫使它更多地了解它所处理的 SQL,而不是期望的,并导致维护和性能挑战。

关于postgresql - 列名 ... 在此结果集中找不到,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21268415/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com