主题:问个很菜的问题!
刚刚学习JSP不久 在javabean 这一块出问题很久了!
我的步骤如下:
用Access建立一个名叫student的一个数据库
创建1个表:table
配制的ODBC数据源:
Conn.java代码如下:
package student;
import java.sql.*;
import java.util.*;
import java.io.*;
public class conn {
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "jdbc:odbc:student";
Connection connect = null;
ResultSet rs = null;
public conn() {
try {
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e) {
System.err.println(e.getMessage());
}
}
public ResultSet executeQuery(String sql) {
try {
connect = DriverManager.getConnection(sConnStr);
Statement stmt = connect.createStatement();
rs = stmt.executeQuery(sql);
}
catch(SQLException ex) {
System.err.println(ex.getMessage());
}
return rs;
}
}
把编译好的conn.class 放在WEB-INF\classes的根目录下
conn_db.jsp代码如下:
<%@ page contentType="Text/html;charset=GBK" %>
<%@ page language="java" import="java.sql.*" %>
<jsp:useBean id="connDbBean" scope="page" class="student.conn"/>
<html>
<head>
<title>无标题文档</title>
</head>
<body>
<table width="60%" border="1">
<tr>
<td width="30%">姓名</td>
<td width="15%">性别</td>
<td width="15%">成绩</td>
</tr>
<%
ResultSet RS_result = connDbBean.executeQuery("select * from table");
String name="";
String sex="';
int score=0;
while(RS_result.next())
{
name = RS_result.getString("name");
sex = RS_result.getString("sex");
score = RS_result.getInt("score");
%>
<tr>
<td><%=name%></td>
<td><%=sex%></td>
<td><%=score%></td>
</tr>
<%
}
RS_result.close();
%>
</table>
</body>
</html>
运行conn_db.jsp文件出现错误:
HTTP Status 500 -
________________________________________
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: /conn_db.jsp(2,0) [color=FF0000]The value for the useBean class attribute student.conn is invalid.[/color] org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:39)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:405)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:146)
org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1223)
org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1116)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2163)
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2213)
org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2219)
org.apache.jasper.compiler.Node$Root.accept(Node.java:456)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2163)
org.apache.jasper.compiler.Generator.generate(Generator.java:3270)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:189)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:556)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:293)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.9 logs.
________________________________________
Apache Tomcat/5.5.9
我的步骤如下:
用Access建立一个名叫student的一个数据库
创建1个表:table
配制的ODBC数据源:
Conn.java代码如下:
package student;
import java.sql.*;
import java.util.*;
import java.io.*;
public class conn {
String sDBDriver = "sun.jdbc.odbc.JdbcOdbcDriver";
String sConnStr = "jdbc:odbc:student";
Connection connect = null;
ResultSet rs = null;
public conn() {
try {
Class.forName(sDBDriver);
}
catch(java.lang.ClassNotFoundException e) {
System.err.println(e.getMessage());
}
}
public ResultSet executeQuery(String sql) {
try {
connect = DriverManager.getConnection(sConnStr);
Statement stmt = connect.createStatement();
rs = stmt.executeQuery(sql);
}
catch(SQLException ex) {
System.err.println(ex.getMessage());
}
return rs;
}
}
把编译好的conn.class 放在WEB-INF\classes的根目录下
conn_db.jsp代码如下:
<%@ page contentType="Text/html;charset=GBK" %>
<%@ page language="java" import="java.sql.*" %>
<jsp:useBean id="connDbBean" scope="page" class="student.conn"/>
<html>
<head>
<title>无标题文档</title>
</head>
<body>
<table width="60%" border="1">
<tr>
<td width="30%">姓名</td>
<td width="15%">性别</td>
<td width="15%">成绩</td>
</tr>
<%
ResultSet RS_result = connDbBean.executeQuery("select * from table");
String name="";
String sex="';
int score=0;
while(RS_result.next())
{
name = RS_result.getString("name");
sex = RS_result.getString("sex");
score = RS_result.getInt("score");
%>
<tr>
<td><%=name%></td>
<td><%=sex%></td>
<td><%=score%></td>
</tr>
<%
}
RS_result.close();
%>
</table>
</body>
</html>
运行conn_db.jsp文件出现错误:
HTTP Status 500 -
________________________________________
type Exception report
message
description The server encountered an internal error () that prevented it from fulfilling this request.
exception
org.apache.jasper.JasperException: /conn_db.jsp(2,0) [color=FF0000]The value for the useBean class attribute student.conn is invalid.[/color] org.apache.jasper.compiler.DefaultErrorHandler.jspError(DefaultErrorHandler.java:39)
org.apache.jasper.compiler.ErrorDispatcher.dispatch(ErrorDispatcher.java:405)
org.apache.jasper.compiler.ErrorDispatcher.jspError(ErrorDispatcher.java:146)
org.apache.jasper.compiler.Generator$GenerateVisitor.visit(Generator.java:1223)
org.apache.jasper.compiler.Node$UseBean.accept(Node.java:1116)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2163)
org.apache.jasper.compiler.Node$Visitor.visitBody(Node.java:2213)
org.apache.jasper.compiler.Node$Visitor.visit(Node.java:2219)
org.apache.jasper.compiler.Node$Root.accept(Node.java:456)
org.apache.jasper.compiler.Node$Nodes.visit(Node.java:2163)
org.apache.jasper.compiler.Generator.generate(Generator.java:3270)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:189)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:286)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:267)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:255)
org.apache.jasper.JspCompilationContext.compile(JspCompilationContext.java:556)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:293)
org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:291)
org.apache.jasper.servlet.JspServlet.service(JspServlet.java:241)
javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.9 logs.
________________________________________
Apache Tomcat/5.5.9