http://docs.oracle.com/javase/1.5.0/docs/api/java/util/concurrent/ScheduledExecutorService.html
有一个scheduleAtFixedRate方法。要将某些东西传递给匿名类,需要将其声明为final。它必须在相同的范围内。
同样,您现在拥有的代码正在关闭连接,如果您打算将其传递给另一个线程,则需要保持连接打开。
编辑一些示例代码
public class Whatever { public static void main(String[] args) throws Exception { // ... do your frame thing loadDatabaseDriver(); BoneCP connectionPool = createConnectionPool(); try { final Connection connection = connectionPool.getConnection(); ScheduledExecutorService exec = Executors.newSingleThreadScheduledExecutor(); exec.scheduleAtFixedRate(new Runnable(){ @Override public void run(){ System.out.println("Working ... "); // use connection } }, 0, 1, TimeUnit.SECONDS); } catch (SQLException e) { // do whatever } } public static BoneCP createConnectionPool() throws SQLException { BoneCPConfig config = new BoneCPConfig(); config.setJdbcUrl("jdbc:mysql://192.0.0.1:3306/database"); // jdbc url specific to your database, eg jdbc:mysql://127.0.0.1/yourdb config.setUsername("root"); config.setPassword(""); connectionPool = new BoneCP(config); return connectionPool; } public static void loadDatabaseDriver() { try { // load the database driver (make sure this is in your classpath!) Class.forName("com.mysql.jdbc.Driver"); } catch (Exception e) { e.printStackTrace(); return; } }}我不知道您正在调用的方法的签名,因此错误可能是错误的



