您的位置:首页 >> 编程开发 >> Java >> JavaBeans >> 正文
RSS
 

如何处理连接数据库JavaBean的错误信息

http://www.rdxx.com 01年11月18日 11:08 互联网 我要投稿


 strDBError="";
    try{
      countInt = drpStmt.executeUpdate(updateString);
    }catch(SQLException sqlz){
      countInt=-1;//返回-1,表示发生错误
      strDBError="Error occur while using dataConn.exeUpdate()!The SQL is:<p>" + updateString + "<p>来自数据库的出错信息为:<P>" + sqlz.getMessage();
    }
     return countInt;
   }
//--------------------------------------------------------------------


  //-------------------------------------------------------
  //获取指定SQL查询语句执行后返回的记录数
  //避免在strSQL中包含“order  by”语句,如果有则除去,否则会出错。
  //去掉“order  by”子句对于获取总记录数没有任何影响。
  public int getRowCount(String queryString){
  countInt=0;
  strDBError="";
  ResultSet rs;
  int nRowCount=0;
    try{
      rs=drpStmt.executeQuery("select count(*) from (" + queryString + ") as viewTempQueryString" );
      while(rs.next()) nRowCount=rs.getInt(1);
    }
    catch(SQLException errGetRowCount){
      countInt=-1;//发生错误
      strDBError+="Error occur while useing dataConn.getRowCount("+queryString+")! 请与管理员联系。<p>从数据库返回的错误信息为:" + errGetRowCount.getMessage();
    }catch(java.lang.Exception errOther){
      countInt=-2;
      strDBError+="<p>其他错误:" + errOther.getMessage();
    }
    return nRowCount;
  }
  //-----------------------------------------------------------

}//end of all

    定义了上述JavaBean,在JSP页面中的使用演示如下:

Test.jsp源代码-----------------

<jsp:useBean id="conDB" scope="session" class="DB.dataConn" />

<%
  String strSQL="select * from table";
  ResultSet rs=conDB.exeQuery(strSQL);
  if(conDB.getErrorCode()<0){
    out.println("发生错误:"+conDB.getErrorInfo());
    out.close();
  }
%>

 

9 7 3 1 2 4 8 :

 
 
标签: 连接数据库 , 错误信息 , JavaBean , 数据 , 信息 , BEA , 连接 , Java , 错误 打印本文
 
 
  热点搜索
 
 
 



Valid XHTML 1.0 Transitional
Copyright ©2005 - 2008 Rdxx.Com,All Rights Reserved
收藏本页
收藏本站