一开始我先单独做了个jsp连了一下,正常的连上了,并且从表中都把数据读出来了。
然后想做个javabean来处理连接oracle数据库的问题,照着例子写了一个,出了错误,我是个jsp新手,不太明白错在哪里了,希望高手指点一下。---------------------------------------------------------------
javabean代码
// Java Document
package test;
import java.sql.*;
public class ContactBean
{
 private Connection con;
 //初始化连接。
 public ContactBean()
 {
        
  try
  {
   Class.forName("oracle.jdbc.driver.OracleDriver").newInstance();
   String url="jdbc:oracle:thin:@localhost:1521:DBcredit"; 
         String user="system"; 
         String password="manager"; 
         con= DriverManager.getConnection(url,user,password); 
  }
  catch (InstantiationException e)
  {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 
  catch (IllegalAccessException e)
  {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }
 
  catch(ClassNotFoundException e)
   {
    System.out.println("DRIVER NOT FOUND!");
    con=null;
   }
  catch(SQLException e)
   {
    con=null;
   }
    
 }
 //查询数据库
 public  ResultSet getContact(String id)
 {
  try
  {
   Statement stm=con.createStatement();
   ResultSet result=stm.executeQuery("select * from TBL_TEST1 where id='"+id+"'");
   return result;
  }
  catch(Exception e)
  {
  }
  return null;
 }
}  
-----------------------------------------
jsp页面代码
<%@ page contentType="text/html; charset=gb2312" language="java" import="java.sql.*" 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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>
<jsp:useBean id="oc" scope="page" class="test.ContactBean" />
<%
String nid="001";
ResultSet rs=oc.getContact(nid);
%>
<%=rs.getString(1)%>
<%=rs.getString(2)%><body>
</body>
</html>
------------------------------------------
报错信息
type Exception reportmessage description The server encountered an internal error () that prevented it from fulfilling this request.exception org.apache.jasper.JasperException: /c.jsp(8,0) The value for the useBean class attribute test.ContactBean is invalid.
org.apache.jasper.servlet.JspServletWrapper.handleJspException(JspServletWrapper.java:510)
org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:375)
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)
root cause org.apache.jasper.JasperException: /c.jsp(8,0) The value for the useBean class attribute test.ContactBean is invalid.
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:1174)
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:3304)
org.apache.jasper.compiler.Compiler.generateJava(Compiler.java:198)
org.apache.jasper.compiler.Compiler.compile(Compiler.java:295)
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:303)
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)
note The full stack trace of the root cause is available in the Apache Tomcat/5.5.16 logs.
------------------------------------------------------
由于一开始的单独的jsp好使,所以不是环境和jdbc驱动的问题,ContactBean.class已经放在WEB-INF\classes\test文件夹里了,应该也不是路径的问题,错误说test.ContactBean is invalid怎么回事呢???