Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jflight/db/AbstractDAO.java


1   /**
2    * AbstractDAO.java
3    *
4    * @author Created by Ingo Koerber
5    */
6   
7   package jflight.db;
8   
9   import java.util.*;
10  import java.sql.*;
11  import jflight.tools.*;
12  
13  /**
14   *  the base class for all Data access objects
15   */
16  public abstract class AbstractDAO
17  {
18    protected abstract String getPkColumnName();
19    protected abstract String getTableName();
20    protected abstract Vector load(PreparedStatement ps) throws SQLException;
21    
22    public Vector fields = new Vector();
23  
24    protected Vector getFieldMap(){return fields;};
25  
26    protected String getCreateTableStmt()
27    {
28      StringBuffer buf = new StringBuffer("create table");
29      buf.append(" ").append(getTableName()).append(" (");
30      Vector fields = getFieldMap();
31  //    Iterator iterator = fields.iterator();
32  //    while(iterator.hasNext()) {
33  //      String[] mapping = (String[])iterator.next();
34      for( int cnt=0;; cnt++ )
35      {
36              try {
37                  String[] mapping = (String[])fields.elementAt(cnt);
38            buf.append(mapping[0]).append("=?").append(", ");
39              } catch (Exception e) {
40                  break;
41              }
42      }
43      buf.append(getPkColumnName()).append(" integer)");
44      return buf.toString();
45    }
46    
47    protected String getUpdateStmt()
48    {
49      JfStringBuffer buf = new JfStringBuffer("update");
50      buf.append(" ").append(getTableName()).append(" set ");
51      Vector fields = getFieldMap();
52  //    Iterator iterator = fields.iterator();
53  //    while(iterator.hasNext()) {
54  //      String[] mapping = (String[])iterator.next();
55      for( int cnt=0;; cnt++ )
56      {
57              try {
58                  String[] mapping = (String[])fields.elementAt(cnt);
59            buf.append(mapping[0]).append("=?").append(", ");
60              } catch (Exception e) {
61                  break;
62              }
63      }
64      buf.deleteCharAt(buf.lastIndexOf(", "));
65          // workaround for jdk 1.1:
66          // remove the ",": Ingo: ist that ok???
67  /*
68          String str1, str2;
69          str1 = new String(buf);
70          int index;
71          index = str1.lastIndexOf(", ");
72          if( index != -1 ) {
73              str2 = new String(str1.substring(0,index-1));
74              str2.concat(str1.substring(index+1, str1.length()-1));
75              buf = new StringBuffer(str2);
76          }
77  */
78          buf.append("where ").append(getPkColumnName()).append("=?");
79      return buf.toString();
80    }
81    
82    /**
83     * Method getInsertStmt
84     *
85     * @return   the insert statement which is necessary to insert an object
86     * in the database
87     *
88     */
89    protected String getInsertStmt()
90    {
91      StringBuffer buf = new StringBuffer("insert into ");
92      buf.append(getTableName()).append(" values(");
93      Vector fields = getFieldMap();
94      for(int i=0;i<fields.size();i++)
95        buf.append("?, ");
96      
97      buf.append("?)");
98      return buf.toString();
99    }
100 
101   /**
102    * Method createTable create the necessary table for this object type
103    *
104    */
105   protected void createTable()
106   {
107     try
108     {
109       Connection conn = ConnectionManager.getInstance().getConnection();
110       Statement statement = conn.createStatement();
111       statement.execute(this.getCreateTableStmt());
112     }
113     catch (SQLException e) {}
114   }
115 
116   /**
117    * Method getNextID gets the next valid primary key
118    *
119    * @param    conn                a  Connection to database
120    *
121    * @return   the next valid primary key, for this object type
122    *
123    * @throws   SQLException
124    *
125    */
126   protected int getNextID(Connection conn)
127     throws SQLException
128   {
129     int ret = 0;
130     Statement statement = conn.createStatement();
131     ResultSet rSet = statement.executeQuery("select max(" + this.getPkColumnName() + ") from " + this.getTableName());
132     if(rSet.next()) ret = rSet.getInt(1);
133     rSet.close();
134     statement.close();
135     return ret+1;
136   }
137   
138   /**
139    * Method loadAll loads all object from database
140    *
141    * @return   a Vector of objects
142    *
143    */
144   public Vector loadAll()
145   {
146     Vector all = new Vector();
147     Connection conn = null;
148     
149     try
150     {
151       conn = ConnectionManager.getInstance().getConnection();
152       PreparedStatement statement = conn.prepareStatement("select * from " + this.getTableName());
153       all = load(statement);
154       statement.close();
155       conn.commit();
156     }
157     catch (SQLException e)
158     {
159       e.printStackTrace();
160     }
161     finally
162     {
163       try
164       {
165         conn.rollback();
166         conn.close();
167       }
168       catch (SQLException e) {e.printStackTrace();}
169     }
170     return all;
171   }
172 
173   /**
174    * Method findDOAByKey find the object with specified primary key in database
175    *
176    * @param    pk                  a primary key
177    *
178    * @return   an Object with the specified pk
179    *
180    */
181   public Object findDOAByKey(int pk)
182   {
183     Vector daos = new Vector();
184     Connection conn = null;
185     try
186     {
187       conn = ConnectionManager.getInstance().getConnection();
188       PreparedStatement statement =
189         conn.prepareStatement("select * from " + this.getTableName() +
190                     " where " + this.getPkColumnName() + " = ?");
191       statement.setInt(1,pk);
192       daos = load(statement);
193       statement.close();
194       conn.commit();
195     }
196     catch (SQLException e)
197     {
198       e.printStackTrace();
199     }
200     finally
201     {
202       try
203       {
204         conn.rollback();
205         conn.close();
206       }
207       catch (SQLException e) {}
208     }
209     if(daos.isEmpty()) return null;
210     return (Object)daos.elementAt(0);
211   }
212 
213   /**
214    * Method findObjects in database
215    *
216    * @param    whereClause         clause in sql statement after: select * from table where
217    * @param    queryParameters     an Object[] with retrieved objects
218    *
219    * @return   an Object
220    *
221    */
222   public Object findObjects(String whereClause, Object[] queryParameters)
223   {
224     Vector objects = new Vector();
225     Connection conn = null;
226     try
227     {
228       conn = ConnectionManager.getInstance().getConnection();
229       String sql = "select * from " + this.getTableName() + " where " + whereClause;
230       PreparedStatement statement =
231         conn.prepareStatement(sql);
232       for(int i=0;i<queryParameters.length;i++)
233         statement.setObject(i+1,queryParameters[i]);
234       objects = load(statement);
235       statement.close();
236       conn.commit();
237     }
238     catch (SQLException e)
239     {
240       e.printStackTrace();
241     }
242     finally
243     {
244       try
245       {
246         conn.rollback();
247         conn.close();
248       }
249       catch (SQLException e) {}
250     }
251     if(objects.isEmpty()) return null;
252     return objects.elementAt(0);
253   }
254 
255 }
256