• 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
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

parameter index out of bounds:3 is not valid value between 1 and 1

 
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi frnds...while using prepared Statement i got a exceptioin "parameter index out of bounds:3 is not valid value between 1 and 1..this comes when i use

"preparedStatement ps;
Connection con;
String sql="insert into table values(?,?,?);
ps=con.preparedStatement(sql);
ps.setInt(1,3);
ps.setInt(2,4);
ps.setString(3,"hi");
ps.execute();
"
please sort out this problem..i need solution urgently.
thanks in advance
 
Marshal
Posts: 64680
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please make sure to post the real code; you haven't specified a table name, so that will probably not execute. It would appear you are trying to insert 3 values into a 1-column table. And there is no such thing as "urgently" on JavaRanch.
 
cchetan jain
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
sorry boss..i 9 there is no urgent term in javaranch..bt i was fed up by that problem ..but at the last i sorted out that.thanks for concern...
 
Campbell Ritchie
Marshal
Posts: 64680
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well done sorting it. Please tell us what you did, in case anybody else has the same problem.
 
cchetan jain
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya sir,..there is a silly mistake by me..i made ps and sql variables as global..so when i use it in a method..it works fine.bt when i called another method in it,then it creates a problem..because in the called method i was using the same ps and sql variables.thats why,the previous ps variable's values is changed..and it was giving that sort type of error..then i sorted out by making a local variable ps in the called method..and as you know,local variable hide the global variable..the previous calling method's ps value did not change..and it worked fine..ok boss:-)
 
Campbell Ritchie
Marshal
Posts: 64680
225
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Seems this is the only thread I can find with that exact error message, so I'll post my findings here.  You can also get this error if your parameter array is defined larger than your SQL is expecting.  Something like this.

Object[] args = new Object[2];
args[0] = paramName;
String sqlQuery = "select a from table_a where name=?";
jdbcTemplate.query(sqlQuery, new FilterStringValueMapper(),args);

It's a simple mistake and it took me way to long to figure it out.

Hope that someone else finds that useful.
 
Saloon Keeper
Posts: 2619
329
Android Eclipse IDE Angular Framework MySQL Database TypeScript Redhat Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe easiest just to do something like this, and not deal with having to bother with sizing the array:
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!