簡介
補充一下,傳統運算元據庫的類指的是JDBC(java database connectivity:java資料庫連線,java的資料庫操作的基礎API。)。
特徵
DBUtils是java編程中的資料庫操作實用工具,小巧簡單實用,
1.對於數據表的讀操作,他可以把結果轉換成List,Array,Set等java集合,便於程式設計師操作;
2.對於數據表的寫操作,也變得很簡單(只需寫sql語句)
3.可以使用數據源,使用JNDI,資料庫連線池等技術來最佳化性能--重用已經構建好的資料庫連線對象,而不像php,asp那樣,費時費力的不斷重複的構建和析構這樣的對象。
DBUtils包括3個包:
org.apache.commons.dbutils
org.apache.commons.dbutils.handlers
org.apache.commons.dbutils.wrappers
DBUtils封裝了對JDBC的操作,簡化了JDBC操作,可以少寫代碼。
org.apache.commons.dbutils
DbUtils 關閉連結等操作
QueryRunner 進行查詢的操作
org.apache.commons.dbutils.handlers
ArrayHandler :將ResultSet中第一行的數據轉化成對象數組
ArrayListHandler將ResultSet中所有的數據轉化成List,List中存放的是Object[]
BeanHandler :將ResultSet中第一行的數據轉化成類對象
BeanListHandler :將ResultSet中所有的數據轉化成List,List中存放的是類對象
ColumnListHandler :將ResultSet中某一列的數據存成List,List中存放的是Object對象
KeyedHandler :將ResultSet中存成映射,key為某一列對應為Map。Map中存放的是數據
MapHandler :將ResultSet中第一行的數據存成Map映射
MapListHandler :將ResultSet中所有的數據存成List。List中存放的是Map
ScalarHandler :將ResultSet中一條記錄的其中某一列的數據存成Object
org.apache.commons.dbutils.wrappers
SqlNullCheckedResultSet :對ResultSet進行操作,改版裡面的值
StringTrimmedResultSet :去除ResultSet中中欄位的左右空格。Trim()
主要方法:
DbUtils類:啟動類
ResultSetHandler接口:轉換類型接口
MapListHandler類:實現類,把記錄轉化成List
BeanListHandler類:實現類,把記錄轉化成List,使記錄為JavaBean類型的對象
Query Runner類:執行SQL語句的類
建立三個Java檔案
命名為BeanListExample.java
Guestbook.java
MapListExample.java
源碼:
BeanListExample.java
Code
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
public class BeanListExample {
public static void main(String[] args) {
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/people";
String jdbcDriver = "com.mysql.jdbc.Driver";
String user = "root";
String password = "hicc";
DbUtils.loadDriver(jdbcDriver);
try {
conn = DriverManager.getConnection(url, user, password);
QueryRunner qr = new QueryRunner();
List results = (List) qr.query(conn, "select id,name from guestbook", new BeanListHandler(Guestbook.class));
for (int i = 0; i < results.size(); i++) {
Guestbook gb = (Guestbook) results.get(i);
System.out.println("id:" + gb.getId() + ",name:" + gb.getName());
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DbUtils.closeQuietly(conn);
}
}
}
Guestbook.java
Code
public class Guestbook {
private Integer id;
private String name;
public Integer getId() {
return id;
}
/**get,set方法*/
}
MapListExample.java
Code
import org.apache.commons.dbutils.DbUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.MapListHandler;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.List;
import java.util.Map;
public class MapListExample {
public static void main(String[] args) {
Connection conn = null;
String url = "jdbc:mysql://localhost:3306/people";
String jdbcDriver = "com.mysql.jdbc.Driver";
String user = "root";
String password = "hicc";
DbUtils.loadDriver(jdbcDriver);
try {
conn = DriverManager.getConnection(url, user, password);
QueryRunner qr = new QueryRunner();
List results = (List) qr.query(conn, "select id,name from guestmessage", new MapListHandler());
for (int i = 0; i < results.size(); i++) {
Map map = (Map) results.get(i);
System.out.println("id:" + map.get("id") + ",name:" + map.get("name"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
DbUtils.closeQuietly(conn);
}
}
}
使用組建好需要添加commons-dbutils-1.1.jar和mysql-connector-java-5.1.6-bin.jar兩個jar包。
配置完畢!!!
//另一種方法
//使用dbutils1.0版本
import java.util.*;
import java.util.logging.*;
import java.sql.*;
import org.apache.commons.dbutils.*;
import org.apache.commons.dbutils.handlers.*;
public class TestDBUnits {
public static void main(String[]args) throws Exception {
TestDBUnits test = new TestDBUnits();
for(int i = 0 ; i < 1 ; i++) {
test.testQuery1();
test.testQuery2();
test.testUpdate();
}
}
public void testQuery1(){
try {
QueryRunner qr = new QueryRunner() ;
ResultSetHandler rsh = new ArrayListHandler();
String strsql = "select * from test1";
ArrayList result = (ArrayList)qr.query(getConnection() ,strsql ,rsh);
//System.out.print("");
} catch(Exception ex) {
ex.printStackTrace(System.out);
}
}
public void testQuery2(){
try {
QueryRunner qr = new QueryRunner() ;
ResultSetHandler rsh = new MapListHandler();
String strsql = "select * from test1";
ArrayList result = (ArrayList)qr.query(getConnection() ,strsql ,rsh);
for(int i = 0 ; i < result.size() ; i++) {
Map map = (Map)result.get(i);
//System.out.println(map);
}
//System.out.print("");
} catch(Exception ex) {
ex.printStackTrace(System.out);
}
}
public void testUpdate(){
try {
QueryRunner qr = new QueryRunner() ;
ResultSetHandler rsh = new ArrayListHandler();
String strsql = "insert test1(page ,writable ,content)values('ttt','ttt','faskldfjklasdjklfjasdklj')";
qr.update(getConnection() ,strsql);
//System.out.print("");
} catch(Exception ex) {
ex.printStackTrace(System.out);
}
}
private Connection getConnection() throws InstantiationException,
IllegalAccessException, ClassNotFoundException, SQLException {
String strDriver = "org.gjt.mm.mysql.Driver";
String strUrl = "jdbc:mysql://localhost:3306/test";
String strUser = "root";
String strPass = "";
Class.forName(strDriver).newInstance();
return DriverManager.getConnection(strUrl, strUser, strPass);
}
}