1. 程式人生 > >java sql SQLException Column Index out of range 0

java sql SQLException Column Index out of range 0

tst http details tps drive otf gets teacher eache

1、錯誤描述

java.sql.SQLException:Column Index out of range,0<1

2、錯誤原因

try 
{
	Class.forName("com.mysql.jdbc.Driver");
	Connection conn = null;
	Statement stat = null;
	ResultSet rs = null;
	try 
	{
		conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/school", "root", "root");
		stat = conn.createStatement();
		rs = stat.executeQuery(" select * from teacher ");
		while(rs.next())
		{
			System.out.println(rs.getInt(0)+"-----"+rs.getString(1)+"-----"+rs.getInt(2)+"-----"+rs.getString(3));
		}
	} 
	catch (SQLException e) 
	{
		e.printStackTrace();
	}
			
} 
catch (ClassNotFoundException e) 
{
	e.printStackTrace();
}

由於rs.next()遍歷查詢結果時,下標是從“1”開始,而這裏打印是從“0”開始,導致出錯

3、解決辦法

將遍歷打印“System.out.println(rs.getInt(0)+"-----"+rs.getString(1)+"-----"+rs.getInt(2)+"-----"+rs.getString(3));”修改為“System.out.println(rs.getInt(1)+"-----"+rs.getString(2)+"-----"+rs.getInt(3)+"-----"+rs.getString(4));”

再分享一下我老師大神的人工智能教程吧。零基礎!通俗易懂!風趣幽默!還帶黃段子!希望你也加入到我們人工智能的隊伍中來!https://blog.csdn.net/jiangjunshow

java sql SQLException Column Index out of range 0