MySQL报错解决--Parameter index out of range (1 > number of parameters, which is 0)

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/stalin_/article/details/80499496

今天在操作Mysql进行删除操作时,出现了这么一个错误。

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

结合网上各位大佬给出的解答,终于将其解决。

问题出在手写的sql出错。

这个报错的含义指的是第一个参数的问题,也就是说问题出在?左右。

这是我写的sql:

String sql = "DELETE FROM T_PUB_NODE_INFO WHERE C_IP =?";

看起来是不是没有错误。。。

原来是我的问号是中文输入法下的?,不是英文输入下的?。改了之后就好了。

另外网上也有其他错误情况。

例如这样写:

String sql = "DELETE FROM T_PUB_NODE_INFO WHERE C_IP ='?'";

还有在MyBatis下报这个错误可能是你 like 语句写错了。

在mybatis里面写就是应该是 like  '%${name} %' 而不是 '%#{name} %'  。

${name} 是不带单引号的,而#{name} 是带单引号的。



展开阅读全文

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 问答

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