Java. SQL. SQLException: Column Index out of range, 0 <1, indexoutofrange
1. Error description
Java. SQL. SQLException: Column Index out of range, 0 <1
2. Error cause
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();}
When rs. next () traverses the query results, the subscript starts from "1", and the print here starts from "0", resulting in an error.
3. Solution
Print "System. out. println (rs. getInt (0) + "-----" + rs. getString (1) + "-----" + rs. getInt (2) + "-----" + rs. getString (3); "to" System. out. println (rs. getInt (1) + "-----" + rs. getString (2) + "-----" + rs. getInt (3) + "-----" + rs. getString (4 ));"