第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

我應(yīng)該如何使用jdbc來嘗試使用資源呢?

我應(yīng)該如何使用jdbc來嘗試使用資源呢?

動(dòng)漫人物 2019-07-27 15:15:12
我應(yīng)該如何使用jdbc來嘗試使用資源呢?我有一種使用JDBC從數(shù)據(jù)庫中獲取用戶的方法:public List<User> getUser(int userId) {     String sql = "SELECT id, name FROM users WHERE id = ?";     List<User> users = new ArrayList<User>();     try {         Connection con = DriverManager.getConnection(myConnectionURL);         PreparedStatement ps = con.prepareStatement(sql);          ps.setInt(1, userId);         ResultSet rs = ps.executeQuery();         while(rs.next()) {             users.add(new User(rs.getInt("id"), rs.getString("name")));         }         rs.close();         ps.close();         con.close();     } catch (SQLException e) {         e.printStackTrace();     }     return users;}如何使用Java 7試著用資源來改進(jìn)這個(gè)代碼?我嘗試了下面的代碼,但是它使用了很多try塊,而不改進(jìn)可讀性太多了。我應(yīng)該用try-with-resources以另一種方式?public List<User> getUser(int userId) {     String sql = "SELECT id, name FROM users WHERE id = ?";     List<User> users = new ArrayList<>();     try {         try (Connection con = DriverManager.getConnection(myConnectionURL);              PreparedStatement ps = con.prepareStatement(sql);) {             ps.setInt(1, userId);             try (ResultSet rs = ps.executeQuery();) {                 while(rs.next()) {                     users.add(new User(rs.getInt("id"), rs.getString("name")));                 }             }         }     } catch (SQLException e) {         e.printStackTrace();     }     return users;}
查看完整描述

3 回答

?
神不在的星期二

TA貢獻(xiàn)1963條經(jīng)驗(yàn) 獲得超6個(gè)贊

我意識(shí)到這很久以前就已經(jīng)回答了,但我想提出一種額外的方法,以避免嵌套的使用資源的嘗試雙塊。

public List<User> getUser(int userId) {
    try (Connection con = DriverManager.getConnection(myConnectionURL);
         PreparedStatement ps = createPreparedStatement(con, userId); 
         ResultSet rs = ps.executeQuery()) {

         // process the resultset here, all resources will be cleaned up

    } catch (SQLException e) {
        e.printStackTrace();
    }}private PreparedStatement createPreparedStatement(Connection con, int userId) throws SQLException {
    String sql = "SELECT id, username FROM users WHERE id = ?";
    PreparedStatement ps = con.prepareStatement(sql);
    ps.setInt(1, userId);
    return ps;}




查看完整回答
反對(duì) 回復(fù) 2019-07-28
?
蝴蝶刀刀

TA貢獻(xiàn)1801條經(jīng)驗(yàn) 獲得超8個(gè)贊

在您的示例中不需要進(jìn)行外部嘗試,所以您至少可以從3下降到2,而且也不需要關(guān)閉;在資源列表的末尾。使用兩個(gè)try塊的優(yōu)點(diǎn)是,您的所有代碼都是預(yù)先出現(xiàn)的,因此您不必引用單獨(dú)的方法:

public List<User> getUser(int userId) {
    String sql = "SELECT id, username FROM users WHERE id = ?";
    List<User> users = new ArrayList<>();
    try (Connection con = DriverManager.getConnection(myConnectionURL);
         PreparedStatement ps = con.prepareStatement(sql)) {
        ps.setInt(1, userId);
        try (ResultSet rs = ps.executeQuery()) {
            while(rs.next()) {
                users.add(new User(rs.getInt("id"), rs.getString("name")));
            }
        }
    } catch (SQLException e) {
        e.printStackTrace();
    }
    return users;}



查看完整回答
反對(duì) 回復(fù) 2019-07-28
?
楊__羊羊

TA貢獻(xiàn)1943條經(jīng)驗(yàn) 獲得超7個(gè)贊

下面是一種使用lambdas和JDK 8供應(yīng)商來滿足外部所有嘗試的簡潔方法:

    try (Connection con = DriverManager.getConnection(JDBC_URL, prop);
            PreparedStatement stmt = ((Supplier<PreparedStatement>)() -> {
                try {
                  PreparedStatement s = con.prepareStatement(
                        "SELECT userid, name, features FROM users WHERE userid = ?");
                  s.setInt(1, userid);
                  return s;
                } catch (SQLException e) { throw new RuntimeException(e); }
            }).get();
          ResultSet resultSet = stmt.executeQuery()) {
    }



查看完整回答
反對(duì) 回復(fù) 2019-07-28
  • 3 回答
  • 0 關(guān)注
  • 313 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)