The Artima Developer Community
Sponsored Link •

Design Forum
How to write eefficiency database fetching function?

1 reply on 1 page. Most recent reply: Nov 6, 2002 8:16 AM by Carfield Yim

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Carfield Yim

Posts: 32
Nickname: carfield
Registered: Sep, 2002

How to write eefficiency database fetching function? Posted: Nov 6, 2002 8:13 AM
Reply to this message Reply
Advertisement
I would like to write a efficiency database fetching method with reture an iterator so that the actually database read is on dedmand, something like this:

public Iterator static read(Connection con, String policyId) throws SQLException, NotFoundException{
ResultSet rset = null;
final PreparedStatement pstmt = con.prepareStatement(SQL_SELECT_POILCY);
try{
pstmt.setString(1, policyId);
rset = pstmt.executeQuery();
return new Iterator () {
public boolean hasNext() {
return rset.isLast();
}
public Object next() {
if(rset.next()) {
return new withdrawDeduction(policyId, rset.getInt(1), new BigDecimal(rset.getDouble(2)));
else
throw new NoSuchElementException();
}
public void remove() {
throw new UnsupportedOperationException();
}
protected void finalize() throws Throwable{
try{
SQLUtility.cleanup(rset);
SQLUtility.cleanup(pstmt);
}
catch(Throwable t) {}
super.finalize();
}
};
}
}

However, according to "Efficient java", we should not rely on finalize to free resource, any better approach?


Carfield Yim

Posts: 32
Nickname: carfield
Registered: Sep, 2002

Re: How to write eefficiency database fetching function? Posted: Nov 6, 2002 8:16 AM
Reply to this message Reply
Format the source better:

     public Iterator static read(Connection con, String policyId) throws SQLException, NotFoundException{
         ResultSet rset = null;
         final PreparedStatement pstmt = con.prepareStatement(SQL_SELECT_POILCY);
         try{
             pstmt.setString(1, policyId);
             rset = pstmt.executeQuery();
             return new Iterator () {
                 public boolean hasNext() {
                     return rset.isLast();
                 }
                 public Object next()     {
                     if(rset.next()) {
                         return new withdrawDeduction(policyId, rset.getInt(1), new BigDecimal(rset.getDouble(2)));
                     else
                         throw new NoSuchElementException();
                 }
                 public void remove() {
                     throw new UnsupportedOperationException();
                 }
                 protected void finalize() throws Throwable{
                     try{
                        SQLUtility.cleanup(rset);
                        SQLUtility.cleanup(pstmt);
                     }
                     catch(Throwable t) {}
                     super.finalize();
                 } 
             };
         }
     }

Flat View: This topic has 1 reply on 1 page
Topic: VB.NET  --- ARRAYS Previous Topic   Next Topic Topic: impossible to create a Factory Method in Java?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use