This week's book giveaway is in the OCPJP forum.
We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line!
See this thread for details.
The moose likes JDBC and the fly likes java.sql.SQLException: Parameter index out of range (1 > number of parameters, which Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Databases » JDBC
Bookmark "java.sql.SQLException: Parameter index out of range (1 > number of parameters, which " Watch "java.sql.SQLException: Parameter index out of range (1 > number of parameters, which " New topic
Author

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

Sachindra Pratap
Ranch Hand

Joined: Jun 03, 2008
Posts: 42
code:
---------------------------------------------------------------
String query="INSERT INTO opr_rt_info VALUES ('"+"?"+"','"+
"?"+"','"+"?"+"','"+"?"+"','"+"?"
+"','"+"?"+"')";
System.out.println(query);
pstmt = conn.prepareStatement(query);
pstmt.setString(1,opr_name1); // Exception is thrown
pstmt.setString(2,opr_mode1);
pstmt.setString(3,rt_name1);
pstmt.setString(4,rt_ch_no1);
pstmt.setString(5,rt_ch_freq1);
pstmt.setString(6,rt_selection_flag1);
int res1=pstmt.executeUpdate(query);
--------------------------------------------------------------------end


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

Exception is thrown on instruction

pstmt.setString(1,opr_name1);

if anybody has solution then please help me out.
Regards Sachindra
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

You are using a sql string like this:

which will insert six question marks into your table.

If you want to use a PreparedStatement (which is a good thing) get rid of the quotes round the question marks.
[ August 21, 2008: Message edited by: Paul Sturrock ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Sachindra Pratap
Ranch Hand

Joined: Jun 03, 2008
Posts: 42
Thankyou Paul,
I did same what you suggested ,and it worked.

regards Sachindra
imran sujoy
Greenhorn

Joined: Sep 02, 2008
Posts: 9
I'm facing the same problem.

My java code:



Server log:



mysql table structure:


I'm getting exception at:



I've already registered one user with this. But after that now I'm getting this exception.

Can somebody kindly help?

Thanks
imran sujoy
Greenhorn

Joined: Sep 02, 2008
Posts: 9
===SOLVED===

Please forget about it. I found why the error was occurring. I was actually using one single PreparedStatement for two different methods (silly me), and when the flow returned from the called method, it still was the first preparedstatement rather than the new one, hence it couldn't found the parameter in the proper indexed position.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: java.sql.SQLException: Parameter index out of range (1 > number of parameters, which