The moose likes JDBC and the fly likes JSP & SQL problems Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "JSP & SQL problems" Watch "JSP & SQL problems" New topic

JSP & SQL problems

Bob Backlund
Ranch Hand

Joined: Jun 05, 2003
Posts: 51
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).

Joe Ess

Joined: Oct 29, 2001
Posts: 8834

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?

"blabbing like a narcissistic fool with a superiority complex" ~ N.A.
[How To Ask Questions On JavaRanch]
jQuery in Action, 2nd edition
subject: JSP & SQL problems
Similar Threads
weird functioning of servlet
can you help with this sql statement to insert checkbox data using JSP?
I face a New java.sql.SQLException
MS SQL Server and Java...
Before insert Trigger