• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSP & SQL problems

 
Bob Backlund
Ranch Hand
Posts: 51
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi I am try to create a simple page (insert.jsp) that allows me to insert a record into a SQL database, the code looks ok to me and the POST gets the field values, but then i get a "String index out of range error":
java.lang.StringIndexOutOfBoundsException: String index out of range: 307
at java.lang.String.substring(String.java:1666)
The SQL command is a pretty straight forward line:
String strInsertSQL =
("INSERT INTO table1 (company_code,job_number,start_date,end_date,amount,)
VALUES ('"+str1+"','"+str2+"','"+str3+"','"+str4+"','"+str5+"'");
Also I was wondering, is there a way of inserting this without POSTing to another page? Just I'd quite like it if, when you click 'ADD RECORD' the JSP was just refreshed or something. I was thinking could I POST to the same page (as in redirect to the insert.jsp page).
Cheers,
KPS
 
Joe Ess
Bartender
Posts: 9372
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by kps uk:

The SQL command is a pretty straight forward line:
String strInsertSQL =
("INSERT INTO table1 (company_code,job_number,start_date,end_date,amount,)
VALUES ('"+str1+"','"+str2+"','"+str3+"','"+str4+"','"+str5+"'");

If this snippet is exactly how your code appears, I'd say you have more than a few quotation mark problems. You need to escape (i.e. \') all those single quotes.
The String index exception is usually thrown when one uses String.substring(). Do you use that anywhere?
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!