DBUtils資料查詢範例(使用ArrayHandler)
前言
DBUtils有8種資料查詢方式,楊藝會將先前的資料一一整理出來,首先ArrayHandler僅會返回查詢結果第一列資料,並結果存成一個Array,索引值則為查詢回傳欄位的順序。
實作
Java程式碼
DBUtilsSelectByArrayHandlerDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
| package cc.artyang;
import java.sql.SQLException;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.ArrayHandler;
import cc.artyang.conf.DataSourceConf;
/**
* DBUtils 資料查詢 (使用 ArrayHandler)
*
* @author Art Yang
*
*/
public class DBUtilsSelectByArrayHandlerDemo {
/**
* <p>主程式方法</p>
* <p>使用 ArrayHandler 進行資料查詢會返回查詢結果的第一列,並將欄位封裝成 Object[]</p>
*
* @param args 外部參數
* @throws ClassNotFoundException
* @throws SQLException
*/
public static void main(String[] args) throws ClassNotFoundException, SQLException {
String sqlStmt = "select * from blog";
QueryRunner queryRunner = new QueryRunner(DataSourceConf.getDataSource());
Object[] result = queryRunner.query(sqlStmt, new ArrayHandler());
for(Object object: result) {
System.out.println(object);
}
}
}
|
執行結果
1
2
3
4
5
6
| 1
楊藝的洋溢生活
Art Yang
生活記事、系統管理、程式設計、資料庫
2019-04-29 04:30:04.0
null
|