Win a copy of Programming with Types this week in the Angular and TypeScript forum
or The Design of Web APIs in the Web Services forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Knute Snortum
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Joe Ess
  • salvin francis

Severe Error: Syntax

 
Greenhorn
Posts: 21
Android Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Bartender
Posts: 1111
Eclipse IDE Oracle VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Sheriff
Posts: 3838
66
Netbeans IDE Oracle Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 21
Android Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:



 
author & internet detective
Posts: 39760
797
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.
 
Luis Villamarin
Greenhorn
Posts: 21
Android Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Posts: 39760
797
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
Android Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Posts: 39760
797
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 21
Android Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
author & internet detective
Posts: 39760
797
Eclipse IDE VI Editor Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 21
Android Chrome Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne I managed to make it work revising the lines your mentioned.

Thanks you very much.
 
Well behaved women rarely make history - Eleanor Roosevelt. tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!