[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[jfriends-ml 11182] Re: EJB-QL と Dynamic Query (2/3)



  高橋(智)です。
  Beanの実装で、JDBCドライバによっては問題が発生すると思う部分がありましたので、
以下のように変更しておきます。
      ------------------------------------------------------------------------
      DataSource ds = (DataSource)context.lookup("java:comp/env/jdbc/MySQL");
      ArrayList ids = new ArrayList();
      try {
        conn = ds.getConnection();
        stmt = conn.createStatement();
        rs = stmt.executeQuery(sql);
        while(rs.next())
        {
          int id = rs.getInt("ID");
          ids.add(new Integer(id));
        }
      }
      finally {
        // 省略 : close rs, stmt, conn ...
      }
      Iterator iter = ids.iterator();
      while( iter.hasNext() )
      {
        Integer pk = (Integer)iter.next();
        BookRemote book = bookRemoteHome.findByPrimaryKey(pk);
        array.add(book.getHandle());
      }
      ------------------------------------------------------------------------


--
高橋智宏

"TAKAHASHI, Tomohiro" wrote:
snip
>       DataSource ds = (DataSource)context.lookup("java:comp/env/jdbc/MySQL");
>       try {
>         conn = ds.getConnection();
>         stmt = conn.createStatement();
>         rs = stmt.executeQuery(sql);
>         while(rs.next())
>         {
>           int id = rs.getInt("ID");
>           BookRemote book = bookRemoteHome.findByPrimaryKey(new Integer(id));
>           array.add(book.getHandle());
>         }
>       }
>       finally {
>         if( rs != null ) {
>           try { rs.close(); } catch (SQLException ex) {}
>         }
>         if( stmt != null ) {
>           try { stmt.close(); } catch (SQLException ex) {}
>         }
>         if( conn != null ) {
>           try { conn.close(); } catch (SQLException ex) {}
>         }
>       }
snip