日期:2014-05-20  浏览次数:20810 次

使用jdbc查询postgres数据库遇到问题
Java code

package traffic;
import java.sql.*;

public class TrafficDatabaseCore  {
    
    static void getConnection() throws SQLException, InstantiationException, IllegalAccessException, ClassNotFoundException
    {
        Class.forName("org.postgresql.Driver").newInstance();
        Connection conn;
        conn = DriverManager.getConnection("jdbc:postgresql://localhost:5432/traffic", "postgres", "******");
        Statement st = conn.createStatement();
        ResultSet rs = st.executeQuery("select * from test");
        while(rs.next())
        {
            System.out.print(rs.getString(0));
            System.out.println(rs.getInt(1));
        }
    }
    
}




出现异常:org.postgresql.util.PSQLException: 栏位索引超过许可范围:0,栏位数:1。


我使用查询语句"select * from test"在SQL SHELL中查询可以得到正确结果:
  test_str | test_int
----------------------+----------
 0943111196 | 120
(1 行记录)

------解决方案--------------------
列数是从"1"开始的,不能从"0"开始.