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

a5389303 2012-05-22 12:41:19
我用mysql+tomcat 以下是我的JSP页面:
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*,java.util.*,java.text.*" errorPage="" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>
<%
request.setCharacterEncoding("gb2312");

String stridcard=request.getParameter("idcard");
String strname=request.getParameter("name");
String strroomnub=request.getParameter("roomnub");
int roomnub=0;
String strreson=request.getParameter("reson");
String strriqi=request.getParameter("riqi");
java.util.Date riqi;
String strvisit=request.getParameter("visit");
java.util.Date visit;
String strlefttime=request.getParameter("lefttime");
java.util.Date lefttime;




roomnub=Integer.parseInt(strroomnub);

SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
riqi=sdf.parse(strriqi);
java.sql.Date riqie=new java.sql.Date(riqi.getTime());


SimpleDateFormat abc=new SimpleDateFormat("HH:MM:SS");
visit=abc.parse(strvisit);
java.sql.Time visite=new java.sql.Time(visit.getTime());

SimpleDateFormat abcd=new SimpleDateFormat("HH:MM:SS");
lefttime=abcd.parse(strlefttime);
java.sql.Time lefttimee=new java.sql.Time(lefttime.getTime());




String dbDriver="org.gjt.mm.mysql.Driver";
String dbUrl="jdbc:mysql://localhost:3306/mldn";
String dbUser="root";
String dbPass="abc";
String sql="update people set idcard='"+stridcard+"',name='"+strname+"',roomnub='"+roomnub+"',reson='"+strreson+"',riqi='"+riqie+"',visit='"+visite+"',lefttime='"+lefttimee+"' where idcard='"+stridcard+"';";
Connection conn=null;
PreparedStatement pstmt=null;



try{
Class.forName(dbDriver);
conn=DriverManager.getConnection(dbUrl,dbUser,dbPass);
pstmt=conn.prepareStatement(sql);
request.setCharacterEncoding("gb2312");

pstmt.setString(1,stridcard);
pstmt.setString(2,strname);
pstmt.setInt(3,roomnub);
pstmt.setString(4,strreson);
pstmt.setDate(5,riqie);
pstmt.setTime(6,visite);
pstmt.setTime(7,lefttimee);


int result=pstmt.executeUpdate();
if(result>0){
response.sendRedirect("update_ok.html");
}else{
response.sendRedirect("update_faild.html");
}
conn.close() ;
pstmt.close();
}catch(Exception e){
out.println(e);
}

%>
</body>
</html>
求救吖!!~
...全文
352 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
ACMAIN_CHM 2012-05-22
  • 打赏
  • 举报
回复
String sql="update people set idcard='"+stridcard+"',name='"+strname+"',roomnub='"+roomnub+"',reson='"+strreson+"',riqi='"+riqie+"',visit='"+visite+"',lefttime='"+lefttimee+"' where idcard='"+stridcard+"';";
response.write sql


看看你的SQL语句到底是什么?
a5389303 2012-05-22
  • 打赏
  • 举报
回复
嗯嗯,我也有仔细看过了,但看不出来哪了,求解~
yangxiao_jiang 2012-05-22
  • 打赏
  • 举报
回复
这个错误就是参数的数量不对。
yangxiao_jiang 2012-05-22
  • 打赏
  • 举报
回复
你的sql里,也不带?为什么要用pstmt.setString(1,stridcard);这种形式啊?

56,677

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