java sql SQLException Column Index out of range 0

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

猜你喜欢

转载自www.cnblogs.com/odejsjhshw/p/10368190.html
今日推荐