Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Error in updating the DataBase using Update Query

 
Neeraj jain
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When i am trying to update the database i am getting an exception

java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Too few paramet ers. Expected 6.

i hav six fields all of text type in emi table

i am doing like this

PreparedStatement ps=con.prepareStatement("update emi set pass=?,mobile=?,email=?,add=? where Ename=? ");
 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Count the question marks. It doesn't matter how many fields are in the record. Those question marks represent the parameters.
 
Neeraj jain
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ok but still you havn't give solution to my problem
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65335
97
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yeah, he has. How many parameters have you passed to the prepared statement before execution?
 
Neeraj jain
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i have passed total 5 parameters shown below
ps.setString (1,pass);
ps.setString (2,mobile);
ps.setString (3,email);
ps.setString (4,add);
ps.setString(5,name);
ps.executeUpdate();
 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And what was the error message again? You're almost there...

We're not trying to be difficult. We're trying to teach you to analyze the problem and think for yourself which, in the long run, will make you a better programmer.
 
Neeraj jain
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
getting the same error
java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 2.

now please tell me why i am getting it now every time this Excepted no increases or decreases............. please do resolve it as i am using it in my project
 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Let's try again. Your first error was "too few parameters, expected 6" and in your code sample you showed us that you only passed 5 parameters. Hence the error. Now you are doing the same thing some place else, it's expecting 2 and you are only passing 1 or none.

Post your code and lets see if we can walk you through this.
 
Neeraj jain
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I agree with you but as you watched i want to update the 4 fields on the basis of 5th field here is the code .......

As this below is the bean class which i am using in my project so the function whcich i am discussing about is update function
 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting. I'm not sure why it's expecting 6 (or 2) parameters when that's not how you've defined them. First step, replace System.out.println(e) with e.printStackTrace() and post the trace here. Second, you need to add finally blocks to your code, unless you deleted them here just to keep the code small. The finally code should look something like this:


Just replace those LOGGER statements with e.printStackTrace() if you aren't using logging, and the updates don't use a resultset so you can delete that block where it's not needed.
 
Neeraj jain
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
dude At this time i want to acheive this updatation so if you can help in it then do it ............. you are so much experienced but please don't confuse me at starting stage please if can help in this way then do it else thankxx for your time and support
 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am trying to help, but neither me nor anyone else here is going to write code for you. The goal is to teach you, not do your work for you. If you want copy and paste code that you don't understand then I suggest you go to stackoverflow. If you want to understand what you are doing and get better at your job, then try the steps I described.

 
Neeraj jain
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Neither I am asking about writing the code for me nor provide the code to me as you see full code i have written myself and just asking for indication where i have made the mistake
 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you aren't going to provide the stack traces then all I can do is guess, and my best guess is that one of your field names is misspelled. Perhaps pass is actually password or pwd? A misspelled field would cause the driver to think that one of the parameters is missing.

What happens if you try the SQL statement directly in Access?
 
Paul Clapham
Sheriff
Posts: 21572
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
J. Kevin Robbins wrote:If you aren't going to provide the stack traces then all I can do is guess, and my best guess is that one of your field names is misspelled. Perhaps pass is actually password or pwd? A misspelled field would cause the driver to think that one of the parameters is missing.


Yes, indeed. And you'll also get that helpful message if you refer to a table which doesn't exist in the database. So check the spelling of your table name as well.
 
Neeraj jain
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

J. Kevin Robbins -> my field is perfect i have implemented the printStackTrace() still getting the same problem and if my field name is misspelled then i must get error in other functions but there it's working fine
 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post the stacktrace here, please, and also the DDS or whatever Access calls it, of your database.
 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@Paul. I'm not very familiar with Access. Does it generally require a schema name along with the table name, i.e. "select * from myschema/mytable"?

@Neeraj. Are you saying the register method works and only the update method is failing? Because a previous message implied that both were failing.
 
Neeraj jain
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yes only update is giving the problem else working perfectlly
and here is what i get after printStackTrace

java.sql.SQLException: [Microsoft][ODBC Microsoft Access Driver] Too few paramet ers. Expected 6. at sun.jdbc.odbc.JdbcOdbc.createSQLException(JdbcOdbc.java:6879) at sun.jdbc.odbc.JdbcOdbc.standardError(JdbcOdbc.java:7036) at sun.jdbc.odbc.JdbcOdbc.SQLExecute(JdbcOdbc.java:3104) at sun.jdbc.odbc.JdbcOdbcPreparedStatement.execute(JdbcOdbcPreparedState ment.java:214) at sun.jdbc.odbc.JdbcOdbcPreparedStatement.executeUpdate(JdbcOdbcPrepare dStatement.java:136) at pack.RegisterBean.update(RegisterBean.java:60) at jsp_servlet.__login2._jspService(__login2.java:167) at weblogic.servlet.jsp.JspBase.service(JspBase.java:33) at weblogic.servlet.internal.ServletStubImpl$ServletInvocationAction.run (ServletStubImpl.java:1053) at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubIm pl.java:387) at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubIm pl.java:431) at weblogic.servlet.internal.ServletStubImpl.invokeServlet(ServletStubIm pl.java:305) at weblogic.servlet.internal.WebAppServletContext$ServletInvocationActio n.run(WebAppServletContext.java:6291) at weblogic.security.acl.internal.AuthenticatedSubject.doAs(Authenticate dSubject.java:317) at weblogic.security.service.SecurityManager.runAs(SecurityManager.java: 97) at weblogic.servlet.internal.WebAppServletContext.invokeServlet(WebAppSe rvletContext.java:3575) at weblogic.servlet.internal.ServletRequestImpl.execute(ServletRequestIm pl.java:2573) at weblogic.kernel.ExecuteThread.execute(ExecuteThread.java:178) at weblogic.kernel.ExecuteThread.run(ExecuteThread.java:151)
 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please post the DDS or description of your database table. I will create one to match and see if I can run your code, but I need to make sure my table matches yours in field names and data types.
 
Neeraj jain
Ranch Hand
Posts: 108
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey problem resolved as i resetted my database and recreate the table .....................now you can give me tips to improve my JAVA Programming actually that time was a bit frustated
 
J. Kevin Robbins
Bartender
Pie
Posts: 1801
28
Chrome Eclipse IDE Firefox Browser jQuery Linux MySQL Database Netbeans IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interesting. I hate problems that just disappear without an explanation, but sometimes you take what you can get and move on.

As for improvements, look back at my message about using a finally block. The way your code is written it's going to leave connections open and prevent the garbage collector from cleaning them up.

Also, after your executeUpdate you are returning true. That's a dangerous assumption. Read the api for PreparedStatement and you'll find that executeUpdate will return an integer that will tell you if it really was successful. Check that result instead of assuming.
 
jz shang
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
("UPDATE emi SET pass=?,mobile=?,email=?,add=? where Ename=? ");

It might be due to one column in your sql does not exists in your table emi.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic