1

我不断收到一条错误消息,指出我需要一个catch子句来伴随try(inside public Connection getConnection())。我不明白为什么编译器没有catch注意到try. 有谁知道为什么它不会编译?我也检查了所有的括号......

package crud.data;

import java.sql.*;
import javax.sql.DataSource;
import javax.naming.InitialContext;
import javax.naming.NamingException;

public class ConnectionPool
{
       private static ConnectionPool pool = null;
       private static DataSource dataSource = null;


       private ConnectionPool()
       {
           try
           {
                InitialContext ic = new InitialContext();
                dataSource = (DataSource)           
                        ic.lookup("java:/comp/env/jdbc/acm14n");
           }
           catch (NamingException e)
           {
                System.out.println(e);
           }
     }

     public static synchronized ConnectionPool getInstance()
     {
            if(pool == null)
            {
                 pool = new ConnectionPool();
            } 
            return pool;
     }

     public Connection getConnection()
     {
         try
         {
             return dataSource.getConnection();
         }
         catch  (SQLException e)
         {
            System.out.println(e);
            return null;
         }  
     }

     public void freeConnection(Connection c)
     {
         try
         {
            c.close();
         } 
         catch (SQLException e)
         {
                System.out.println(e);
         }
     }
}
4

0 回答 0