I'm receiving this error : "Column Index out of range, 5 > 4" when I try to print my query.
I know that the error is because I'm trying to get more values that my tables has.... but in my case I trying to do a method which will be called from different query, thats mean that the value rage of each table could be different...
This is the code:
import java.io.PrintWriter;
import java.io.IOException;
import java.sql.ResultSet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class TableCreator {
String Query;
ResultSet rs1;
String string, string1, string2, string3, string4, string5, string6, string7,string8,string9;
int numecolumns;
public TableCreator(String Query, String string, String string1, String string2, String string3, String string4, String string5, String string6, String string7,String string8,String string9){
this.Query = Query;
this.string = string;
this.string1 = string1;
this.string2 = string2;
this.string3 = string3;
this.string4 = string4;
this.string5 = string5;
this.string6 = string6;
this.string7 = string7;
this.string8 = string8;
this.string9 = string9;
}
public void Table(HttpServletRequest req, HttpServletResponse res) throws IOException {
PrintWriter pw = res.getWriter();
res.setContentType("text/html");
try
{
CallDB query = new CallDB(Query);
query.connectDB();
rs1 = query.connectDB();
pw.println(string+ "<br>");
pw.println("<table><tr>");
pw.println("<td><b>"+string+"</b></td>");
pw.println("<td><b>"+string1+"</b></td>");
pw.println("<td><b>"+string2+"</b></td>");
pw.println("<td><b>"+string3+"</b></td>");
pw.println("<td><b>"+string4+"</b></td>");
pw.println("<td><b>"+string5+"</b></td>");
while (rs1.next()){
String string1 = rs1.getString(1);
String string2 = rs1.getString(2);
String string3 = rs1.getString(3);
String string4 = rs1.getString(4);
String string5 = rs1.getString(5);
pw.println("<tr>");
pw.println("<td>"+string1+"</td>");
pw.println("<td>"+string2+"</td>");
pw.println("<td>"+string3+"</td>");
pw.println("<td>"+string4+"</td>");
pw.println("<td>"+string5+"</td>");
pw.println("</tr>");
}
}catch (Exception e) {
e.printStackTrace();
}
}
}
It is working fine if the table has at least the same number of columns but if it's least, I receive the error. There is any way to break the while execution when the column doesn't exist?
thanks in advance