投票系统,用到id的就出错
<%@page contentType="text/html;charset=gb2312"%><%@page import="java.sql.*"%>
<jsp:useBean id="java" scope="page" class="vote.DBConnect"/>
<%
String question=request.getParameter("question");
if(question==null){
question ="";
}
String type=request.getParameter("type");
int num=0,optType=0;
try{
num=Integer.parseInt(request.getParameter("num"));
}
catch(Exception e){}
%>
<html>
<head>
<title>增加主题</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<link href="vote.css" rel="stylesheet" type="text/css">
</head>
<body>
<center>
<h3>增加主题</h3>
<hr>
<a href="add.jsp">增加主题</a>||<a href="del.jsp">删除主题</a>|| <a href="display.jsp">查看主题</a>
|| <a href="modify.jsp">修改主题</a> ||<a href="index.jsp">返回首页</a>
<hr>
<form action="add.jsp" method="post">
请输入主题名:<br>
<input type="text" name="question" size="50" value="<%=question%>"><p>
<input type="checkbox" value="T" name="type"
<%
if(type!=null){
out.print("checked");
optType=1;
}
else{
optType=0;
}
%>
>多选 请输入选项的数目:
<input typ="text" name="num" size="5" value="<%=num%>">
<input type="submit" name="sumbit" value="创建选项">
<p>
<%
String submit=request.getParameter("sumbit");
if(submit!=null){
for(int i=1;i<=num;i++){
%>
选项<%=i%>:
<input type="text" name="<%=i%>" value="" size="50">
<br>
<%}%>
<input type="submit" name="submit1" value="增加主题">
<%}%>
</form>
<%
String submit1=request.getParameter("submit1"),str="",optContent,sql;
int id;
if(submit1!=null){
sql="insert into vote(flag,optType,question) values(0,"+optType+",'"+question+"')";
java.executeUpdate(sql);
sql="select * from vote order by id desc";
ResultSet rs=java.executeQuery(sql);
id=rs.getInt(1);
for(int i=1;i<=num;i++){
str=Integer.toString(i);
optContent=request.getParameter(str);
sql="insert into vote(flag,optContent,number) values("+id+",'"+optContent+"',0)";
java.executeUpdate(sql);
}
out.print("成功插入主题!");
}
%>
</center>
</body>
</html>
错误提示:The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: An exception occurred processing JSP page /add.jsp at line 66
63: java.executeUpdate(sql);
64: sql="select * from vote order by id desc";
65: ResultSet rs=java.executeQuery(sql);
66: id=rs.getInt(1);
67: for(int i=1;i<=num;i++){
68: str=Integer.toString(i);
69: optContent=request.getParameter(str);
我的数据库已经可以连接(包括查询、删除),就是增加、修改用到id就提示相同的错误。希望高手指点下,谢谢 --------------------编程问答-------------------- 什么错误。。你没有贴出来啊 --------------------编程问答-------------------- The server encountered an internal error () that prevented it from fulfilling this request
错误信息太少了 --------------------编程问答-------------------- 还能不能好好写代码了,看到这样的代码,真心,好吧,最好jsp里面别写java,那个你查数据库的时候没有用while(rs.next())不能不能保证rs非空,所以rs.getInt()空指针了吧 --------------------编程问答-------------------- The server encountered an internal error () that prevented it from fulfilling this request
服务器内在错误,不能完成这次请求,通过这个信息是没法看出你的具体问题!
sql="insert into vote(flag,optType,question) values(0,"+optType+",'"+question+"')";
java.executeUpdate(sql);
sql="select * from vote order by id desc";
ResultSet rs=java.executeQuery(sql);
id=rs.getInt(1);
for(int i=1;i<=num;i++){
str=Integer.toString(i);
optContent=request.getParameter(str);
sql="insert into vote(flag,optContent,number) values("+id+",'"+optContent+"',0)";
标红的几处:
1:number字段你数据库类型是number? '"+question+"' 这样是字符串写法了,这样会存在类型转换异常了
2:executeUpdate这个方法是即实现了增,也实现了改? 如果是命名时建议改成executeAddOrUpdate --------------------编程问答-------------------- number的数据库类型是int;请问 '"+question+"' 的类型转换异常要怎么处理
补充:Java , Web 开发