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

标签 postgresql jdbc

我们正在使用 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/

相关文章:

r - 多个 selectInput 值会产生意外的 dplyr (postgres) 行为

hibernate - 在 Hibernate 中查询复合表

sql - Postgresql 按不同表中的多列排序

java - 是否可以在一个属性文件中包含两个不同的数据库属性?

java - 通过 JDBC 插入时出错

java - 在 JDBC 中将密码哈希转换为密码

sql - 如何在连续几天的 'streak' 中向行添加运行计数

sql - 如何合并2个完全不同的表?

java - 我应该将哪个 JDBC jar 与 java 1.5.0_16 和 PostgreSQL 8.3.5 一起使用?

java - LocalContainerEntityManagerFactoryBean 无法加载 JDBC 驱动程序类 'org.h2.Driver' 无法构建 Hibernate SessionFactory