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

问题描述

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"没有东西可以替换,所以报错了

时间: 2024-11-05 22:43:27

java.sql.SQLException: Parameter index out of range (1 > number of parameters, w的相关文章

java.sql.SQLException错误

问题描述 运行StuDialog.class出现下面错误提示,初学Java请求帮助找出下面是什么原因?java.sql.SQLException: No value specified for parameter 2at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)at com.mysql.

java.sql.SQLException: 索引中丢失 IN或OUT 参数::x

使用JDBC时,会有这么一个错误:java.sql.SQLException: 索引中丢失 IN或OUT 参数::x 如下示例中insertLog.execute();这行会抛出这个异常: String logSQL = "insert into tbl_obj(id, obj, type, cont, proposer, operator, date, remark) " + "values(seq_tot.nextval, ?, ?, ?, ?, ?, SYSDATE,

求教关于“java.sql.SQLException 列名无效”的问题

问题描述 使用DBUtil查询数据库时Java报列名'lei_xing'无效.但是这个SQL语句在MSSQL的控制台下面能够争取执行.到底是什么原因啊?这是查询部分的代码:Stringsql="selecttg.fid,tg.intime,tg.lei_bie,tg.ftitle,tg.inpart,tg.inname,jl.idfromtonggaotgleftjoin(selectid,fidfromyue_du_ji_luywherelei_xing=?)jlontg.fid=jl.fid

Caused by: java.sql.SQLException: ORA-01861: 文字与格式字符串不匹配

          项目中遇到这样一个问题:                 Caused by:java.sql.SQLException: ORA-01861: 文字与格式字符串不匹配 atoracle.jdbc.driver.DatabaseError.throwSqlException(DatabaseError.java:112) atoracle.jdbc.driver.T4CTTIoer.processError(T4CTTIoer.java:331) atoracle.jdbc.

spring-Could not obtain connection metadata java.sql.SQLException: Io exception

问题描述 Could not obtain connection metadata java.sql.SQLException: Io exception 启动tomcat的时候就报下面的错.求大神指引....... 谢谢 [INFO] - Initializing connection provider: org.springframework.orm.hibernate3.TransactionAwareDataSourceConnectionProvider 2013-07-08 16:2

hibernate proxool java.sql.SQLException

问题描述 大家好,我现在在项目中使用了hibernate来连接数据库,使用到的连接池是proxool,但奇怪的是,在tomcat的日志中,每隔一定时间就会出现以下信息:09:02:35,768 DEBUG fm_oracleproxool:431 - 009441 (00/09/00) - #7920 removed because it has problems: java.sql.SQLException: ORA-00923: 未找到要求的 FROM 关键字09:02:36,407 DEB

mysql java.sql.SQLException 问题

问题描述 mysql java.sql.SQLException 问题 java.sql.SQLException: We are already in the process of making 20 connections and the number of simultaneous builds has been throttled to 19 这是什么问题,大家帮我解决下,急. . . 解决方案 并发的线程过多,已经超过你设置的连接数,同时强调一点,设置的连接数可能会对你的系统的性能有一

数据库一添加-java.sql.SQLException: 列名无效

问题描述 java.sql.SQLException: 列名无效 表单添加的时候数据库能正常添加这一行,但是控制台上还是会报 java.sql.SQLException: 列名无效 这个错误?请问怎么回事啊~~~急 //插入方法 public void insert(HttpServletRequest request, HttpServletResponse response) throws ClassNotFoundException, SQLException{ Connection co

insert语句报java.sql.SQLException: ORA-00928

问题描述 insert语句报java.sql.SQLException: ORA-00928 java.sql.SQLException: ORA-00928: 缺失 SELECT 关键字,请问哪里出错了?? insert into app_user_ayth userId userName deptementId oainfo power depar values #userId:VARCHAR# #userName:VARCHAR# #deptementId:VARCHAR# #oainfo