Syntax error! Urgent.

Thread Solved

Join Date: Apr 2009
Posts: 6
Reputation: aixing is an unknown quantity at this point 
Solved Threads: 0
aixing's Avatar
aixing aixing is offline Offline
Newbie Poster

Syntax error! Urgent.

 
0
  #1
Apr 15th, 2009
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

  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!
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,698
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 227
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Syntax error! Urgent.

 
0
  #2
Apr 16th, 2009
The problem is where you create your query not where you read the value and call the set method:
Assuming this:
  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
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Jul 2007
Posts: 1,175
Reputation: stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light stephen84s is a glorious beacon of light 
Solved Threads: 125
Featured Poster
stephen84s's Avatar
stephen84s stephen84s is offline Offline
Veteran Poster

Re: Syntax error! Urgent.

 
0
  #3
Apr 16th, 2009
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:-

  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. .
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand."

"How to ask questions the smart way ?"
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 6
Reputation: aixing is an unknown quantity at this point 
Solved Threads: 0
aixing's Avatar
aixing aixing is offline Offline
Newbie Poster

Re: Syntax error! Urgent.

 
0
  #4
Apr 16th, 2009
Here's my insert statement in my MainApplicant.java

  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,698
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 227
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Syntax error! Urgent.

 
0
  #5
Apr 16th, 2009
  1. VALUES ('" + id + "','" + fullName
  2.  

I don't see a missing quote. Are you sure this is the code you tried to run?
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 6
Reputation: aixing is an unknown quantity at this point 
Solved Threads: 0
aixing's Avatar
aixing aixing is offline Offline
Newbie Poster

Re: Syntax error! Urgent.

 
0
  #6
Apr 16th, 2009
yup, very sure. I've double checked it a lot of time but the syntax error still exist.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 1,698
Reputation: javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all javaAddict is a name known to all 
Solved Threads: 227
Featured Poster
javaAddict's Avatar
javaAddict javaAddict is offline Offline
Posting Virtuoso

Re: Syntax error! Urgent.

 
0
  #7
Apr 16th, 2009
can you do a System.out.println of the values:
id and fullName before you set them at the lifeinsured object?
Check out my New Bike at my Public Profile at the "About Me" tab
Reply With Quote Quick reply to this message  
Join Date: Apr 2009
Posts: 6
Reputation: aixing is an unknown quantity at this point 
Solved Threads: 0
aixing's Avatar
aixing aixing is offline Offline
Newbie Poster

Re: Syntax error! Urgent.

 
0
  #8
Apr 16th, 2009
The syntax error is solved!!! Thanks for all your advices!! =)
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for JSP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC