Dbutils 简明教程

AsyncQueryRunner interface

org.apache.commons.dbutils.AsyncQueryRunner 类可帮助以异步支持执行长时间运行的 SQL 查询。该类是线程安全的。该类支持与 QueryRunner 相同的方法,但它返回 Callable 对象,可用于稍后检索结果。

Class Declaration

以下是 org.apache.commons.dbutils.AsyncQueryRunner 类的声明 −

public class AsyncQueryRunner
   extends AbstractQueryRunner

Usage

  1. Step 1 - 创建连接对象。

  2. Step 2 − 使用 AsyncQueryRunner 对象方法进行数据库操作。

Example

下面的示例将演示如何使用 AsyncQueryRunner 类更新记录。我们将更新 employee 表中可用的一个记录。

Syntax

String updateQuery = "UPDATE employees SET age=? WHERE id=?";
future = asyncQueryRunner.update(conn,
            "UPDATE employees SET age=? WHERE id=?", 33,103);

其中,

  1. updateQuery − 具有占位符的更新查询。

  2. asyncQueryRunner − 用于更新数据库中员工对象的 asyncQueryRunner 对象。

  3. future − Future 对象用于稍后检索结果。

为了了解与 DBUtils 相关的上述概念,让我们编写一个将以异步模式运行更新查询的示例。为了编写我们的示例,让我们创建一个示例应用程序。

Step

Description

1

更新在章节 DBUtils - First Application 下创建的文件 MainApp.java。

2

按以下说明编译并运行应用程序。

以下是 Employee.java 的内容。

public class Employee {
   private int id;
   private int age;
   private String first;
   private String last;
   public int getId() {
      return id;
   }
   public void setId(int id) {
      this.id = id;
   }
   public int getAge() {
      return age;
   }
   public void setAge(int age) {
      this.age = age;
   }
   public String getFirst() {
      return first;
   }
   public void setFirst(String first) {
      this.first = first;
   }
   public String getLast() {
      return last;
   }
   public void setLast(String last) {
      this.last = last;
   }
}

以下是 MainApp.java 文件的内容。

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

import org.apache.commons.dbutils.AsyncQueryRunner;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;

import java.util.concurrent.Callable;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorCompletionService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.TimeoutException;

public class MainApp {
   // JDBC driver name and database URL
   static final String JDBC_DRIVER = "com.mysql.jdbc.Driver";
   static final String DB_URL = "jdbc:mysql://localhost:3306/emp";

   //  Database credentials
   static final String USER = "root";
   static final String PASS = "admin";

   public static void main(String[] args) throws
      SQLException, InterruptedException,
      ExecutionException, TimeoutException {
      Connection conn = null;

      AsyncQueryRunner asyncQueryRunner = new AsyncQueryRunner( Executors.newCachedThreadPool());

      DbUtils.loadDriver(JDBC_DRIVER);
      conn = DriverManager.getConnection(DB_URL, USER, PASS);
      Future<Integer> future = null;
      try {
         future = asyncQueryRunner.update(conn,
            "UPDATE employees SET age=? WHERE id=?", 33,103);
         Integer updatedRecords = future.get(10, TimeUnit.SECONDS);
         System.out.println(updatedRecords + " record(s) updated.");
      } finally {
         DbUtils.close(conn);
      }
   }
}

一旦你创建好源文件,让我们运行应用程序。如果你的应用程序没有问题,它将打印以下消息。

1 record(s) updated.