• 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
  • Paul Clapham
  • Jeanne Boyarsky
  • Junilu Lacar
  • Henry Wong
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Frits Walraven
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • salvin francis
  • fred rosenberger

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

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
public class ForgetDao {

public static Connection dbCon;

public ForgetDao(Connection con) {
dbCon = con;
}

public static ForgetObj forget(ForgetObj user) {
PreparedStatement pstmt = null;


String email = user.getEmail();
String password = user.getPassword();

System.out.println("DAO email '"+user.getEmail()+"'DAO password '"+user.getPassword()+"'");

String sql ="update registration set password ='"+password+"' where email='"+email+"'";
// String sql ="update registration set password ='?' where email='?'";
System.out.println("str sql==> "+sql);


try{
dbCon = DBConnection.getConnection();

pstmt = dbCon.prepareStatement(sql);

pstmt.setString(1, user.getEmail());
pstmt.setString(2, user.getPassword());


int update = pstmt.executeUpdate();
System.out.println("update==> "+update);
System.out.println("pstm value==> "+pstmt.execute(sql));

if (update != 1)
throw new Exception("record not updated..");

} catch (Exception ex) {

System.out.println("Update failed " + ex);

}

finally {
if (pstmt != null) {
try {
pstmt.close();
} catch (Exception e) {
}
pstmt = null;
}
if (dbCon != null) {
try {
dbCon.close();
} catch (Exception e) {
}
dbCon = null;
}
}


return user;

}
}
 
Rancher
Posts: 4576
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is your sql string:


This is your PreparedStatement:


Your SQL has no parameters, yet you are trying to set 2.

Are you sure you shouldn't be using the commented out SQL string?
 
jiral dekhtawala
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya i am sure abt this i had commented previously...
 
Marshal
Posts: 25594
69
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Dave Tolls wrote:Are you sure you shouldn't be using the commented out SQL string?



Actually the commented-out SQL won't work as is -- removing the quotes around the question-marks would fix that though.
 
Dave Tolls
Rancher
Posts: 4576
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:

Dave Tolls wrote:Are you sure you shouldn't be using the commented out SQL string?



Actually the commented-out SQL won't work as is -- removing the quotes around the question-marks would fix that though.



Good eyesight!
 
jiral dekhtawala
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
//re modify my code

public class ForgetDao {

public static Connection dbCon;

public ForgetDao(Connection con) {
dbCon = con;
}

public static ForgetObj forget(ForgetObj user) {

PreparedStatement pstmt = null;

String sql ="update registration set password = ? where email = ?";
System.out.println("str sql==> "+sql);

try{
dbCon = DBConnection.getConnection();

pstmt = dbCon.prepareStatement(sql);
System.out.println("pstmt==> "+pstmt.execute(sql));

pstmt.setString(1, user.getPassword());
pstmt.setString(2, user.getEmail());

System.out.println("pstmt string ==> "+pstmt);



pstmt.executeUpdate();
System.out.println("pstm value==> "+pstmt);



} catch (Exception ex) {

System.out.println("Update failed " + ex);

}

finally {
if (pstmt != null) {
try {
pstmt.close();
} catch (Exception e) {
}
pstmt = null;
}
if (dbCon != null) {
try {
dbCon.close();
} catch (Exception e) {
}
dbCon = null;
}
}

return user;

}
}
// new is syntax error i dnt were i am wrong...
 
Dave Tolls
Rancher
Posts: 4576
47
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Why are you executing the sql here?

Also, when you get an error you should post it here rather than make us guess as to what the problem is.
 
Climb the rope! CLIMB THE ROPE! You too tiny ad:
Devious Experiments for a Truly Passive Greenhouse!
https://www.kickstarter.com/projects/paulwheaton/greenhouse-1
    Bookmark Topic Watch Topic
  • New Topic