Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sachindra Pratap
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Sachindra Pratap
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thankyou Paul,
I did same what you suggested ,and it worked.

regards Sachindra
 
imran sujoy
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
===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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic