问题描述
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;}}异常 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) 问题补充:changchang 写道
解决方案
引用PreparedStatement pst = cn .prepareStatement("select * from temp where name=? and password=?");select的时候没有给条件参数name和password填值,所以报异常了。不过按你的逻辑来看,不需要带password,只要检查有没有同名的帐号存在就可以了。这样后面的while循环判断也可以省掉。PreparedStatement pst = cn .prepareStatement("select * from temp where name=?");pst.setString(1, user.getName());ResultSet rs = pst.executeQuery(); if(rs.next()) { return false;}
解决方案二:
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(); 10. 11. while (rs.next()) { 12. if (rs.getString(1).equals(user.getName())) { 13. System.out.println("The username " + user.getName() 14. + " has already existed!"); 15. return false; 16. } 17. } 18. 19. PreparedStatement pst2 = cn.prepareStatement 20. ("insert into temp (name, password) values(?, ?)"); 21. System.out.println(user.getName()); 22. pst2.setString(1, user.getName()); 23. pst2.setString(2, user.getPassword()); 24. pst2.executeUpdate(); 25. cn.close(); 26. 27. return true;
解决方案三:
没有执行呀pst2.executeUpdate(); 完了con.commit().
解决方案四:
只是设置好了参数,没有最终执行sql语句。设完参数后加一句就可以了。pst2.execute();
解决方案五:
最后没有执行SQL语句啊少了:pst2.executeUpdate();
解决方案六:
引用PreparedStatement pst = cn.prepareStatement ("select * from temp");这里没定义有参数的位置,后面设置参数的时候提示说没地方设参数。引用pst.setString(1, name); pst.setString(2, password);可以改成类似下面这样的PreparedStatement pst = cn.prepareStatement ("select * from temp where name = ? and password = ?");不过你的逻辑貌似不对吧。你是想先检查账号是否存在,然后再插入新的账号的吧?
解决方案七:
pst.setString(1, name); pst.setString(2, password); 这两行错了,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(); preparedstatement 是用来替换SQL里面的问号的,你上面那句"select * from temp"没有东西可以替换,所以报错了