数据库异常:java.sql.SQLException: No value specified for parameter 1

搜索后发现是:执行executeQuery()先于对sql语句中的占位符赋值,从逻辑上面还未将值传给占位符,导致No value specified for parameter 1,(占位符1未得到参数)

源码

public boolean register(String name, String affirmpwd){
// TODO Auto-generated method stub
String sql = “INSERT INTO users (user_name,user_pwd) VALUES (?,?)”;
Connection con = null;
PreparedStatement psta = null;
int rs = 0;
boolean bl = true;
try {
con = C3P0Utils.getConn();
psta = con.prepareStatement(sql);
rs = psta.executeUpdate();
psta.setString(1, name);
psta.setString(2, affirmpwd);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (rs > 0) {
System.out.println(“添加成功!”);
return bl;
}
else {
return false;
}
}

更改

public boolean register(String name, String affirmpwd){
// TODO Auto-generated method stub
String sql = “INSERT INTO users (user_name,user_pwd) VALUES (?,?)”;
Connection con = null;
PreparedStatement psta = null;
int rs = 0;
boolean bl = true;
try {
con = C3P0Utils.getConn();
psta = con.prepareStatement(sql);
psta.setString(1, name);
psta.setString(2, affirmpwd);
rs = psta.executeUpdate();
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
if (rs > 0) {
System.out.println(“添加成功!”);
return bl;
}
else {
return false;
}
}

你可能感兴趣的:(java数据库异常)