在进行Java数据库开发的时候,我们通常将对数据库的操作封装在JavaBean中。这样在页面中的数据处理时我们就不用将这些语句都放置在try-catch语句了。但是如何返回详细的错误信息呢? 
    在实际操作中,我采用了这样的办法--定义两个私有变量int countInt 和 String strDBError 记录错误编码和错误信息。演示代码如下: 
package DB; 
/**  * Title:        JavaBean of DataBase  * Copyright:    Copyright (c) 2002  * Company:      weide  * @author   weidegong  * @version 1.0  */ import java.sql.*; import java.util.*; 
public class dataConn { 
  private Connection conn = null;   private Statement drpStmt = null;   private ResultSet drpRst = null;   private int countInt = 0;//0,默认情况;-1,表示发生数据库操作错误;-2,java.lang.Exception;其他编码.....   private String strDBError="";//错误信息   boolean autoCommit; 
  public dataConn() {   try{  //jdbc-odbc  Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");  conn=DriverManager.getConnection("jdbc:odbc:sqlserver","sa","706adm607"); 
 drpStmt = conn.createStatement();   }   catch(SQLException sqle){      countInt=-1;   strDBError=sqle.getMessage();   }   catch(java.lang.Exception e){       countInt=-2;    strDBError=e.getMessage();   }   } 
//------------------------------------------------------------------- 
public int getErrorCode(){ 
   return countInt; 
} 
//------------------------------------------------------------------- 
  
//------------------------------------------------------------------- 
public String getErrorInfo(){ 
   return strDBError; 
} 
//------------------------------------------------------------------- 
  
//-------------------------------------------------------------------   public ResultSet exeQuery(String queryString){     countInt=0;//初始化为0     strDBError="";//初始化错误信息     try{      drpRst = drpStmt.executeQuery(queryString);      countInt=1;     }catch(SQLException sqly){       countInt=-1;       strDBError+="Error occur while useing dataConn.exeQuery(queryString)!<p>The queryString is <p>" + queryString + "<p>The Error Information from DBMS:<p>"+sqly.getMessage();     }catch(java.lang.Exception y){       countInt=-2;       strDBError+="<p>java.lang.Exception:"+y.getMessage();     }     return drpRst;   } //------------------------------------------------------------------- 
  
//-------------------------------------------------------------------   public int exeUpdate(String updateString){     countInt=0;//初始化为0  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();   } %> 
   
 
  |