-1

我有这段代码:

try{
            Class.forName("com.mysql.jdbc.Driver");
            Connection con= DriverManager.getConnection(
                    "jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8","root","Icdjoil100"","root","Iconofcoil100");
            Statement stmt=con.createStatement();
            ResultSet rs=stmt.executeQuery("select user_id , user_name from t_user");
            while(rs.next()) {
                System.out.println(rs.getInt(0));
            }
            con.close();
        }catch(Exception e){
            e.printStackTrace();
        }

但是运行示例时出现此错误:

java.sql.SQLException: Column Index out of range, 0 < 1.
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
    at com.mysql.jdbc.ResultSet.checkColumnBounds(ResultSet.java:684)
    at com.mysql.jdbc.ResultSet.getInt(ResultSet.java:2621)
    at Test.main(Test.java:20)
4

1 回答 1

0

JDBC(如 SQL)是基于 1 的 API。即列索引从 1 开始。写:

rs.getInt(1);

ResultSet.getInt()

columnIndex - 第一列为 1,第二列为 2,...

于 2020-09-26T14:42:21.013 回答