Dbutils 简明教程
Apache Commons DBUtils - First Application
本章提供了一个使用 DBUtils 库创建简单 JDBC 应用程序的示例。这将向您展示如何打开数据库连接、执行 SQL 查询和显示结果。
This chapter provides an example of how to create a simple JDBC application using DBUtils library. This will show you, how to open a database connection, execute a SQL query, and display the results.
本模板示例中提到的所有步骤都将在本教程的后续章节中进行解释。
All the steps mentioned in this template example, would be explained in subsequent chapters of this tutorial.
Creating JDBC Application
构建一个 JDBC 应用程序涉及以下六个步骤 -
There are following six steps involved in building a JDBC application −
-
Import the packages − Requires that you include the packages containing the JDBC classes which are needed for database programming. Most often, using import java.sql.* will suffice.
-
Register the JDBC driver − Requires that you initialize a driver, so you can open a communication channel with the database.
-
Open a connection − Requires using the DriverManager.getConnection() method to create a Connection object, which represents a physical connection with the database.
-
Execute a query − Requires using an object of type Statement for building and submitting an SQL statement to the database.
-
Extract data from result set − Requires that you use the appropriate ResultSet.getXXX() method to retrieve the data from the result set.
-
Clean up the environment − Requires explicitly closing all the database resources versus relying on the JVM’s garbage collection.
Sample Code
当您需要在未来创建自己的 JDBC 应用程序时,该示例可以作为模板。
This sample example can serve as a template, when you need to create your own JDBC application in the future.
此示例代码是基于前一章节中完成的环境与数据库设置编写的。
This sample code has been written based on the environment and database setup done in the previous chapter.
在 MainApp.java 中复制并粘贴以下示例,按如下方式编译并运行 −
Copy and paste the following example in MainApp.java, compile and run as follows −
MainApp.java
MainApp.java
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.ResultSetHandler;
import org.apache.commons.dbutils.handlers.BeanHandler;
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 {
Connection conn = null;
QueryRunner queryRunner = new QueryRunner();
//Step 1: Register JDBC driver
DbUtils.loadDriver(JDBC_DRIVER);
//Step 2: Open a connection
System.out.println("Connecting to database...");
conn = DriverManager.getConnection(DB_URL, USER, PASS);
//Step 3: Create a ResultSet Handler to handle Employee Beans
ResultSetHandler<Employee> resultHandler = new BeanHandler<Employee>(Employee.class);
try {
Employee emp = queryRunner.query(conn,
"SELECT * FROM employees WHERE first=?", resultHandler, "Sumit");
//Display values
System.out.print("ID: " + emp.getId());
System.out.print(", Age: " + emp.getAge());
System.out.print(", First: " + emp.getFirst());
System.out.println(", Last: " + emp.getLast());
} finally {
DbUtils.close(conn);
}
}
}
Employee.java
Employee.java
程序如下所示 −
The program is given below −
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;
}
}
现在让我们如下编译上述示例:
Now let us compile the above example as follows −
C:\>javac MainApp.java Employee.java
C:\>
运行 MainApp ,它会生成以下结果 −
When you run MainApp, it produces the following result −
C:\>java MainApp
Connecting to database...
ID: 103, Age: 28, First: Sumit, Last: Mittal
C:\>