Cause: java.sql.SQLException: Parameter index out of range (5 > number of parameters, which is 4).

错误描述:

    启动项目调接口的时候控制台报错

    Cause: java.sql.SQLException: Parameter index out of range (5 > number of parameters, which is 4).

错误分析:

    可以看到控制台日志输出了下面这一句话

    ### The error occurred while setting parameters

    很好理解,注入参数的时候发生错误,那肯定就是自己写的sql有问题了。

    找了下,发现这么一句话,SUBSTR("#{today}", 1, 4),很明显这个双引号不是我想要的,改成SUBSTR(#{today}, 1, 4)之后,错误解决。

    总结一下,“大意失荆州”,sql修改不彻底,一般这种sql解析错误在mybatis的模糊查询中比较常见。

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

06-22

rnjava.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).rn at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)rn at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:2715)rn at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:3546)rn at com.achievo.test.team.Connection.ConnectionDB.insert(ConnectionDB.java:170)rn at com.achievo.test.team.DAO.FileDAO.insert(FileDAO.java:111)rn at com.achievo.test.team.po.imp.FilePOImpl.insert(FilePOImpl.java:21)rn at com.achievo.test.team.bo.imp.FileBOImpl.insert(FileBOImpl.java:21)rn at com.achievo.test.team.ui.action.ResourceUploadAction.execute(ResourceUploadAction.java:96)rn at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:419)rn at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:224)rn at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1194)rn at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:432)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)rn at com.achievo.test.team.commom.SetCharacterEncodingFilter.doFilter(SetCharacterEncodingFilter.java:39)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)rn at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)rn at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)rn at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)rn at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)rn at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)rn at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)rn at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)rn at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)rn at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)rn at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)rn at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)rn at java.lang.Thread.run(Thread.java:595)谁能帮我看下有错在哪里啊?rnConnectionDB.javarnrn public int insert(String sql ,String fileID,String typeID)rn int INSERT_RESULT=0;rn con = getConnection();rn ps = con.prepareStatement(sql); rn ps.setString(1, fileID); rn ps.setString(2, typeID);rn INSERT_RESULT = ps.executeUpdate(); rn return INSERT_RESULT;rn rnrnThe insert() was been used by this:rnfileDAO.javarnrnpublic boolean insert(FileInformation fileInformation) rn ConnectionDB connectionDB;rn int INSERT_RESULT_RETURN ,INSERT_FILE_TYPE_RETURN= 0;rn List items = new ArrayList();rn items.add(fileInformation.getFileName());rn items.add(fileInformation.getFileDescription());rn items.add(fileInformation.getFileSize());rn connectionDB = new ConnectionDB();rn try rn INSERT_RESULT_RETURN = connectionDB.insert(FILE_INFO_ADD, items);rn if (INSERT_RESULT_RETURN > 0) rn rn String fileID = selectFileID(fileInformation);rn String typeID = selectTypeID(fileInformation);rn INSERT_FILE_TYPE_RETURN=connectionDB.insert(FILE_TYPE_ADD, fileID,typeID);rn if ( INSERT_FILE_TYPE_RETURN> 0) rn this.isRSTure = true;rn rnrn rn catch (Exception e) rn e.printStackTrace();rn rnrn connectionDB.close();rn return isRSTure;rn rnrnrn

Parameter index out of range (1 > number of parameters, which is 0).求解

11-24

