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

bnna8356586 2015-09-08 08:10:22
本人是个菜鸟,想问下,我想在HTML中添加一条记录到数据库t_emp中,然后出现这个错误,这个参数出错在哪?百度弄不出。怎么改才行呢?
package web;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class AddEmpServlet extends HttpServlet {
public void service(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
request.setCharacterEncoding("utf-8");
response.setContentType("text/html;charset=utf-8");//设置内容输出类型
PrintWriter out = response.getWriter();//获取输出流对象
//以下三行代码功能是获取HTML表单提交的数据
String name =request.getParameter("name");
double salary=Double.valueOf(request.getParameter("salary"));
int age=Integer.valueOf(request.getParameter("age"));
Connection conn=null;
PreparedStatement stat=null;
try{
Class.forName("com.mysql.jdbc.Driver");
conn=DriverManager.getConnection("jdbc:mysql://localhost:3306/t_emp","root","admin");
stat=conn.prepareStatement("insert into t_emp values (name,salary,age) ;");
stat.setString(1, name);
stat.setDouble(2, salary);
stat.setInt(3, age);
stat.executeUpdate();
out.println("添加成功");
}catch(Exception e){
e.printStackTrace();
out.println("系统繁忙,请稍后再试");
}finally{
if(stat!=null){
try{stat.close();
}catch(SQLException e){
e.printStackTrace();
}
}
if(conn!=null){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
}
}
java.sql.SQLException: Parameter index out of range (1 > number of parameters, which is 0).

at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:910)
at com.mysql.jdbc.PreparedStatement.setInternal(PreparedStatement.java:2715)
at com.mysql.jdbc.PreparedStatement.setString(PreparedStatement.java:3546)
at web.AddEmpServlet.service(AddEmpServlet.java:42)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:727)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:303)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)

at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:241)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:208)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:220)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:122)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:504)

at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:170)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103)
at org.apache.catalina.valves.AccessLogValve.invoke(AccessLogValve.java:950)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:116)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:421)
at org.apache.coyote.http11.AbstractHttp11Processor.process(AbstractHttp11Processor.java:1074)
at org.apache.coyote.AbstractProtocol$AbstractConnectionHandler.process(AbstractProtocol.java:611)
at org.apache.tomcat.util.net.JIoEndpoint$SocketProcessor.run(JIoEndpoint.java:316)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)

...全文
4195 点赞 收藏 7
写回复
7 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
简筱诺 2019-04-26
我也有同样的问题,是关于更新数据的,我的主键也是自动生成的,这个要如何处理才可以成功修改数据呢?下面是我的代码,麻烦哪个大神帮忙看看,谢谢~ package com.ysl.servlet; import java.io.IOException; import java.io.PrintWriter; import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import javax.servlet.ServletException; import javax.servlet.annotation.WebServlet; import javax.servlet.http.HttpServlet; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpSession; /** * Servlet implementation class UpdateServlet */ @WebServlet("/UpdateServlet") public class UpdateServlet extends HttpServlet { private static final long serialVersionUID = 1L; protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { response.setContentType("text/html;charset=utf-8"); PrintWriter out = response.getWriter(); HttpSession session = request.getSession(); Integer userId = (Integer) session.getAttribute("userId");//从session获得userId //获得用户信息 String userName = request.getParameter("userName"); String userPwd = request.getParameter("userPwd"); String userEmail = request.getParameter("userEmail"); String userTel = request.getParameter("userTel"); Connection con = null; PreparedStatement pstmt = null; try { Class.forName("com.mysql.jdbc.Driver"); //加载驱动程序 con = DriverManager.getConnection("jdbc:mysql://localhost:3306/tests?useUnicode =true&characterEncoding = gbk", "root", ""); //连接数据库 String sql = "update user set userName= ? , userPwd= ? , userEmail= ? , userTel= ? where userId= ? "; pstmt = con.prepareStatement(sql); //建立预处理对象 pstmt.setString(1, userName); //设置参数值,用户号为2。 pstmt.setString(2, userPwd); //设置参数值,用户号为2。 pstmt.setString(3, userEmail); pstmt.setString(4, userTel); pstmt.setInt(5, userId); int rs = pstmt.executeUpdate(); } catch (Exception e) { e.printStackTrace(); } finally { try { pstmt.close(); } catch (Exception e) { } try { con.close(); } catch (Exception e) { } } } /** * @see HttpServlet#HttpServlet() */ public UpdateServlet() { super(); // TODO Auto-generated constructor stub } /** * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) */ protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //response.getWriter().append("Served at: ").append(request.getContextPath()); processRequest(request, response); } /** * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) */ protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { // TODO Auto-generated method stub //doGet(request, response); processRequest(request, response); } }
回复
mx5721 2015-09-17
你要考虑到你的主键是否自动生成,否则需要指明主键值。
回复
bnna8356586 2015-09-12
我找到错误的原因了,不好意思来晚了,我也知道是越界了,但就是不知道如何改,我随意改了,就行了。把这句话"insert into t_emp values (name,salary,age) ;"修改成"insert into t_emp values (id,?,?,?) ;"
回复
解开者 2015-09-09
楼上正解,此外楼主确定你的数据库名和表名是一模一样的吗?
回复
ITjavaman 2015-09-09
SQL语句改成"insert into t_emp (name,salary,age) values(?,?,?) "
回复
业余草 2015-09-09
java代码放到html5不合适,我给你移动 了!
回复
sinat_30977573 2015-09-08
要不就是你数据库中表的列数不对,要不就是你插入的位置越界。个人建议插入位置最好用对应的属性名。
回复
相关推荐
发帖
Web 开发
创建于2007-09-28

8.0w+

社区成员

Java Web 开发
申请成为版主
帖子事件
创建了帖子
2015-09-08 08:10
社区公告
暂无公告