还有一个错误没有解决,数据库的连接DBConn没有问题,可以编译生成class文件,就是下边的操作文件DBResult.java总是编译错,找不到错在哪里?? 
-------------------------------------------
错误信息:
javac DBResult.javaDBResult.java:12: unreported exception java.lang.Exception; must be caught or de
clared to be thrown
    this.con=DBConn.getConnection();
                                 ^
1 error
---------------------------------------------
//数据库连接的DBConn.java 
package com.mingri.dbconn; import java.sql.*; 
import javax.naming.*; 
import javax.sql.DataSource; public class DBConn 

  public static synchronized Connection getConnection() throws Exception{ 
    try{ 
      Context initCtx=new javax.naming.InitialContext(); 
      //从Context中lookup数据源 
      Context envCtx=(Context)initCtx.lookup("java:comp/env"); 
      DataSource ds=(DataSource)envCtx.lookup("jdbc/mingri"); 
      return ds.getConnection(); 
    } 
    catch(SQLException e){ 
      throw e; 
    } 
    catch(NamingException e){ 
      throw e; 
    } 
  } 

--------------------------------------------------
//数据库操作DBResult.javapackage com.mingri.dbconn;import java.sql.*;import javax.naming.*;public class DBResult
{
  private Connection con;
  public  DBResult(){
    try{
    
    this.con=DBConn.getConnection();
    
    }
    catch(SQLException ex){}
    catch(NamingException ex){ }
  }
  /**
   * 用于获得执行SQL语句的ResultSet对象
   */
  public ResultSet getResult(String sql){
    try{
      Statement stmt=con.createStatement();
      ResultSet rs=stmt.executeQuery(sql);
      return rs;
    }
    catch(Exception e){}
    return null;
  }
  /**
   * 用于执行SQL语句没有返回值
   */
  public void doExecute(String sql){
    try{
      Statement stmt=con.createStatement();
      stmt.executeQuery(sql);
    }catch(Exception e){}
  }
  /**
   * 用于获得执行SQL语句的PreparedStatement(预处理)对象
   */
  public PreparedStatement getPreparedStatement(String sql){
    try{
      PreparedStatement pstmt=con.prepareStatement(sql);
      return pstmt;
    }
    catch(Exception e){}
    return null;
  }
  /**
   * 关闭连接
   */
  public void closeCon(){
    try{
      this.con.close();
    }catch(Exception e){
        e.printStackTrace();
    }
  }
}

解决方案 »

  1.   

    DBResult.java:12: unreported exception java.lang.Exception; must be caught or de
    clared to be thrown 错误提示很清楚呀。public class DBResult throws Exception{ must be caught or declared to be thrown 
      

  2.   

        public DBResult()
        {
            try
            {
                this.con = DBConn.getConnection();
            }
            catch (SQLException ex)
            {
            }
            catch (NamingException ex)
            {
            }
            catch (Exception e)
            {
            }
        }
      

  3.   

    this.con=DBConn.getConnection(); 很明显,DBConn.getConnection()方法所抛出的异常是Exception。而捕获这个方法的catch块中并没有捕获Exception异常。所以无法进行编译。楼上的解决方法可行。但从设计角度上来说,数据访问层的异常应当继续上抛。并使用自定义异常类或手动编写异常信息的方式上抛。