[code=Java]rnpackage com.asiasoft.studentsmanager;rnrnimport java.util.ArrayList;rnimport java.io.BufferedReader;rnimport java.io.IOException;rnimport java.io.InputStreamReader;rnimport java.sql.*;rnrnimport com.asiasoft.studentsmanager.utils.MyDBUtil;rnrnrnpublic class StudentsManger rn public static void main(String[] args) throws ClassNotFoundException, SQLException, IOException rn rn String uname;rn String pword;rn String sql = "";rn rn InputStreamReader input1 = new InputStreamReader(System.in);rn BufferedReader br1 = new BufferedReader(input1);rn System.out.println("****************");rn System.out.println("**欢迎登陆学员管理系统**");rn System.out.println("****************");rn Connection con1 = MyDBUtil.getConnection();rn PreparedStatement ps = MyDBUtil.getPreparedStatement(con1, sql);rn System.out.print("请输入用户名:");rn uname = br1.readLine();rn System.out.print("请输入密码:");rn pword = br1.readLine();rn ps.setString(1, uname);rn ps.setString(2, pword);rn sql = "select * from students where uname=? & pword=?";rn ResultSet rs1 = ps.executeQuery(sql);rn while(rs1.next())rn if(rs1.next() == true)rn System.out.println("登陆成功!");rn display();rn elsern System.out.println("用户名或密码错误!");rn rn rn rn rn rn rn /*display();*/rn rn rn rn public static void display() throws ClassNotFoundException, SQLException, IOException rn String operater = "";rn int id;rn String uname;rn String pword;rn String email;rn InputStreamReader input rn = new InputStreamReader(System.in);rn BufferedReader br rn = new BufferedReader(input);rn rn Connection con = MyDBUtil.getConnection();rn /*Statement st = con.createStatement();rn ResultSet rs = st.executeQuery("select * from students");*/rn String sql = "select * from students";rn Statement st = MyDBUtil.getStatement(con);rn ResultSet rs = MyDBUtil.getResultSet(st, sql);rn System.out.println("id\t|uname\t|pword\t|email\t");rn rn while(rs.next())rn System.out.println(rs.getString("id")+"\t|"rn +rs.getString("uname")+"\t|"+rn rs.getString("pword")+"\t|"+rn rs.getString("email")+"\t");rn rn System.out.println("********************");rn System.out.println("*增加学生信息 输入add; *");rn System.out.println("*删除学生信息 输入delete; *");rn System.out.println("*修改学生信息 输入update; *");rn System.out.println("*查询学生信息 输入query; *");rn System.out.println("*退出请输入exit; *");rn System.out.println("********************");rn rn rn operater = br.readLine();rn rn rn rn rn if("add".equalsIgnoreCase(operater))rn //addrn System.out.print("请输入要添加的姓名:");rn uname = br.readLine();rn rn System.out.print("请输入要添加的密码:");rn pword = br.readLine();rn rn System.out.print("请输入要添加的邮箱:");rn email = br.readLine();rn rn Student s = new Student();rn s.setUname(uname);rn s.setPword(pword);rn s.setEmail(email);rn String addsql = "insert into students (uname,pword,email) values('"+uname+"','"+pword+"','"+email+"')";rn int addresult = st.executeUpdate(addsql);rn System.out.println("添加成功!");rn display();rn rn else if("delete".equalsIgnoreCase(operater))rn //shanchurn System.out.println("请输入将要删除的编号:");rn id = Integer.parseInt(br.readLine());rn String deletesql = "delete from students where id='"+id+"'";rn int deleteResult = st.executeUpdate(deletesql);rn System.out.println("删除成功!");rn display();rn rn else if("update".equalsIgnoreCase(operater))rn //updatern System.out.println("请输入你所要修改的编号:");rn id = Integer.parseInt(br.readLine());rn System.out.println("请输入修改后的名字:");rn uname = br.readLine();rn System.out.println("请输入修改后的密码:");rn pword = br.readLine();rn System.out.println("请输入修改后的邮箱:");rn email = br.readLine();rn rn String updatesql = "update students set uname='"+uname+"',pword='"+pword+"',email='"+email+"' where id = '"+id+"'";rn int updateResult = st.executeUpdate(updatesql);rn System.out.println("修改成功!");rn display();rn else if("query".equalsIgnoreCase(operater))rn //queryrn System.out.println("请输入要查询的编号:");rn id = Integer.parseInt(br.readLine());rn rn String querysql = "select * from students where id = '"+id+"'";rn rs = st.executeQuery(querysql);rn /*while(rs.next())rn int k = Integer.parseInt(rs.getString("id"));rn if(k!= id)rn System.out.println("查无此人!");rn break;rn elsern continue;rn rn */rn System.out.println("id\t|uname\t|pword\t|email\t");rn while(rs.next())rn System.out.println(rs.getString("id")+"\t|"rn +rs.getString("uname")+"\t|"+rn rs.getString("pword")+"\t|"+rn rs.getString("email")+"\t|");rn rn System.out.println("查询完毕!");rn System.out.println("是否继续使用本系统?(y/n)");rn String j = br.readLine();rn if("y".equalsIgnoreCase(j))rn display();rn elsern System.out.println("谢谢你的使用!");rn rn else if("exit".equalsIgnoreCase(operater))rn System.out.println("谢谢你的使用!!");rn rn rn rn rn rn rn rn rn rn rnrnrn[/code]出现了如题的错误,求高手解答!下面是我的那个数据库的连接。rn[code=Java]package com.asiasoft.studentsmanager.utils;rnrnimport java.sql.Connection;rnimport java.sql.DriverManager;rnimport java.sql.PreparedStatement;rnimport java.sql.ResultSet;rnimport java.sql.SQLException;rnimport java.sql.Statement;rnrnpublic class MyDBUtil rn private static String className = "com.mysql.jdbc.Driver";rn private static String url = "jdbc:mysql://localhost/j2ee1111" +rn "?user=root&password=root";rnrn rn rn public static Connection getConnection() throws ClassNotFoundException, SQLExceptionrn Class.forName(className);rn Connection con = DriverManager.getConnection(url);rn return con;rn rn rn public static Statement getStatement(Connection con) throws SQLExceptionrn Statement st = con.createStatement();rn return st;rn rn rn public static PreparedStatement getPreparedStatement(Connection con,String sql) throws SQLExceptionrn PreparedStatement ps = con.prepareStatement(sql);rn return ps;rn rn rn public static ResultSet getResultSet(Statement st,String sql) throws SQLExceptionrn ResultSet rs = st.executeQuery(sql);rn /*int rs1 = st.executeUpdate(sql);*/rn return rs;rn rn rn rn rn public static void closeAllMethod(Connection con,Statement st,ResultSet rs)rn try rn if(rs!=null)rn rs.close();rn rn if(st!=null)rn st.close();rn rn if(con!=null)rn con.close();rn rn catch (SQLException e) rn // TODO 自动生成 catch 块rn e.printStackTrace();rn rn rnrnrn[/code]rnrn出现的问题如下:[code=Java]Exception in thread "main" java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).rn at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1055)rn at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:956)rn at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:926)rn at com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3288)rn at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3272)rn at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4108)rn at com.asiasoft.studentsmanager.StudentsManger.main(StudentsManger.java:30)[/code]

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

