?? usingrowset.java
字號(hào):
package book.database;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.sql.SQLException;
import javax.sql.RowSet;
import javax.sql.rowset.CachedRowSet;
import javax.sql.rowset.FilteredRowSet;
import javax.sql.rowset.JdbcRowSet;
import javax.sql.rowset.JoinRowSet;
import javax.sql.rowset.Predicate;
import javax.sql.rowset.WebRowSet;
import com.sun.rowset.CachedRowSetImpl;
import com.sun.rowset.FilteredRowSetImpl;
import com.sun.rowset.JdbcRowSetImpl;
import com.sun.rowset.JoinRowSetImpl;
import com.sun.rowset.WebRowSetImpl;
/**
* 本例演示如何使用RowSet接口。RowSet繼承ResultSet,比ResultSet更好用,
* 是是JDK 1.5的新特征之一。該接口有幾個(gè)子接口:
* (1)CachedRowSet:可以不用與數(shù)據(jù)源建立長(zhǎng)期的連接,只有當(dāng)從數(shù)據(jù)庫(kù)讀取數(shù)據(jù),
* 或是往數(shù)據(jù)庫(kù)寫(xiě)入數(shù)據(jù)的時(shí)候才會(huì)與數(shù)據(jù)庫(kù)建立連接,它提供了一種輕量級(jí)的訪問(wèn)數(shù)據(jù)庫(kù)的方式,其數(shù)據(jù)均存在內(nèi)存中。
* 避免了ResultSet在關(guān)閉Statement或Connection后無(wú)法讀取數(shù)據(jù)的問(wèn)題。
* (2)JdbcRowSet:對(duì)ResultSet的對(duì)象進(jìn)行包裝,使得可以將ResultSet對(duì)象做為一個(gè)JavaBeans組件,
* (3)FilteredRowSet:繼承自CachedRowSet,可以根據(jù)設(shè)置條件得到數(shù)據(jù)的子集。
* (4)JoinRowSet:繼承自CachedRowSet,可以將多個(gè)RowSet對(duì)象進(jìn)行SQL Join語(yǔ)句的合并。
* (5)WebRowSet:繼承自CachedRowSet,可以將WebRowSet對(duì)象輸出成XML格式。
*/
public class UsingRowSet {
/**
* 使用CachedRowSet。
* 一旦獲得數(shù)據(jù),CachedRowSet就可以斷開(kāi)與數(shù)據(jù)庫(kù)的連接,
* 直到往數(shù)據(jù)庫(kù)寫(xiě)入數(shù)據(jù)的時(shí)候才需建立連接。
*/
public static void usingCachedRowSet() throws SQLException{
CachedRowSet crs = new CachedRowSetImpl();
// 設(shè)置CachedRowSet的屬性,用它可以直接連數(shù)據(jù)庫(kù)
crs.setUrl("jdbc:mysql://127.0.0.1/studentdb");
crs.setUsername("test");
crs.setPassword("test");
crs.setCommand("select * from student_basic where score > ?");
crs.setDouble(1, 60);
try {
// 需要先加載驅(qū)動(dòng),否則在使用執(zhí)行時(shí)會(huì)找不到驅(qū)動(dòng)類(lèi)
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
}
// CachedRowSet的execute方法執(zhí)行SQL語(yǔ)句,
// 首先會(huì)創(chuàng)建與數(shù)據(jù)庫(kù)的連接,然后將結(jié)果集取出來(lái),再關(guān)閉連接
crs.execute();
// 此時(shí)的CachedRowSet與數(shù)據(jù)庫(kù)是斷開(kāi)連接的
System.out.println("使用CachedRowSet操作數(shù)據(jù)前:");
OperateDB.showResultSet(crs);
// 在斷開(kāi)連接的情況下可以操作數(shù)據(jù)
crs.beforeFirst();
while (crs.next()) {
if (crs.getString("name").equals("mary")) {
crs.updateDouble("score",75);
// 要想將更新的數(shù)據(jù)提交到數(shù)據(jù)庫(kù),必須進(jìn)行下面兩步
// 首先確認(rèn)要修改
crs.updateRow();
// 再調(diào)用acceptChanges方法獲得與數(shù)據(jù)庫(kù)的連接,將修改提交到數(shù)據(jù)庫(kù)
crs.acceptChanges();
break;
}
}
System.out.println("使用CachedRowSet操作數(shù)據(jù)后:");
OperateDB.showResultSet(crs);
crs.close();
}
/**
* 使用JdbcRowSet。
* JdbcRowSet功能與ResultSet類(lèi)似,JdbcRowSet在操作時(shí)保持與數(shù)據(jù)庫(kù)的連接,
* JdbcRowSet返回的結(jié)果默認(rèn)是可以上下滾動(dòng)和可更新的。
*/
public static void usingJdbcRowSet() throws SQLException{
JdbcRowSet jdbcrs = new JdbcRowSetImpl();
// 設(shè)置JdbcRowSet的屬性,用它可以直接連數(shù)據(jù)庫(kù)
jdbcrs.setUrl("jdbc:mysql://127.0.0.1/studentdb");
jdbcrs.setUsername("test");
jdbcrs.setPassword("test");
jdbcrs.setCommand("select * from student_basic where score > ?");
jdbcrs.setDouble(1, 70);
try {
// 需要先加載驅(qū)動(dòng),否則在使用執(zhí)行時(shí)會(huì)找不到驅(qū)動(dòng)類(lèi)
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
}
// JdbcRowSet的execute方法執(zhí)行SQL語(yǔ)句,首先獲得數(shù)據(jù)庫(kù)連接,再獲取結(jié)果集,
// 與CachedRowSet的execute方法不同,執(zhí)行完后它不關(guān)閉連接,它會(huì)一直保持該連接,直到調(diào)用close方法。
jdbcrs.execute();
System.out.println("使用JdbcRowSet操作數(shù)據(jù)前:");
OperateDB.showResultSet(jdbcrs);
// 然后操作數(shù)據(jù)
jdbcrs.beforeFirst();
while (jdbcrs.next()) {
if (jdbcrs.getString("name").equals("mary")) {
jdbcrs.updateDouble("score",85);
// 提交到數(shù)據(jù)庫(kù)
jdbcrs.updateRow();
// 因?yàn)樗旧硎沁B接到數(shù)據(jù)庫(kù)的,所以和CachedRowSet不同,它不需要再次獲得連接。
break;
}
}
System.out.println("使用JdbcRowSet操作數(shù)據(jù)后:");
OperateDB.showResultSet(jdbcrs);
// 關(guān)閉結(jié)果集,此時(shí)關(guān)閉數(shù)據(jù)庫(kù)連接
jdbcrs.close();
}
/**
* 使用FilteredRowSet。
* FilteredRowSet接口中規(guī)定了可以設(shè)定過(guò)濾器,其過(guò)濾接口為Predicate接口,
* 必須實(shí)現(xiàn)Predicate接口中的evaluate方法
*/
public static void usingFilteredRowSet() throws SQLException{
FilteredRowSet frs = new FilteredRowSetImpl();
// 設(shè)置FilteredRowSet的屬性,用它可以直接連數(shù)據(jù)庫(kù)
frs.setUrl("jdbc:mysql://127.0.0.1/studentdb");
frs.setUsername("test");
frs.setPassword("test");
frs.setCommand("select * from student_basic where score > ?");
frs.setDouble(1, 80);
try {
// 需要先加載驅(qū)動(dòng),否則在使用執(zhí)行時(shí)會(huì)找不到驅(qū)動(dòng)類(lèi)
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
}
// FilteredRowSet繼承CachedRowSet,
// 所以execute方法功能與CachedRowSet的execute方法一樣
frs.execute();
// 此時(shí)的CachedRowSet與數(shù)據(jù)庫(kù)是斷開(kāi)連接的
System.out.println("使用FilteredRowSet過(guò)濾數(shù)據(jù)之前:");
OperateDB.showResultSet(frs);
// 設(shè)置過(guò)濾器,過(guò)濾器必須實(shí)現(xiàn)Predicate接口定義的三個(gè)execute方法。
frs.setFilter(new Predicate(){
public boolean evaluate(RowSet rs){
CachedRowSet crs=(CachedRowSet)rs;
// 如果名字為mary則返回true
try {
if (crs.getString("name").equals("mary")){
return true;
}
} catch (SQLException e){
}
return false;
}
public boolean evaluate(Object arg0, int arg1) throws SQLException{
return false;
}
public boolean evaluate(Object arg0, String arg1) throws SQLException{
return false;
}
});
System.out.println("使用FilteredRowSet過(guò)濾數(shù)據(jù)之后:");
OperateDB.showResultSet(frs);
frs.close();
}
/**
* 使用JoinRowSet。
* JoinRowSet可以將多個(gè)RowSet對(duì)象進(jìn)行join合并,
* Join的列可以通過(guò)每個(gè)RowSet通過(guò)調(diào)用setMatchColumn方法來(lái)設(shè)置。
* setMatchColumn方式是Joinable接口定義的方法,五種類(lèi)型的RowSet規(guī)定都需要實(shí)現(xiàn)該接口。
* JoinRowSet繼承CachedRowSet,也不需要保持與數(shù)據(jù)庫(kù)的連接。
*/
public static void usingJoinRowSet() throws SQLException{
JoinRowSet joinrs = new JoinRowSetImpl();
CachedRowSet crs = new CachedRowSetImpl();
// 設(shè)置CachedRowSet的屬性,用它可以直接連數(shù)據(jù)庫(kù)
crs.setUrl("jdbc:mysql://127.0.0.1/studentdb");
crs.setUsername("test");
crs.setPassword("test");
crs.setCommand("select * from student_basic");
try {
// 需要先加載驅(qū)動(dòng),否則在使用執(zhí)行時(shí)會(huì)找不到驅(qū)動(dòng)類(lèi)
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
}
// 獲取結(jié)果集
crs.execute();
// 設(shè)置結(jié)果集在Join時(shí)匹配的列名。
crs.setMatchColumn("name");
// 將結(jié)果集數(shù)據(jù)放入JoinRowSet
joinrs.addRowSet(crs);
crs.close();
// 查另外一個(gè)表
crs.setCommand("select name, address from student_address");
crs.execute();
crs.setMatchColumn("name");
joinrs.addRowSet(crs);
crs.close();
System.out.println("使用JoinRowSet對(duì)多個(gè)結(jié)果集進(jìn)行Join操作:");
// 此時(shí)兩個(gè)結(jié)果集已經(jīng)Join在一起了,
// 表student_basic的name列和student_address的name列進(jìn)行匹配
while (joinrs.next()){
// name屬性公有,score屬性為表student_basic所有
System.out.print(joinrs.getString("name") + "\t");
System.out.print(joinrs.getDouble("score") + "\t");
// address屬性為student_address所有
System.out.println(new String(joinrs.getBytes("address")));
}
joinrs.close();
}
/**
* 使用WebRowSet。
* WebRowSet繼承自CachedRowSet,支持XML格式的查詢,更新等操作,
* 下面將WebRowSet對(duì)象輸出成XML格式到文件。
*/
public static void usingWebRowSet() throws SQLException{
WebRowSet wrs = new WebRowSetImpl();
// 設(shè)置CachedRowSet的屬性,用它可以直接連數(shù)據(jù)庫(kù)
wrs.setUrl("jdbc:mysql://127.0.0.1/studentdb");
wrs.setUsername("test");
wrs.setPassword("test");
wrs.setCommand("select * from student_basic");
try {
// 需要先加載驅(qū)動(dòng),否則在使用執(zhí)行時(shí)會(huì)找不到驅(qū)動(dòng)類(lèi)
Class.forName("com.mysql.jdbc.Driver");
} catch (ClassNotFoundException e) {
}
// 獲取結(jié)果集
wrs.execute();
// 輸出到XML文件
try {
FileOutputStream out = new FileOutputStream("student_basic_data.xml");
wrs.writeXml(out);
out.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
wrs.close();
}
public static void main(String[] args) throws SQLException {
UsingRowSet.usingCachedRowSet();
UsingRowSet.usingJdbcRowSet();
UsingRowSet.usingFilteredRowSet();
UsingRowSet.usingJoinRowSet();
UsingRowSet.usingWebRowSet();
}
}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -