• Post Reply Bookmark Topic Watch Topic
  • New Topic

JSP SQL String: Too long?  RSS feed

 
Bradly Fackrell
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I execute this string (I have it entered as one line):

String command = ("INSERT INTO customer (custID, firstName, middleName, lastName, street, city, state, zipcode, homePhone, cellPhone, DOB, profession, agentID, employer, referrer) VALUES ('"+custID+"','"+firstName+"','"+middleName+"','"+lastName+"','"+street+"','"+city+"','"+state+"','"+zipcode+"','"+homePhone+"','"+cellPhone+"','"+DOB+"','"+profession+"','"+agentID+"','"+employer+"','"+referrer+"')");

I get this exception:

org.apache.jasper.JasperException: An exception occurred processing JSP page

But when I cut the string in half (only insert about half of the fields) everything works. Is there a max lenght for a String? Is there another option?
 
Jeanne Boyarsky
author & internet detective
Sheriff
Posts: 36453
459
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, there is not a maximum length for a String. Is it possible one of the variables has a special character or the quotes are wrong?

Note that it is bad practice to have Java code in a JSP. Extracting this code to a Java class would help. And get rid of this problem.
 
Bradly Fackrell
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the pointer.

It turns out that one of my values (the date) was in the wrong format.

I'll certainly consider your suggestion to use a class. Right now I'm "knee deep" in this with a deadline looming and I'm scared to change gears at this point....
 
Paul Clapham
Sheriff
Posts: 22185
38
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And while you're refactoring, you should consider using a PreparedStatement. You say you don't have time to do that now; if you had done it before, you
wouldn't have had the error caused by the date format, because you wouldn't have had to format any dates.

I do recommend doing the refactoring sooner rather than later (i.e. before that code goes into production) as generating queries the way you have done it
leaves you open to SQL injection attacks.

 
Bradly Fackrell
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're both right. I need to spend the time to make this right.

This is actually a project for a school assignment so I want to make sure that I learn the proper way to do this. Can either of you suggest a good place that discusses the advantages/disadvantages of JSP vs. a java class? (My book does not go into the details) To me, they are simply two different ways to get the same result. In fact, when I first read Jeanne's response my initial thought was "both ways require the same code (written a little different) so what's the big deal". I still have a lot to learn
 
Ankit Tripathi
Ranch Hand
Posts: 199
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can either of you suggest a good place that discusses the advantages/disadvantages of JSP vs. a java class?


Certainly this question is out of concern here, if you have known a difference between web applications[made of Servlets and JSPs,run on some server] and stand alone applications[No Server side code,run locally and data is fetched locally if any],this question won't have been raised here.Actually this is not a question of any advantage/disadvange.These are used to serve two completely different purposes.Here I am providing a link to understand the differences.Feal free to query.

Link
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 65833
134
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe this article will help.
 
Bradly Fackrell
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to both of you!

I liked your article Bear. Your theory about always improving holds true in other professions as well. I’ve been a mechanical engineer for 20 years now and I still don’t feel like I’ve mastered my skills. There is always room to learn more.

I truly appreciate people like you who are willing to share their knowledge and experiences.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!