postgresql 在此 ResultSet 中找不到列名...

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21268415/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 00:35:09  来源:igfitidea点击:

The column name ... was not found in this ResultSet

postgresqljdbc

提问by rootser

We are using java jdk 1.7.0_45, postgresql jdbc connector postgresql-9.3-1100.jdbc41.jar.

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

Here is a synopsis of our problem, as much as possible of code pasted below.

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

This code:

这段代码:

        ResultSet rs = DbConn.getInstance().doQuery("Select d.deptId from Depts d");
                while (rs.next()){
                    System.out.println(rs.getInt("d.deptId"));
    
产生错误:
    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"));
    
不会产生错误。

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

除了删除“d”之外,还有其他方法吗?从第一个查询开始,使第一个代码片段抛出错误消息?

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;
        }
        }

}

回答by Craig Ringer

The query:

查询:

 select d.deptId from Depts d

produces a single-column resultset with the result-alias "deptId". There is no "d.deptId" column. If you want one, you can request that as the column alias instead:

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

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

PgJDBC can't do anything about this because it has no idea that the resultset column "deptId" is related to the "d.deptId" in the select-list. Teaching it about that would force it to understand way more about the SQL it processes than would be desirable, and lead to maintenance and performance challenges.

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

回答by duffymo

The second one works - why isn't that acceptable?

第二个有效 - 为什么不能接受?

You can also do this:

你也可以这样做:

 System.out.println(rs.getInt(1));

If you change the query you have to change the code, too.

如果您更改查询,您也必须更改代码。