CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Java >  Web 开发

javabean访问数据库出错

楼主wangbd8(wangbd8)2003-01-04 19:56:28 在 Java / Web 开发 提问

我的应用环境为:tomcat4.12+mysql+mm.mysql.jdbc+poolman2  
  在jsp中调用javaBean总是出错,请帮忙看看错在哪里?  
  另外:如果将javabean中open()方法的catch(Exception   e)改为catch(SQLException   e),编译时就出错,为什么?  
   
   
  错误信息  
  =================================  
  org.apache.jasper.JasperException:   Unable   to   compile   class   for   JSP  
   
  An   error   occurred   at   line:   7   in   the   jsp   file:   /testpoolread.jsp  
   
  Generated   servlet   error:  
          [javac]   Compiling   1   source   file  
   
  E:\Program   Files\Apache   Group\Tomcat   4.1\work\Standalone\localhost\_\testpoolread_jsp.java:44:   cannot   resolve   symbol  
  symbol     :   class   Mysql      
  location:   class   org.apache.jsp.testpoolread_jsp  
              Mysql   tia   =   null;  
              ^  
   
   
   
  An   error   occurred   at   line:   7   in   the   jsp   file:   /testpoolread.jsp  
   
  Generated   servlet   error:  
  E:\Program   Files\Apache   Group\Tomcat   4.1\work\Standalone\localhost\_\testpoolread_jsp.java:46:   cannot   resolve   symbol  
  symbol     :   class   Mysql      
  location:   class   org.apache.jsp.testpoolread_jsp  
                  tia   =   (Mysql)   pageContext.getAttribute("tia",   PageContext.PAGE_SCOPE);  
                                ^  
   
   
   
  An   error   occurred   at   line:   7   in   the   jsp   file:   /testpoolread.jsp  
   
  Generated   servlet   error:  
  E:\Program   Files\Apache   Group\Tomcat   4.1\work\Standalone\localhost\_\testpoolread_jsp.java:49:   cannot   resolve   symbol  
  symbol     :   class   Mysql      
  location:   class   org.apache.jsp.testpoolread_jsp  
                          tia   =   (Mysql)   java.beans.Beans.instantiate(this.getClass().getClassLoader(),   "Mysql");  
                                        ^  
   
   
   
  An   error   occurred   at   line:   9   in   the   jsp   file:   /testpoolread.jsp  
   
  Generated   servlet   error:  
  E:\Program   Files\Apache   Group\Tomcat   4.1\work\Standalone\localhost\_\testpoolread_jsp.java:60:   cannot   resolve   symbol  
  symbol     :   class   ResultSet      
  location:   class   org.apache.jsp.testpoolread_jsp  
  ResultSet   rs   =   null;    
  ^  
  4   errors  
   
   
  javaBEAN  
  =============================================  
  import   java.sql.*;    
  import   java.io.*;  
  import   javax.sql.*;  
  import   javax.naming.*;  
     
  /***    
  *   处理数据库的连接和访问    
  *   @version   1.01    
  ***/    
   
  public   class   mysql  
  {    
  Connection   conn;    
  Statement   stmt;    
  PreparedStatement   prepstmt;    
   
  /***  
  *构造方法  
  ***/  
  public   mysql()  
  {  
  conn   =   null;  
  stmt   =   null;  
  prepstmt   =   null;  
  }  
   
  /***    
  *功能:   连接数据库,创建Statement对象    
  *返回类型:int  
  *返回值:0:成功;-1:连接失败,无法建立Connection对象;-2:其他错误  
  ***/    
  public   int   Open()  
  {    
  int   f=0;  
  try  
  {  
  Class.forName("com.codestudio.sql.PoolMan").newInstance();  
  conn   =   DriverManager.getConnection("jdbc:poolman://tia");    
  stmt   =   conn.createStatement();    
  }  
  catch(Exception   e)  
  {  
  if   (conn==null   ||   stmt==null) f=-1;  
  else f=-2;  
  this.close();  
  }  
  return   f;  
  }  
   
  /***    
  *功能:   连接数据库,创建PrepareStatement对象  
  *参数:String   sql:预置的SQL语句  
  *返回类型:int  
  *返回值:0:成功;-1:连接失败,无法建立Connection对象;-2:其他错误  
  ***/    
  public   int   PreOpen(String   sql)  
  {    
  int   f=0;  
  try  
  {  
  Class.forName("com.codestudio.sql.PoolMan").newInstance();  
  conn   =   DriverManager.getConnection("jdbc:poolman://tia");    
  this.setPreStmt(sql);  
  }  
  catch(Exception   e)  
  {  
  if   (conn==null   ||   prepstmt==null) f=-1;  
  else f=-2;  
  this.close();  
  }  
  return   f;    
  }    
   
  /***    
  *功能:设置PreparedStatement对象的SQL语句  
  *参数:String   sql:预置的SQL语句  
  ***/    
  public   void   setPreStmt(String   sql)   throws   SQLException  
  {    
  prepstmt   =   conn.prepareStatement(sql);    
  }  
   
  /***  
  *功能:清除PreparedStatement对象的SQL语句,  
  *   与设置PreparedStatement对象SQL语句的方法(setPreStmt)配合,  
  *   实现PreparedStatement对象的重复使用  
  ***/  
  public   void   clearParameters()   throws   SQLException    
  {    
  prepstmt.clearParameters();    
  }    
   
  /***  
  *   功能:返回连接    
  *   返回值:Connection   连接    
  ***/    
  public   Connection   getConnection()  
  {    
  return   conn;    
  }  
   
  /***    
  *   返回PreparedStatement对象    
  ***/    
  public   PreparedStatement   getPreparedStatement()  
  {    
  return   prepstmt;    
  }  
   
  /***    
  *   返回Statement对象    
  ***/    
  public   Statement   getStatement()  
  {    
  return   stmt;    
  }  
   
  /***  
  *   设置PreparedStatement对象的参数值    
  *   @param   index   参数序号,从1开始    
  *   @param   value   对应值    
  ***/    
   
  public   void   setString(int   index,String   value)   throws   SQLException  
  {    
  prepstmt.setString(index,value);    
  }  
   
  public   void   setInt(int   index,int   value)   throws   SQLException  
  {    
  prepstmt.setInt(index,value);    
  }  
   
  public   void   setDate(int   index,Date   value)   throws   SQLException  
  {    
  prepstmt.setDate(index,value);    
  }  
   
  public   void   setLong(int   index,long   value)   throws   SQLException  
  {    
  prepstmt.setLong(index,value);    
  }  
   
  public   void   setFloat(int   index,float   value)   throws   SQLException  
  {    
  prepstmt.setFloat(index,value);    
  }  
   
  //File   file   =   new   File("test/data.txt");    
  //int   fileLength   =   file.length();    
  //InputStream   fin   =   new   java.io.FileInputStream(file);    
  //mysql.setBinaryStream(5,fin,fileLength);    
  public   void   setBinaryStream(int   index,InputStream   in,int   length)   throws   SQLException  
  {    
  prepstmt.setBinaryStream(index,in,length);    
  }  
   
     
  /***  
  *   通过Statement对象,执行SQL查询语句返回字段集    
  *   @param   sql   SQL语句    
  *   @return   ResultSet   字段集    
  ***/    
  public   ResultSet   Query(String   sql)   throws   SQLException  
  {    
  if   (stmt   !=   null)  
  {    
  return   stmt.executeQuery(sql);    
  }    
  else   return   null;    
  }    
   
  /***  
  *   通过PreparedStatement对象,执行SQL查询语句返回字段集    
  *   @return   ResultSet   字段集    
  ***/    
  public   ResultSet   PreQuery()   throws   SQLException  
  {    
  if   (prepstmt   !=   null)  
  {    
  return   prepstmt.executeQuery();    
  }  
  else   return   null;    
  }    
   
  /***  
  *   通过Statement对象,执行SQL更新语句    
  *   @param   sql   SQL语句    
  ***/    
  public   void   Update(String   sql)   throws   SQLException  
  {    
  if   (stmt   !=   null)   stmt.executeUpdate(sql);    
  }    
   
  /***  
  *   通过PreparedStatement对象,执行SQL更新语句    
  ***/    
  public   void   PreUpdate()   throws   SQLException  
  {    
  if   (prepstmt   !=   null)   prepstmt.executeUpdate();    
  }    
   
  /***    
  *   关闭连接    
  ***/    
  public   void   close()  
  {    
  if   (stmt   !=   null)  
  {    
  try  
  {  
  stmt.close();    
  }  
  catch   (Exception   e1){}      
  stmt   =   null;    
  }    
  if   (prepstmt   !=   null)  
  {  
  try  
  {  
  prepstmt.close();  
  }  
  catch   (Exception   e1)   {}    
  prepstmt   =   null;    
  }    
  try  
  {  
  conn.close();    
  }  
  catch   (Exception   e1)   {}  
  conn   =   null;    
  }    
  }    
   
   
  jsp文件:  
  ===========================================  
  //引用javaBean:tia.class  
  <jsp:useBean   id="tia"   class="Mysql"   scope="page"/>  
   
  <%  
  ResultSet   rs   =   null;    
  long   start   =   System.currentTimeMillis();  
  tia.Open();  
  String   sql="Select   *   from   ccbhdzb";  
  //执行查询  
  rs   =   tia.Query(sql);  
  while   (rs.next())  
  {  
      //输出每一条记录  
      for(int   j=1;   j<=rs.getMetaData().getColumnCount();   j++)  
        {  
        //输出一条记录每一列的值  
        out.print(   rs.getObject(j)+"\t");  
          }  
        out.println("<BR>");          
  }  
   
  //取得程序执行时间  
  out.println("执行时间   (毫秒):   "   +   (System.currentTimeMillis()   -   start));  
  %>    
  问题点数:0、回复次数:2Top

1 楼zxhong(红透半边天)回复于 2003-01-04 21:39:43 得分 0

class位置:  
  jsp文件所在目录下/WEB-INF/classes/package(if   you   have   package   in   your   java   class)/*.class  
   
  用SQLException   要import   java.sql.*Top

2 楼wangbd8(wangbd8)回复于 2003-01-05 12:28:18 得分 0

解决了,使病毒闹的Top

相关问题

  • 我用ado访问数据库,出错·!!!
  • ejb访问sqlserver数据库出错。
  • 数据库访问出错问题。
  • ADO访问数据库出错问题
  • DELPHI访问ACCESS数据库出错,怎么回事?
  • 用ADO访问有密码的ACCESS数据库出错!!
  • 访问数据库出错???急需解决!!!
  • 访问远程数据库出错,该如何解决,help,help.....
  • 关于asp访问数据库的出错
  • 为什么出错?ADO访问数据库问题!

关键词

  • 语句
  • sql
  • javabean
  • 连接
  • jsp
  • mysql
  • apache
  • tomcat
  • jdbc
  • program

得分解答快速导航

  • 帖主:wangbd8

相关链接

  • CSDN Java频道
  • Java类图书
  • Java类源码下载

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo