943,657 Members | Top Members by Rank

Ad:
  • JSP Discussion Thread
  • Marked Solved
  • Views: 833
  • JSP RSS
Apr 15th, 2009
0

Syntax error! Urgent.

Expand Post »
Hello, I'm doing a school project in .JSP and I came across this syntax error while inserting data in my database.

DB Query: INSERT INTO MainApplicant (MainApplicantId,FullName, MaritalStatus, Height, Weight, Gender, AgeAsOfLastBDay,
DateOfBirth, PlaceOfBirth, IDNo, CountryOfIssue, Nationality, Race, HomeNo, OfficeNo, MobileNo, Address, PostalCode, CorrAddress, CorrPostalCode, EmailAddress) VALUES ('P-8915710B,'Jaslyn ', 'Single', '160', '45', 'Female', '20', '12/4/1989', 'Singapore', 'S8915710B', 'SG', 'Singaporean', 'Chinese', '-', '-', '-', 'Bishan St 13', '650190', '', '', 'Jaslyn@hotmail.com')

java.sql.SQLException: [Microsoft][ODBC SQL Server Driver][SQL Server]Incorrect
syntax near 'Jaslyn'.

If you realise, there's no closing quote at the end of "P-S8915710B"
I suspect the error is from my JSP page.

<%
String id = request.getParameter("idNo");
String fullName = request.getParameter("fullName");
String maritalStatus = request.getParameter("maritalStatus");
String height = request.getParameter("height");
String weight = request.getParameter("weight");
String gender = request.getParameter("gender");
String age = request.getParameter("age");
String dateOfBirth = request.getParameter("dateOfBirth");
String placeOfBirth = request.getParameter("placeOfBirth");
String idNo = request.getParameter("idNo");
String countryOfIssue = request.getParameter("countryOfIssue");
String nationality = request.getParameter("nationality");
String race = request.getParameter("race");
String homeNo = request.getParameter("homeNo");
String officeNo = request.getParameter("officeNo");
String mobileNo = request.getParameter("mobileNo");
String address = request.getParameter("address");
String postalCode = request.getParameter("postalCode");
String corrAddress = request.getParameter("corrAddress");
String corrPostalCode = request.getParameter("corrPostalCode");
String emailAddress = request.getParameter("emailAddress");
%>
<%
String command1 = request.getParameter("command");

if ((command1 != null) && (command1.equals("insert")))
{
MainApplicant lifeinsured = new MainApplicant();

lifeinsured.setId("P-"+idNo);
lifeinsured.setFullName(fullName);
lifeinsured.setMaritalStatus(maritalStatus);
lifeinsured.setHeight(height);
lifeinsured.setWeight(weight);
lifeinsured.setGender(gender);
lifeinsured.setAge(age);
lifeinsured.setDateOfBirth(dateOfBirth);
lifeinsured.setPlaceOfBirth(placeOfBirth);
lifeinsured.setIdNo(idNo);
lifeinsured.setCountryOfIssue(countryOfIssue);
lifeinsured.setNationality(nationality);
lifeinsured.setRace(race);
lifeinsured.setHomeNo(homeNo);
lifeinsured.setOfficeNo(officeNo);
lifeinsured.setMobileNo(mobileNo);
lifeinsured.setAddress(address);
lifeinsured.setPostalCode(postalCode);
lifeinsured.setCorrAddress(corrAddress);
lifeinsured.setCorrPostalCode(corrPostalCode);
lifeinsured.setEmailAddress(emailAddress);

lifeinsured.createMainApplicant();


The codes in red.
How do I set

JSP Syntax (Toggle Plain Text)
  1. lifeinsured.setId("P-"+idNo);

so that it my ID will be closed with a quote without any syntax near my fullname.

sorry if it's kind of confusing. but I really need some help here.
Thanks a lot!
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aixing is offline Offline
6 posts
since Apr 2009
Apr 16th, 2009
0

Re: Syntax error! Urgent.

The problem is where you create your query not where you read the value and call the set method:
Assuming this:
JSP Syntax (Toggle Plain Text)
  1. public void setId(String id) {
  2. this.id = id;
  3. }

>>> query = . . . . + "'" + id +"', "

I think you forgot to add the single quote at the query.
Post the relevant code where you generate the query
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Apr 16th, 2009
0

Re: Syntax error! Urgent.

The problem is occurring where you are constructing your SQL query you need to show us that code, for us to trace the error.

Also Just for your Information, in case one of the values you wish to insert inside your database contains problem characters like the single quote ('), I advise you use the "PreparedStatement" as shown below:-

java Syntax (Toggle Plain Text)
  1. String name = "abc'jhd";
  2. .
  3. .
  4. Connection con = DriverManager.getConnection(.....);
  5. PreparedStatement ps = con.prepareStatement("INSERT INTO student(name) values (?)");
  6. ps.setString(1,name);
  7. ps.executeUpdate();
  8. .
  9. .
  10. .
Featured Poster
Reputation Points: 653
Solved Threads: 151
Nearly a Posting Virtuoso
stephen84s is offline Offline
1,316 posts
since Jul 2007
Apr 16th, 2009
0

Re: Syntax error! Urgent.

Here's my insert statement in my MainApplicant.java

JSP Syntax (Toggle Plain Text)
  1. public boolean createMainApplicant()
  2. {
  3.  
  4. boolean success = false;
  5. ResultSet rs = null;
  6. DBController db = new DBController();
  7.  
  8.  
  9. String dbQuery = "INSERT INTO MainApplicant (MainApplicantId, FullName, MaritalStatus, Height, Weight, Gender, AgeAsOfLastBDay, DateOfBirth, PlaceOfBirth, IDNo, CountryOfIssue, Nationality, Race, HomeNo, OfficeNo, MobileNo, Address, PostalCode, CorrAddress, CorrPostalCode, EmailAddress) VALUES
  10. ('" + id + "','" + fullName + "', '" + maritalStatus + "','" + height + "', '" + weight + "', '" + gender + "', '" + age + "', '" + dateOfBirth + "', '" + placeOfBirth + "', '" + idNo + "', '" + countryOfIssue + "', '" + nationality +"', '" + race + "', '" + homeNo + "', '" + officeNo + "', '" + mobileNo + "', '" + address + "', '" + postalCode + "', '" + corrAddress + "', '" + corrPostalCode + "', '" + emailAddress + "')";
  11.  
  12. rs = db.updateRequestKey(dbQuery);
  13. try {
  14. if (rs.next()) {
  15.  
  16. id = rs.getString(1);
  17. success = true;
  18.  
  19. }
  20. } catch (Exception e) {
  21. e.printStackTrace();
  22. }
  23.  
  24.  
  25. db.terminate();
  26. return success;
  27.  
  28. }
Last edited by aixing; Apr 16th, 2009 at 3:59 am.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aixing is offline Offline
6 posts
since Apr 2009
Apr 16th, 2009
0

Re: Syntax error! Urgent.

JSP Syntax (Toggle Plain Text)
  1. VALUES ('" + id + "','" + fullName
  2.  

I don't see a missing quote. Are you sure this is the code you tried to run?
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Apr 16th, 2009
0

Re: Syntax error! Urgent.

yup, very sure. I've double checked it a lot of time but the syntax error still exist.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aixing is offline Offline
6 posts
since Apr 2009
Apr 16th, 2009
0

Re: Syntax error! Urgent.

can you do a System.out.println of the values:
id and fullName before you set them at the lifeinsured object?
Sponsor
Featured Poster
Reputation Points: 1014
Solved Threads: 446
Nearly a Senior Poster
javaAddict is offline Offline
3,258 posts
since Dec 2007
Apr 16th, 2009
0

Re: Syntax error! Urgent.

The syntax error is solved!!! Thanks for all your advices!! =)
Reputation Points: 10
Solved Threads: 0
Newbie Poster
aixing is offline Offline
6 posts
since Apr 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in JSP Forum Timeline: jsp+servlet webapp
Next Thread in JSP Forum Timeline: Presenting ResultSet into JSP using Struts <logic .. [help]





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC