英文:
JDBC Application - Column Index out of range, 0 < 1
问题
我有这段代码:
try{
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8","root","Icdjoil100");
Statement stmt = con.createStatement();
ResultSet rs = stmt.executeQuery("select user_id , user_name from t_user");
while(rs.next()) {
System.out.println(rs.getInt(1));
}
con.close();
}catch(Exception e){
e.printStackTrace();
}
但是在运行示例时,我遇到了这个错误:
java.sql.SQLException: 列索引超出范围,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)
英文:
I have this piece of code:
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();
}
but I have this error when running the example:
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)
答案1
得分: 0
JDBC(类似于SQL)是一个基于1的API。即列索引从1开始。写入:
rs.getInt(1);
> columnIndex - 第一个列为1,第二个为2,...
英文:
JDBC (like SQL) is a 1-based API. I.e. column indexes start at 1. Write:
rs.getInt(1);
> columnIndex - the first column is 1, the second is 2, ...
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论