Jsp+JavaBean+MySql实现登陆及增删改查出错。
Jsp+JavaBean+MySql实现登陆及增删改查出错。
运行环境:Tomcat6.0+jdk1.6+MyEclipse6.0
报错信息:
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: /ADMS/WebRoot/add.jsp(2,0) The value for the useBean class attribute bean.SqlConn is invalid.
org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:40)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:407)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:148)
org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1200)
org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1155)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2336)
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2386)
org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2392)
org.apache.jasper.compiler.Node$Root.accept(Node.java:489)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2336)
org.apache.jasper.compiler.Generator.generate(Generator.java:3394)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:210)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:306)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:273)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:566)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:308)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:320)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:266)
javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
note The full stack trace of the root cause is available in the Apache Tomcat/6.0.10 logs.
我的代码
1.SqlConn.java
package bean;
import java.sql.*;
public class SqlConn {
private String url = "jdbc:mysql://localhost:3036/adms";
private String user = "root";
private String password = "root";
private Connection conn = null;
private Statement stmt = null;
private ResultSet rs = null;
public SqlConn(){
try{
Class.forName("com.mysql.jdbc.Driver");
}
catch(java.lang.ClassNotFoundException e){
System.err.println("SqlConn():"+e.getMessage());
}
}
public ResultSet executeQuery(String sql){
try{
conn = DriverManager.getConnection(url,user,password);
stmt = conn.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
rs = stmt.executeQuery(sql);
}catch(SQLException ex){
System.err.println("SqlConn.executeQuery:"+ex.getMessage());
}
return rs;
}
public void executeUpdate(String sql){
try{
conn = DriverManager.getConnection(url,user,password);
stmt = conn.createStatement();
stmt.executeUpdate(sql);
stmt.close();
conn.close();
}catch(SQLException ex){
System.err.println("SqlConn.executeUpdate:"+ex.getMessage());
}
}
public void closeStmt(){
try{
stmt.close();
}catch(SQLException e){
e.printStackTrace();
}
}
public void closeConn(){
try{
conn.close();
}catch(SQLException e){
e.printStackTrace();
}
}
public static void main(String[] args) {
// TODO Auto-generated method stub
}
}
2.login.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%--使用SqlConn.class--%>
<jsp:useBean id="sqlbean" scope="page" class="bean.SqlConn"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'login.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<form name="form1" action="loginto.jsp" method="post">
<table width="225" border="1" align="center">
<tbody>
<tr>
<td>用户名</td>
<td><input type="text" name="username"></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td colspan="2" align="center">
<input type="radio" checked="checked" value="1" name="role">用户
<input type="radio" value="0" name="role">管理员
</td>
</tr>
<tr>
<td> </td>
<td><input type="submit" value="登陆" name="login">
<input type="reset" value="重置" name="reset">
</td>
</tr>
</tbody>
</table>
</form>
</body>
</html>
3.loginto.jsp
<%@ page language="java" import="java.sql.*" pageEncoding="UTF-8"%>
<jsp:useBean id="sqlbean" scope="page" class="bean.SqlConn"/>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<title>My JSP 'loginto.jsp' starting page</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
<!--
<link rel="stylesheet" type="text/css" href="styles.css">
-->
</head>
<body>
<%
String username = request.getParameter("username").trim();
String password = request.getParameter("password").trim();
String role = request.getParameter("role").trim();
try{
if(role.equals("0"))
{
String sql = "select * from admin where admin_id='"+username+"'and admin_psw='"+password+"'";
ResultSet rs = sqlbean.executeQuery(sql);
if(rs.next())
response.sendRedirect("adms.jsp");
else
out.println("错误的用户名或密码!");
}
else if(role.equals("1"))
{
String sql = "select * from user where user_id='"+username+"'and user_psw='"+password+"'";
ResultSet rs = sqlbean.executeQuery(sql);
if(rs.next())
out.println("您好,"+username);
else
out.println("错误的用户名或密码!");
}
}catch(Exception ee){
ee.printStackTrace();
}
%>
</body>
</html>
还有添加,删除,修改,查找等jsp页面。
我用JSP页面直接连接就能运行,没有错。只要是有JavaBean,就会报错。希望大家多多指教。
这个问题出现好久了,不仅这一个项目有这样的问题,所有的导入和新建的web project 只要有JavaBean都会有这样的错误。
搞不清是什么原因。数据库是SQLServer2000的时候也是这样的错误。
[解决办法]
这个问题太简单了。
qq:369050897
具体Qq给你说。
我的异常网推荐解决方案:The server encountered an internal error () that prevented it from fulfilling this request.,http://www.myexception.cn/java-web/317.html