久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

由 ResultSet 支持的 Java 迭代器

Java Iterator backed by a ResultSet(由 ResultSet 支持的 Java 迭代器)
本文介紹了由 ResultSet 支持的 Java 迭代器的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個使用 ResultSet 作為數(shù)據(jù)成員來實現(xiàn) Iterator 的類.基本上這個類看起來像這樣:

I've got a class that implements Iterator with a ResultSet as a data member. Essentially the class looks like this:

public class A implements Iterator{
    private ResultSet entities;
    ...
    public Object next(){
        entities.next();
        return new Entity(entities.getString...etc....)
    }

    public boolean hasNext(){
        //what to do?
    }
    ...
}

我如何檢查 ResultSet 是否有另一行,以便創(chuàng)建有效的 hasNext 方法,因為 ResultSet 本身沒有定義 hasNext?我正在考慮執(zhí)行 SELECT COUNT(*) FROM... 查詢以獲取計數(shù)并管理該數(shù)字以查看是否還有另一行,但我想避免這種情況.

How can I check if the ResultSet has another row so I can create a valid hasNext method since ResultSet has no hasNext defined itself? I was thinking doing SELECT COUNT(*) FROM... query to get the count and managing that number to see if there's another row but I'd like to avoid this.

推薦答案

這是個壞主意.這種方法要求在讀取最后一行之前一直打開連接,并且在 DAO 層之外你永遠不知道它什么時候會發(fā)生,而且你似乎也讓結果集保持打開狀態(tài),并在這種情況下冒著資源泄漏和應用程序崩潰的風險連接超時.你不想擁有那個.

This is a bad idea. This approach requires that the connection is open the whole time until the last row is read, and outside the DAO layer you never know when it will happen, and you also seem to leave the resultset open and risk resource leaks and application crashes in the case the connection times out. You don't want to have that.

正常的 JDBC 實踐是在 最短 可能的范圍內(nèi)獲取 ConnectionStatementResultSet.通常的做法是將多行映射到一個 List 或者可能是一個 Map 并猜猜是什么,他們 確實 有一個 Iterator.

The normal JDBC practice is that you acquire Connection, Statement and ResultSet in the shortest possible scope. The normal practice is also that you map multiple rows into a List or maybe a Map and guess what, they do have an Iterator.

public List<Data> list() throws SQLException {
    List<Data> list = new ArrayList<Data>();

    try (
        Connection connection = database.getConnection();
        Statement statement = connection.createStatement("SELECT id, name, value FROM data");
        ResultSet resultSet = statement.executeQuery();
    ) {
        while (resultSet.next()) {
            list.add(map(resultSet));
        }
    }

    return list;
}

private Data map(ResultSet resultSet) throws SQLException {
    Data data = new Data(); 
    data.setId(resultSet.getLong("id"));
    data.setName(resultSet.getString("name"));
    data.setValue(resultSet.getInteger("value"));
    return data;
}

如下使用:

List<Data> list = dataDAO.list(); 
int count = list.size(); // Easy as that.
Iterator<Data> iterator = list.iterator(); // There is your Iterator.

不要像最初想要的那樣將昂貴的數(shù)據(jù)庫資源傳遞到 DAO 層之外.有關常規(guī) JDBC 實踐和 DAO 模式的更多基本示例,您可以找到 這篇文章很有用.

Do not pass expensive DB resources outside the DAO layer like you initially wanted to do. For more basic examples of normal JDBC practices and the DAO pattern you may find this article useful.

這篇關于由 ResultSet 支持的 Java 迭代器的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權益,請聯(lián)系我們刪除處理,感謝您的支持!

相關文檔推薦

Java Remove Duplicates from an Array?(Java從數(shù)組中刪除重復項?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復調(diào)用失敗來自服務器的意外響應:在 Android 工作室中未經(jīng)授權)
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
AES Error: Given final block not properly padded(AES 錯誤:給定的最終塊未正確填充)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: 免费观看黄色av | 一二区视频 | 天堂中文av | 精品国产一区二区在线观看 | 国产欧美日韩在线视频 | xxxx性欧美| 精品视频免费在线观看 | 日韩影音 | 性视频网址 | 午夜精品视频在线 | 四虎影视最新地址 | 国产又粗又猛 | 日韩三级视频 | 好色网站| 精品粉嫩小bbwbbwbbw | 91成人在线视频 | 日韩精品免费观看 | 99re国产| 国产超碰在线 | 91视频成人 | 又色又爽又黄18网站 | 亚洲综合五月 | 免费一级黄色 | 亚洲精品乱码久久久久久 | 国产精品一区二区三区四区 | 中文字幕第一区 | 美日韩在线视频 | 久热在线视频 | 国产aⅴ爽av久久久久成人 | 国产寡妇亲子伦一区二区三区四区 | 亚洲 欧美 激情 另类 校园 | 51成人做爰www免费看网站 | 日日夜夜人人 | 日韩国产精品一区二区 | 久草资源在线 | 一区二区av | 97cao| 天堂网久久 | 日韩资源在线 | 一级片在线免费观看 | 激情综合婷婷 |