iteye_5102
iteye_5102
2011-03-12 14:39

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

已采纳

[code="java"]public class Register {
public static boolean register(String name, String password)
throws SQLException, ClassNotFoundException {
Class.forName("com.mysql.jdbc.Driver");
Connection cn = DriverManager.getConnection
("jdbc:mysql://localhost/test", "root", "179401");
PreparedStatement pst = cn.prepareStatement
("select * from temp");
ResultSet rs = pst.executeQuery();

    while (rs.next()) {
        if (rs.getString(1).equals(name)) {
            System.out.println("The username " + name + " has already existed!");
            return false;
        }
    }

    pst.setString(1, name);
    pst.setString(2, password);

    cn.close();

    return true;
}

}[/code]

异常
java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)
com.mysql.jdbc.SQLError.createSQLException(SQLError.java:929)
com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3711)
com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3695)
com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4546)
com.v.registration.Register.register(Register.java:22)
com.v.registration.RegisterAction.execute(RegisterAction.java:9)
sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
java.lang.reflect.Method.invoke(Method.java:597)
com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)
com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)
com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)
org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)
com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)
com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)
org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)
org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)
org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

7条回答

  • changchanghust changchanghust 10年前

    [quote]PreparedStatement pst = cn

    .prepareStatement("select * from temp where name=? and password=?");[/quote]

    select的时候没有给条件参数name和password填值,所以报异常了。

    不过按你的逻辑来看,不需要带password,只要检查有没有同名的帐号存在就可以了。这样后面的while循环判断也可以省掉。

    [code="java"]PreparedStatement pst = cn

    .prepareStatement("select * from temp where name=?");

    pst.setString(1, user.getName());
    ResultSet rs = pst.executeQuery();
    if(rs.next()) {
    return false;
    }[/code]

    点赞 评论 复制链接分享
  • redstarofsleep redstarofsleep 10年前

    [code="java"]

    1. public class Register {
    2. public static boolean register(User user) throws SQLException,
    3. ClassNotFoundException {
    4. Class.forName("com.mysql.jdbc.Driver");
    5. Connection cn = DriverManager.getConnection(
    6. "jdbc:mysql://localhost/test", "root", "179401");
    7. PreparedStatement pst = cn
    8. .prepareStatement("select * from temp");
    9. ResultSet rs = pst.executeQuery();

      1. while (rs.next()) {
      2. if (rs.getString(1).equals(user.getName())) {
      3. System.out.println("The username " + user.getName()
      4. + " has already existed!");
      5. return false;
      6. }
      7. }

      8. PreparedStatement pst2 = cn.prepareStatement
      9. ("insert into temp (name, password) values(?, ?)");
      10. System.out.println(user.getName());
      11. pst2.setString(1, user.getName());
      12. pst2.setString(2, user.getPassword());
      13. pst2.executeUpdate();
      14. cn.close();

      15. return true;
        [/code]
    点赞 评论 复制链接分享
  • enet_java enet_java 10年前

    没有执行呀
    pst2.executeUpdate();

    完了con.commit().

    点赞 评论 复制链接分享
  • changchanghust changchanghust 10年前

    只是设置好了参数,没有最终执行sql语句。设完参数后加一句就可以了。
    [code="java"]pst2.execute();[/code]

    点赞 评论 复制链接分享
  • redstarofsleep redstarofsleep 10年前

    最后没有执行SQL语句啊
    少了:
    [code="java"]pst2.executeUpdate();[/code]

    点赞 评论 复制链接分享
  • changchanghust changchanghust 10年前

    [quote]PreparedStatement pst = cn.prepareStatement

    ("select * from temp");[/quote]

    这里没定义有参数的位置,后面设置参数的时候提示说没地方设参数。
    [quote]pst.setString(1, name);

    pst.setString(2, password);[/quote]

    可以改成类似下面这样的
    [code="java"]PreparedStatement pst = cn.prepareStatement

    ("select * from temp where name = ? and password = ?");[/code]

    不过你的逻辑貌似不对吧。你是想先检查账号是否存在,然后再插入新的账号的吧?

    点赞 评论 复制链接分享
  • redstarofsleep redstarofsleep 10年前

    pst.setString(1, name);

    pst.setString(2, password);
    这两行错了,

    [code="java"]
    stringsql = "select * from people p where p.id = ? and p.name = ?";
    preparedstatement ps = connection.preparestatement(sql);
    ps.setint(1,id);
    ps.setstring(2,name);
    resultset rs = ps.executequery();
    [/code]

    preparedstatement 是用来替换SQL里面的问号的,你上面那句"select * from temp"没有东西可以替换,所以报错了

    点赞 评论 复制链接分享

相关推荐