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

时间:2014-01-21 20:27:24

标签: postgresql jdbc

我们正在使用java jdk 1.7.0_45,postgresql jdbc connector 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"));
    
    产生错误:     
    org.postgresql.util.PSQLException: The column name d.deptId was not found in this ResultSet.
    

此代码:     

                        ResultSet rs = DbConn.getInstance().doQuery("Select d.deptId from Depts d");
                while (rs.next()){
                    System.out.println(rs.getInt("deptId"));
    
    不会产生错误。

除了删除“d”之外,还有办法吗?从第一个查询,到第一个代码片段抛出错误信息?

以下是源代码:

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

}

3 个答案:

答案 0 :(得分:7)

查询:

 select d.deptId from Depts d

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

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

PgJDBC对此无能为力,因为它不知道结果集列“deptId”与select-list中的“d.deptId”相关。对它进行教学将迫使它更多地了解它所处理的SQL的方式,并导致维护和性能方面的挑战。

答案 1 :(得分:3)

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

你也可以这样做:

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

如果您更改了查询,则还必须更改代码。

答案 2 :(得分:0)

将“ application.properties”中的方言属性从“ org.hibernate.dialect.PostgreSQL10Dialect”更改为“ org.hibernate.dialect.MySQL5InnoDBDialect”。

org.hibernate.dialect.PostgreSQL10Dialect-> org.hibernate.dialect.MySQL5InnoDBDialect

将解决问题。

将属性添加到“ application.properties”中(如果不存在)。 spring.jpa.properties.hibernate.dialect = org.hibernate.dialect.MySQL5InnoDBDialec