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

roo*_*ser 4 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;
        }
        }

}
Run Code Online (Sandbox Code Playgroud)

Cra*_*ger 9

查询:

 select d.deptId from Depts d
Run Code Online (Sandbox Code Playgroud)

使用结果别名"deptId"生成单列结果集.没有"d.deptId"列.如果你想要一个,你可以请求作为列别名:

 select d.deptId AS "d.deptId" from Depts d
Run Code Online (Sandbox Code Playgroud)

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


duf*_*ymo 3

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

您还可以这样做:

 System.out.println(rs.getInt(1));
Run Code Online (Sandbox Code Playgroud)

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