How To Cause 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 arguing inwards Java. I convey seen "java.sql.SQLException: Invalid column index" coming to a greater extent than oft than non 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 crusade of "java.sql.SQLException: Invalid column index" is a misconception that column index started alongside "0" similar array or String index but that's non truthful instead column index starts alongside "1" together with therefore whenever yous endeavor to teach or Set column information alongside column index "0" yous volition teach "java.sql.SQLException: Invalid column index".
java.sql.SQLException: Invalid column index

An illustration of "java.sql.SQLException: Invalid column index"
here is elementary code illustration of getting about information from PreparedStatement SELECT SQL query. hither nosotros convey seat exactly 1 house holder for passing order_id. if yous exceed house holder anything other than "1" similar "0" or "2" yous volition teach "java.sql.SQLException: Invalid column index" , exactly endeavor it on your halt together with yous volition teach concur of it. same is truthful spell yous are reading information from ResultSet.
Actually, ResultSet offers 2 ways to access column information either past times column advert or column index. if yous 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 advert 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 bird 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 especial 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 number = preStatement.executeQuery();
while(result.next()){
System.out.println("Item: " + result.getString(2)); //this volition every bit good throw "java.sql.SQLException: Invalid column index" because resultset has alone 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 ready “Exception inwards thread "main" java.sql.SQLException: Invalid column index” Just beware that column index on ResultSet together with PreparedStatement parametric enquiry starts alongside 1 together with non 0 together with accessing column alongside index 0 volition number inwards invalid column index error.
Further Learning
JSP, Servlets together with JDBC for Beginners: Build a Database App
Complete JDBC Programming Part 1 together with 2
How to ready java.lang.UnSupportedClassVersionError inwards Java
Komentar
Posting Komentar