2012-07-08 24 views
0

我需要將連接池對象傳遞到ScheduledExecutorService線程,但我在這樣做時遇到困難。我試圖在聲明前添加final,但是會引發以下錯誤...最終的局部變量連接無法分配。它必須是空白的並且不使用複合賦值將連接池對象傳遞給方法

如何正確傳遞此連接對象?

public class AdminManager extends JFrame { 

    private JPanel contentPane; 
    private JTable tableQueue; 
    private JTable tableFilled; 

    /** 
    * Launch the application. 
    * @throws InterruptedException 
    */ 

    public static void main(String[] args) throws InterruptedException { 
     EventQueue.invokeLater(new Runnable() { 
      @Override 
      public void run() { 
       try { 
        AdminManager frame = new AdminManager(); 
        frame.setVisible(true); 
       } catch (Exception e) { 
        e.printStackTrace(); 
       } 

      } 
     }); 

     BoneCP connectionPool = null; 
     Connection connection = null; 

     try { 
      // load the database driver (make sure this is in your classpath!) 
      Class.forName("com.mysql.jdbc.Driver"); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      return; 
     } 

     try { 
      // setup the connection pool 
      BoneCPConfig config = new BoneCPConfig(); 
      config.setJdbcUrl("jdbc:mysql://192.0.0.1:3306/db"); // jdbc url specific to your database, eg jdbc:mysql://127.0.0.1/yourdb 
      config.setUsername("root"); 
      config.setPassword(""); 
      connectionPool = new BoneCP(config); // setup the connection pool 

      connection = connectionPool.getConnection(); // fetch a connection 

      if (connection != null){ 
       System.out.println("Connection successful!"); 
      } 

     } catch (SQLException e) { 
      e.printStackTrace(); 
     } finally { 
      if (connection != null) { 
       try { 
        connection.close(); 
       } catch (SQLException e) { 
        e.printStackTrace(); 
       } 
      } 
     }   
ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(); 
     exec.scheduleAtFixedRate(new Runnable(){ 
      @Override 
      public void run(){ 
       System.out.println("Working ... "); 
       String sql = "SELECT * FROM table;"; 
       Statement st; 
       try { 
        st = connection.createStatement(); 
        ResultSet rs = st.executeQuery(sql); 
       } catch (SQLException e) { 
        // TODO Auto-generated catch block 
        e.printStackTrace(); 
       } 


      } 
     }, 2000, 1000, TimeUnit.MILLISECONDS); 

回答

1

你可以定義一個虛設的參考是唯一執行人內部使用:

final Connection conn = connection; 

ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(); 
    exec.scheduleAtFixedRate(new Runnable(){ 
     @Override 
     public void run(){ 
      System.out.println("Working ... "); 
      String sql = "SELECT * FROM table;"; 
      Statement st; 
      try { 
       st = conn.createStatement(); 
       ResultSet rs = st.executeQuery(sql); 
      } catch (SQLException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    }, 2000, 1000, TimeUnit.MILLISECONDS); 

如果問題不能得到解決這樣(不能看到你的代碼的其餘部分),你可以定義一個外部任務類來保存代碼:

class ThreadTask implements Runnable { 
    private Connection connection; 

    public ThreadTask(Connection c) { 
      connection = c; 
    } 

    @Override 
    public void run() { 
     System.out.println("Working ... "); 
     String sql = "SELECT * FROM table;"; 
     Statement st; 
     try { 
      st = connection.createStatement(); 
      ResultSet rs = st.executeQuery(sql); 
     } catch (SQLException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
    } 
} 

和:

ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(); 
exec.scheduleAtFixedRate(new ThreadTask(connection)); 
+0

但是,這會創建一個新班級?我改變了我的代碼以符合你的建議,並且它給了我\t沒有可以訪問AdminManager類型的封閉實例。必須使用封閉的AdminManager類型實例來限定分配(例如x.new A(),其中x是AdminManager的實例)。 – scriptdiddy 2012-07-08 17:59:33

+0

@scriptdiddy:嘗試將類「靜態」或移動到當前正在工作的類之外。 – Tudor 2012-07-08 18:03:00

+0

@scriptdiddy:你有沒有嘗試在創建新類之前應用第一個解決方案?從你更新的代碼,它應該沒有定義一個新的類。只需定義對同一連接對象的最終引用即可。 – Tudor 2012-07-08 18:06:11