my dog learned polymorphism*
The moose likes JDBC and the fly likes Severe Error: Syntax Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "Severe Error: Syntax" Watch "Severe Error: Syntax" New topic
Author

Severe Error: Syntax

Luis Villamarin
Greenhorn

Joined: May 24, 2012
Posts: 21

Hello,

I'm new to java web development and have been trying to set up a basic website. I've having problems with storing information to the database.

I'm getting the following error when I try to store data to the database:

SEVERE: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).

This is the code of the class that manages the data to the database:


I will appreciate if anyone can give me a push in the right direction
Wendy Gibbons
Bartender

Joined: Oct 21, 2008
Posts: 1107

the error usually comes with a line number, could you let us know which line?

and i don't think you need to put the ' around the ? when using a prepared statement.
Martin Vajsar
Sheriff

Joined: Aug 22, 2010
Posts: 3606
    
  60

Wendy Gibbons wrote:and i don't think you need to put the ' around the ? when using a prepared statement.

Good catch. That is also the cause of the problem. When the question mark is in the quotes, it does not represent a parameter, and therefore the number of parameters is zero.
Luis Villamarin
Greenhorn

Joined: May 24, 2012
Posts: 21

Martin and Wendy thank you very much for getting back to me.

I replaced the '?' with ? alone, and I'm now getting this error:

SEVERE: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE emailAddress = 'test@email.com'' at line 1

This is code of UserDB class:



Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150


You still have the extra single quotes around the ? for email address. Also, I never put a comma after my last field (lName) in your case.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Luis Villamarin
Greenhorn

Joined: May 24, 2012
Posts: 21

Hey Jeanne – thanks for suggesting a solution. I removed the coma after the last field as you suggested and it is not throwing any errors anymore. However, the issue that I have right now is that the database is not storing the information. Checked the database after storing the values and nothing is being stored. Do you have any suggestion why this is happening?

Thanks in advance.
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150

Is the insert statement working and not the update? Or the insert doesn't work at all.

What does your program print out when you run it? Any stack traces?

Also, does the insert run if you run it at the command line and not through Java?
Luis Villamarin
Greenhorn

Joined: May 24, 2012
Posts: 21

The Insert command doesn't work at all. No records are being added to the database. I don't have any stack trace the when I run the application the only things shown in Glassfish output log is the confirmation that the application is running and how long it took for it to be deployed.



The information to be added to the database is being passed from a html form.

This is the servlet of registration form for which the database should store the entered information:

Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150


You aren't calling the insert function. If the user's email is not in the database, you should be calling insert rather than update.
Luis Villamarin
Greenhorn

Joined: May 24, 2012
Posts: 21

Jeanne thanks again. I made the corrections now if the user's email is not in the DB the record is inserted in the database.

Now my code looks like this:


After doing this I get again the same error I was getting at the beginning. It does not give a line where the error is though. This is the error I get:

SEVERE: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).

This is the entire stack trace:
Jeanne Boyarsky
internet detective
Marshal

Joined: May 26, 2003
Posts: 30136
    
150

Luis Villamil wrote:After doing this I get again the same error I was getting at the beginning. It does not give a line where the error is though. This is the error I get:

It actually does. The relevant line of the stack trace is
at eBooks.data.UserDB.insert(UserDB.java:35)

What is on that line? In your last post, that line was blank.
Luis Villamarin
Greenhorn

Joined: May 24, 2012
Posts: 21

Jeanne I managed to make it work revising the lines your mentioned.

Thanks you very much.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Severe Error: Syntax
 
Similar Threads
how to convert to statements to an array
Timestamp assistance
Adding code to return an ArrayList of objects
LAST_INSERT_ID() with threads
Validation/DB query question