New To Java - sql.Exception

plz help me in fixing this error

I got this exception

=============================

java.sql.SQLException: Parameter index out of range (4) number of parameters,which is 3)

=============================

in my database i am having 4 fields first field is auto incremented

and the remaining three i am inserting the values

please help me in fixing this error

thanks

try{

Class.forName("com.mysql.jdbc.Driver");

con= DriverManager.getConnection("jdbc:mysql://553.123.23.540/Bidding","mysql","pennywise");

PreparedStatement ps=con.prepareStatement("insert into User values (?,?,?)");

ps.setString(2,s11);

ps.setString(3,s22);

ps.setString(4,s33);

int i= ps.executeUpdate();

}//try

[1188 byte] By [saamera] at [2007-11-26 23:21:06]
# 1
The first parameter is 1, the second is 2, ...
YoGeea at 2007-7-10 14:25:03 > top of Java-index,Java Essentials,New To Java...
# 2
Change code as...ps.setString(1,s11);ps.setString(2,s22);ps.setString(3,s33);int i= ps.executeUpdate();
elroydsilvaa at 2007-7-10 14:25:03 > top of Java-index,Java Essentials,New To Java...
# 3

U are saying first parameter is first and second is.....

OK ,let me know that when i had Auto Incremented the first feild

in the database so (what i think is) it mean that the first field is automatically incremented and the remaining fields ie 2,3,4 i am inserting the values ..So why it is showing the exception as mention above ...PLZ HELP thanks

saamera at 2007-7-10 14:25:03 > top of Java-index,Java Essentials,New To Java...
# 4
same exception is coming as mention above ...
saamera at 2007-7-10 14:25:03 > top of Java-index,Java Essentials,New To Java...
# 5
You have to specify the position according to the index i.e. 1,2,3.Chack again..
elroydsilvaa at 2007-7-10 14:25:03 > top of Java-index,Java Essentials,New To Java...
# 6

try this instead:

ps.setString(1,s11); //1 -> first '?'

ps.setString(2,s22); //2 -> second '?'

ps.setString(3,s33); //3 -> third '?'

the autoincremented id is inserted automatically.

see this article :

http://www.kitebird.com/articles/jdbc.html

java_2006a at 2007-7-10 14:25:03 > top of Java-index,Java Essentials,New To Java...
# 7
If you have a database trigger then exclude that column from the insert:"insert into user (x, y, z) values (?, ?, ?)"Then setString(1, x) etc.
YoGeea at 2007-7-10 14:25:03 > top of Java-index,Java Essentials,New To Java...
# 8
insert into User values (?,?,?)sorry if i miss understood the question, but the problem is with the insert statement, Try this!insert into user ( field1, field2,field3)values (?,?,?)
G_Abubakra at 2007-7-10 14:25:03 > top of Java-index,Java Essentials,New To Java...
# 9

this the exception again its raising:


java.sql.SQLException:Column count doesn't match value count at row 1

This is the code

package com.pennywise;

import java.io.*;

import java.util.*;

import java.sql.*;

public class UserServices1 implements Serializable

{

public UserServices1(){}

public void createProfile(User user)

{

User us=user;

String s11=us.getUserName();

String s22=us.getUserEmailID();

String s33=us.getUserPassword();

Connection con=null;

try{

Class.forName("com.mysql.jdbc.Driver");

con= DriverManager.getConnection("jdbc:mysql://155.677.87.866/Bidding","mysql","pennywise");

PreparedStatement ps=con.prepareStatement("insert into User values (?,?,?)");

ps.setString(1,s11);

ps.setString(2,s22);

ps.setString(3,s33);

int i= ps.executeUpdate();

}//try

catch(Exception e){

e.printStackTrace();

System.out.println(e);

}finally{

try{

con.close();

}catch(Exception e){}

}//finally

}//createProfile

}//UserService1

saamera at 2007-7-10 14:25:03 > top of Java-index,Java Essentials,New To Java...
# 10
sameer did you try the thing i said?I think not only me.. check reply 7 too
G_Abubakra at 2007-7-10 14:25:03 > top of Java-index,Java Essentials,New To Java...
# 11
the answer is given above !try this:PreparedStatement ps=con.prepareStatement("insert into User (FIELD1_NAME, FIELD2_NAME, FIELD3_NAME) values (?,?,?)");hth
java_2006a at 2007-7-10 14:25:03 > top of Java-index,Java Essentials,New To Java...
# 12
Thanks a lot ABU now it worked succesfully ... I posted that without checking it .....ONCE AGAIN THANKS TO ALL OF THOSE for HELPING ME
saamera at 2007-7-10 14:25:03 > top of Java-index,Java Essentials,New To Java...
# 13
you are welcome sameer= )
G_Abubakra at 2007-7-10 14:25:04 > top of Java-index,Java Essentials,New To Java...