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

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

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(&quot;com.mysql.jdbc.Driver&quot;);
			Connection con= DriverManager.getConnection(
					&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;);
			Statement stmt=con.createStatement();
			ResultSet rs=stmt.executeQuery(&quot;select user_id , user_name from t_user&quot;);
			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 &lt; 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);

参见ResultSet.getInt()

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

英文:

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

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:

确定