03-12

[code="java"]public class Register rn public static boolean register(String name, String password) rn throws SQLException, ClassNotFoundException rn Class.forName("com.mysql.jdbc.Driver");rn Connection cn = DriverManager.getConnectionrn ("jdbc:mysql://localhost/test", "root", "179401");rn PreparedStatement pst = cn.prepareStatementrn ("select * from temp");rn ResultSet rs = pst.executeQuery();rn rn while (rs.next()) rn if (rs.getString(1).equals(name)) rn System.out.println("The username " + name + " has already existed!");rn return false;rn rn rn rn pst.setString(1, name);rn pst.setString(2, password);rn rn cn.close();rn rn return true;rn rn[/code]rnrn异常 rnjava.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).rn com.mysql.jdbc.SQLError.createSQLException(SQLError.java:1075)rn com.mysql.jdbc.SQLError.createSQLException(SQLError.java:989)rn com.mysql.jdbc.SQLError.createSQLException(SQLError.java:984)rn com.mysql.jdbc.SQLError.createSQLException(SQLError.java:929)rn com.mysql.jdbc.PreparedStatement.checkBounds(PreparedStatement.java:3711)rn com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:3695)rn com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:4546)rn com.v.registration.Register.register(Register.java:22)rn com.v.registration.RegisterAction.execute(RegisterAction.java:9)rn sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)rn sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)rn sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)rn java.lang.reflect.Method.invoke(Method.java:597)rn com.opensymphony.xwork2.DefaultActionInvocation.invokeAction(DefaultActionInvocation.java:441)rn com.opensymphony.xwork2.DefaultActionInvocation.invokeActionOnly(DefaultActionInvocation.java:280)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:243)rn com.opensymphony.xwork2.interceptor.DefaultWorkflowInterceptor.doIntercept(DefaultWorkflowInterceptor.java:165)rn com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn com.opensymphony.xwork2.validator.ValidationInterceptor.doIntercept(ValidationInterceptor.java:252)rn org.apache.struts2.interceptor.validation.AnnotationValidationInterceptor.doIntercept(AnnotationValidationInterceptor.java:68)rn com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn com.opensymphony.xwork2.interceptor.ConversionErrorInterceptor.intercept(ConversionErrorInterceptor.java:122)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)rn com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn com.opensymphony.xwork2.interceptor.ParametersInterceptor.doIntercept(ParametersInterceptor.java:195)rn com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn com.opensymphony.xwork2.interceptor.StaticParametersInterceptor.intercept(StaticParametersInterceptor.java:179)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn org.apache.struts2.interceptor.MultiselectInterceptor.intercept(MultiselectInterceptor.java:75)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn org.apache.struts2.interceptor.CheckboxInterceptor.intercept(CheckboxInterceptor.java:94)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn org.apache.struts2.interceptor.FileUploadInterceptor.intercept(FileUploadInterceptor.java:235)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn com.opensymphony.xwork2.interceptor.ModelDrivenInterceptor.intercept(ModelDrivenInterceptor.java:89)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn com.opensymphony.xwork2.interceptor.ScopedModelDrivenInterceptor.intercept(ScopedModelDrivenInterceptor.java:130)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn org.apache.struts2.interceptor.debugging.DebuggingInterceptor.intercept(DebuggingInterceptor.java:267)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn com.opensymphony.xwork2.interceptor.ChainingInterceptor.intercept(ChainingInterceptor.java:126)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn com.opensymphony.xwork2.interceptor.PrepareInterceptor.doIntercept(PrepareInterceptor.java:138)rn com.opensymphony.xwork2.interceptor.MethodFilterInterceptor.intercept(MethodFilterInterceptor.java:87)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn com.opensymphony.xwork2.interceptor.I18nInterceptor.intercept(I18nInterceptor.java:165)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn org.apache.struts2.interceptor.ServletConfigInterceptor.intercept(ServletConfigInterceptor.java:164)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn com.opensymphony.xwork2.interceptor.AliasInterceptor.intercept(AliasInterceptor.java:179)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn com.opensymphony.xwork2.interceptor.ExceptionMappingInterceptor.intercept(ExceptionMappingInterceptor.java:176)rn com.opensymphony.xwork2.DefaultActionInvocation.invoke(DefaultActionInvocation.java:237)rn org.apache.struts2.impl.StrutsActionProxy.execute(StrutsActionProxy.java:52)rn org.apache.struts2.dispatcher.Dispatcher.serviceAction(Dispatcher.java:488)rn org.apache.struts2.dispatcher.ng.ExecuteOperations.executeAction(ExecuteOperations.java:77)rn org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter.doFilter(StrutsPrepareAndExecuteFilter.java:91)rnrn

Parameter index out of range 谢谢!

05-27

谢谢大家!帮我看看这个错误怎么回事,谢谢!!rnrnjava.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).rn at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:2493)rn at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:3213)rn at database.LinkDB.addBook(LinkDB.java:35)rn at action.addBookAction.execute(addBookAction.java:37)rn at org.apache.struts.action.RequestProcessor.processActionPerform(RequestProcessor.java:421)rn at org.apache.struts.action.RequestProcessor.process(RequestProcessor.java:226)rn at org.apache.struts.action.ActionServlet.process(ActionServlet.java:1164)rn at org.apache.struts.action.ActionServlet.doPost(ActionServlet.java:415)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:709)rn at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)rn at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)rn at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)rn at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)rn at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)rn at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)rn at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)rn at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)rn at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)rn at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856)rn at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.processConnection(Http11Protocol.java:744)rn at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)rn at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)rn at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)rn at java.lang.Thread.run(Thread.java:595)rn

没有更多推荐了,返回首页

私密
私密原因:
请选择设置私密原因
  • 广告
  • 抄袭
  • 版权
  • 政治
  • 色情
  • 无意义
  • 其他
其他原因:
120
出错啦
系统繁忙,请稍后再试

关闭