How To Laid Java.Sql.Sqlexception: Invalid Column Index
"java.sql.SQLException: Invalid column index" is a frequent mistake spell working inwards Java Database Connectivity (JDBC). As the name suggests "Invalid column index" its related to accessing or setting column inwards your SQL Query using prepared contestation inwards Java. I accept seen "java.sql.SQLException: Invalid column index" coming generally due to 2 reason:
1) Setting column information using setXXXX(int coloumIndex) e.g. setInt(0) setString(0)
2) Getting column information using getXXX(int columnIndex) e.g. getInt(0) getString(0)
Most mutual drive of "java.sql.SQLException: Invalid column index" is a misconception that column index started amongst "0" similar array or String index but that's non truthful instead column index starts amongst "1" in addition to thus whenever you lot endeavour to larn or Set column information amongst column index "0" you lot volition larn "java.sql.SQLException: Invalid column index".
java.sql.SQLException: Invalid column index
That's was the 1 instance precisely about other instance was you lot are merely putting wrong column index, similar if you lot inquiry is returning iii columns in ResultSet in addition to you lot are trying to access fourth column, JDBC volition throw "java.sql.SQLException: Invalid column index" or if your PreparedStatement has iii house holders but you lot are trying to laid information on fourth column index you lot volition live on greeted past times "java.sql.SQLException: Invalid column index". In side past times side section, nosotros volition encounter a existent life instance of "java.sql.SQLException: Invalid column index" spell writing JDBC code.
An instance of "java.sql.SQLException: Invalid column index"
here is uncomplicated code instance of getting precisely about information from PreparedStatement SELECT SQL query. hither nosotros accept position precisely 1 house holder for passing order_id. if you lot overstep house holder anything other than "1" similar "0" or "2" you lot volition larn "java.sql.SQLException: Invalid column index" , precisely endeavour it on your halt in addition to you lot volition larn concord of it. same is truthful spell you lot are reading information from ResultSet.
Actually, ResultSet offers 2 ways to access column information either past times column refer or column index. if you lot access column information using wrong column name, JDBC volition throw "java.sql.SQLException: Invalid column name" spell if index is wrong JDBC volition throw "java.sql.SQLException: Invalid column index" . I prefer accessing column information using the refer because it's to a greater extent than readable inwards code.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
public flat InvalidColumnIndexExample {
world static void main(String args[]) throws SQLException {
Connection conn = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1526:TESTID", "root", "root");
PreparedStatement preStatement = conn.prepareStatement("select distinct detail from Order where order_id=?");
preStatement.setString(0, "123456"); //this volition throw "java.sql.SQLException: Invalid column index" because "0" is non valid colum index
ResultSet lawsuit = preStatement.executeQuery();
while(result.next()){
System.out.println("Item: " + result.getString(2)); //this volition equally good throw "java.sql.SQLException: Invalid column index" because resultset has entirely 1 column
}
}
}
Output:
Exception inwards thread "main" java.sql.SQLException: Invalid column index
at oracle.jdbc.driver.OraclePreparedStatement.setStringInternal(OraclePreparedStatement.java:7700)
at oracle.jdbc.driver.OraclePreparedStatement.setString(OraclePreparedStatement.java:7654)
at oracle.jdbc.driver.OraclePreparedStatementWrapper.setString(OraclePreparedStatementWrapper.java:910)
That’s all on how to cook “Exception inwards thread "main" java.sql.SQLException: Invalid column index” Just beware that column index on ResultSet in addition to PreparedStatement parametric inquiry starts amongst 1 in addition to non 0 in addition to accessing column amongst index 0 volition lawsuit inwards invalid column index error.
Further Learning
JSP, Servlets in addition to JDBC for Beginners: Build a Database App
Complete JDBC Programming Part 1 in addition to 2
How to cook java.lang.UnSupportedClassVersionError inwards Java
0 Response to "How To Laid Java.Sql.Sqlexception: Invalid Column Index"
Post a Comment