1    package com.instantbank.collections.ach;
2    
3    import java.sql.Connection;
4    import java.sql.PreparedStatement;
5    import java.sql.ResultSet;
6    import java.sql.SQLException;
7    import java.sql.Statement;
8    import javax.naming.NamingException;
9    import java.util.LinkedList;
10   import java.util.List;
11   import java.io.Serializable;
12   import com.instantbank.collections.util.DataAccess;
13   import com.instantbank.collections.util.LongWrapper;
14   import com.instantbank.collections.util.SqlUtil;
15   
16   public class AchDAO
17       implements Serializable {
18   
19     private AchDAO() { }
20   
21   
22     /**
23      * Use this table to look up all keys for a given company, table and column
24      * Returns a list of LongWrapper objects
25      *
26      * @param sql Description of the Parameter
27      * @return The primaryKeys value
28      * @throws SQLException Description of the Exception
29      * @throws NamingException Description of the Exception
30      */
31     public static List getPrimaryKeys(String sql) throws SQLException, NamingException {
32   
33       DataAccess da = new DataAccess();
34       ResultSet rs = null;
35       PreparedStatement ps = null;
36   
37       List ret = new LinkedList();
38   
39       try {
40         da.connect();
41         ps = da.getConnection().prepareStatement(sql);
42         rs = ps.executeQuery();
43         if(rs != null) {
44           while(rs.next()) {
45             ret.add(new LongWrapper(rs.getLong(1)));
46           }
47         }
48       }
49       finally {
50         SqlUtil.release(da.getConnection(), ps, rs);
51       }
52       return ret;
53     }
54   
55   
56     public static DataObject populateByPrimaryKey(DataObject nakedDO)
57        throws SQLException, NamingException {
58       DataAccess da = new DataAccess();
59       ResultSet rs = null;
60       PreparedStatement ps = null;
61   
62       try {
63         da.connect();
64         ps = da.getConnection().prepareStatement(nakedDO.getFindByPrimaryKeySQL());
65         ps.setLong(1, nakedDO.getId());
66         rs = ps.executeQuery();
67         if(rs == null || !rs.next()) {
68           System.out.println("No data found when populating object " + nakedDO.getId()
69             + ": " + nakedDO.getFindByPrimaryKeySQL());
70         }
71         else {
72           nakedDO.populate(rs);
73         }
74       }
75       finally {
76         SqlUtil.release(da.getConnection(), ps, rs);
77       }
78       return nakedDO;
79     }
80   
81   
82     public static void create(DataObject dataObject)
83        throws SQLException, NamingException {
84   
85       DataAccess da = new DataAccess();
86       PreparedStatement ps = null;
87   
88       try {
89         da.connect();
90         Connection con = da.getConnection();
91         long primaryKey = SqlUtil.nextOracleSequence(con, false,
92           dataObject.getSequenceObjectName());
93         dataObject.setId(primaryKey);
94         ps = con.prepareStatement(dataObject.getCreateSQL());
95         dataObject.prepareCreateStatement(ps);
96         if(ps.executeUpdate() != 1) {
97           String error = "Error creating new row";
98           System.out.println(error);
99           throw new SQLException(error);
100        }
101      }
102      finally {
103        SqlUtil.release(da.getConnection(), ps, null);
104      }
105    }
106  
107  
108    public static void update(DataObject dataObject)
109       throws SQLException, NamingException {
110  
111      DataAccess da = new DataAccess();
112      PreparedStatement ps = null;
113  
114      try {
115        da.connect();
116        Connection con = da.getConnection();
117        ps = con.prepareStatement(dataObject.getUpdateSQL());
118        dataObject.prepareUpdateStatement(ps);
119        if(ps.executeUpdate() != 1) {
120          String error = "Error updating row";
121          System.out.println(error);
122          throw new SQLException(error);
123        }
124      }
125      finally {
126        SqlUtil.release(da.getConnection(), ps, null);
127      }
128    }
129  
130  
131    /**
132     * Returns a list of LongWrapper objects for any query
133     *
134     * @param queryStr Description of the Parameter
135     * @return Description of the Return Value
136     * @throws SQLException Description of the Exception
137     * @throws NamingException Description of the Exception
138     */
139    public static List genericQuery(String queryStr)
140       throws SQLException, NamingException {
141      DataAccess da = new DataAccess();
142      List ret = new LinkedList();
143      Statement st = null;
144      ResultSet rs = null;
145      try {
146        da.connect();
147        Connection con = da.getConnection();
148        st = da.getConnection().createStatement();
149        rs = st.executeQuery(queryStr);
150        if(rs != null) {
151          while(rs.next()) {
152            ret.add(new LongWrapper(rs.getLong(1)));
153          }
154        }
155      }
156      finally {
157        try {
158          if(rs != null) {
159            rs.close();
160          }
161          if(st != null) {
162            st.close();
163          }
164          if(da != null) {
165            da.disconnect();
166          }
167        }
168        catch(Exception e) {
169          e.printStackTrace();
170        }
171      }
172  
173      return ret;
174    }
175  
176  
177    public static void remove(DataObject dataObject)
178       throws SQLException, NamingException {
179  
180      DataAccess da = new DataAccess();
181      PreparedStatement ps = null;
182  
183      try {
184        da.connect();
185        Connection con = da.getConnection();
186        ps = con.prepareStatement(dataObject.getRemoveSQL());
187        dataObject.prepareRemoveStatement(ps);
188        if(ps.executeUpdate() != 1) {
189          String error = "Error removing row";
190          System.out.println(error);
191          throw new SQLException(error);
192        }
193      }
194      finally {
195        SqlUtil.release(da.getConnection(), ps, null);
196      }
197    }
198  
199  }
200