JDBC 应用程序 – 列索引超出范围,0 < 1

huangapple go评论101阅读模式
英文:

JDBC Application - Column Index out of range, 0 < 1

问题

我有这段代码:

  1. try{
  2. Class.forName("com.mysql.jdbc.Driver");
  3. Connection con = DriverManager.getConnection(
  4. "jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8","root","Icdjoil100");
  5. Statement stmt = con.createStatement();
  6. ResultSet rs = stmt.executeQuery("select user_id , user_name from t_user");
  7. while(rs.next()) {
  8. System.out.println(rs.getInt(1));
  9. }
  10. con.close();
  11. }catch(Exception e){
  12. e.printStackTrace();
  13. }

但是在运行示例时,我遇到了这个错误:

  1. java.sql.SQLException: 列索引超出范围,0 < 1
  2. at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
  3. at com.mysql.jdbc.ResultSet.checkColumnBounds(ResultSet.java:684)
  4. at com.mysql.jdbc.ResultSet.getInt(ResultSet.java:2621)
  5. at Test.main(Test.java:20)
英文:

I have this piece of code:

  1. try{
  2. Class.forName(&quot;com.mysql.jdbc.Driver&quot;);
  3. Connection con= DriverManager.getConnection(
  4. &quot;jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8&quot;,&quot;root&quot;,&quot;Icdjoil100&quot;&quot;,&quot;root&quot;,&quot;Iconofcoil100&quot;);
  5. Statement stmt=con.createStatement();
  6. ResultSet rs=stmt.executeQuery(&quot;select user_id , user_name from t_user&quot;);
  7. while(rs.next()) {
  8. System.out.println(rs.getInt(0));
  9. }
  10. con.close();
  11. }catch(Exception e){
  12. e.printStackTrace();
  13. }

but I have this error when running the example:

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

答案1

得分: 0

JDBC(类似于SQL)是一个基于1的API。即列索引从1开始。写入:

  1. rs.getInt(1);

参见ResultSet.getInt()

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

英文:

JDBC (like SQL) is a 1-based API. I.e. column indexes start at 1. Write:

  1. rs.getInt(1);

See ResultSet.getInt()

> columnIndex - the first column is 1, the second is 2, ...

huangapple
  • 本文由 发表于 2020年9月26日 22:41:20
  • 转载请务必保留本文链接:https://go.coder-hub.com/64078967.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定