JSP程序,谁能告诉我错在哪了,怎样改?插入一条记录并显示数据库
show.jsp代码:<%@pagecontentType="text/html;charset=gbk"%><%@pagelanguage="java"%><%@pag...
show.jsp代码:
<%@ page contentType="text/html;charset=gbk" %>
<%@ page language="java" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<body>
<center>
<%
request.setCharacterEncoding("GBK");
String id=request.getParameter("id");
String foo=request.getParameter("foo");
String bar=request.getParameter("bar");
String driver="com.mysql.jdbc.Driver"; //
String url="jdbc:mysql://localhost:3306/db?user=root&password=root";
String sql="INSERT INTO xmb VALUES('"+id+"','"+foo+"','"+bar+"')";
Connection conn=null; //数据库连接
Statement stmt=null;
ResultSet rs = null; //查询结果
%>
<%
Class.forName(driver); //加载驱动
conn=DriverManager.getConnection(url); //获得连接
stmt=conn.createStatement();
stmt.execute(sql);//存入数据库
rs=stmt.executeQuery("select * from table"); //查询所有语句
%>
<%
if(rs!=null){ //判断以下
while(rs.next()){
String id=rs.getString(1);
String foo=rs.getString(2);
String bar=rs.getString(3);
%>
<table>
<tr>
<td><%=id %></td>
<td><%=foo %></td>
<td><%=bar %></td>
</tr>
</table>
<%
rs.close();//关闭结果集
stmt.close();//关闭Statement
conn.close();//关闭数据库连接
}
}
%>
</center>
</body>
jsp页面提示错误信息如下:
“
起 因
org.apache.jasper.JasperException: 无法编译JSP
An error occurred at line: 26 in the jsp file: /show.jsp
Generated servlet error:
Duplicate local variable id
An error occurred at line: 26 in the jsp file: /show.jsp
Generated servlet error:
Duplicate local variable foo
An error occurred at line: 26 in the jsp file: /show.jsp
Generated servlet error:
Duplicate local variable bar
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:414)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:297)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:276)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:264)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:563)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:305)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
” 展开
<%@ page contentType="text/html;charset=gbk" %>
<%@ page language="java" %>
<%@ page import="java.sql.*" %>
<%@ page import="java.util.*" %>
<body>
<center>
<%
request.setCharacterEncoding("GBK");
String id=request.getParameter("id");
String foo=request.getParameter("foo");
String bar=request.getParameter("bar");
String driver="com.mysql.jdbc.Driver"; //
String url="jdbc:mysql://localhost:3306/db?user=root&password=root";
String sql="INSERT INTO xmb VALUES('"+id+"','"+foo+"','"+bar+"')";
Connection conn=null; //数据库连接
Statement stmt=null;
ResultSet rs = null; //查询结果
%>
<%
Class.forName(driver); //加载驱动
conn=DriverManager.getConnection(url); //获得连接
stmt=conn.createStatement();
stmt.execute(sql);//存入数据库
rs=stmt.executeQuery("select * from table"); //查询所有语句
%>
<%
if(rs!=null){ //判断以下
while(rs.next()){
String id=rs.getString(1);
String foo=rs.getString(2);
String bar=rs.getString(3);
%>
<table>
<tr>
<td><%=id %></td>
<td><%=foo %></td>
<td><%=bar %></td>
</tr>
</table>
<%
rs.close();//关闭结果集
stmt.close();//关闭Statement
conn.close();//关闭数据库连接
}
}
%>
</center>
</body>
jsp页面提示错误信息如下:
“
起 因
org.apache.jasper.JasperException: 无法编译JSP
An error occurred at line: 26 in the jsp file: /show.jsp
Generated servlet error:
Duplicate local variable id
An error occurred at line: 26 in the jsp file: /show.jsp
Generated servlet error:
Duplicate local variable foo
An error occurred at line: 26 in the jsp file: /show.jsp
Generated servlet error:
Duplicate local variable bar
org.apache.jasper.compiler.DefaultErrorHandler.javacError(DefaultErrorHandler.java:84)
org.apache.jasper.compiler.ErrorDispatcher.javacError(ErrorDispatcher.java:328)
org.apache.jasper.compiler.JDTCompiler.generateClass(JDTCompiler.java:414)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:297)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:276)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:264)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:563)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:305)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
” 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询