View Javadoc

1   /* ==============================================================================
2    *   JDBCAdmin, data management software.
3    *   Copyright (C) 2005  Norsys S.A
4    *
5    *   This library is free software; you can redistribute it and/or
6    *   modify it under the terms of the GNU Lesser General Public
7    *   License as published by the Free Software Foundation; either
8    *   version 2.1 of the License, or (at your option) any later version.
9    *
10   *   This library is distributed in the hope that it will be useful,
11   *   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   *   Lesser General Public License for more details.
14   *
15   *   You should have received a copy of the GNU Lesser General Public
16   *   License along with this library; if not, write to the Free Software
17   *   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   * ==============================================================================
19   */
20  package jdbcadmin.core.tools;
21  
22  import java.sql.Connection;
23  import java.sql.SQLException;
24  
25  import jdbcadmin.core.data.ConnectionInfo;
26  import jdbcadmin.core.exceptions.TechnicalException;
27  
28  /***
29   * Base class for classes which use a database connection<br>
30   * The public methods of the overriding classes <b>must</b> call the {@link #connect} and {@link #diconnect} methods
31   */
32  public abstract class AbstractCnxUser {
33  
34      ///////////////  Attributes //////////////////
35      /*** Informations de connexion à la base de donnée */
36      protected ConnectionInfo conInfos;
37      /*** Connexion à la base, apres fermeture, elle doit être mise à null */
38      protected Connection cnx;
39  
40  
41      /////////////// Constructors ///////////////////
42      /***
43       * Constructor
44       * @param aCnxInfos Connection informations
45       */
46      public AbstractCnxUser (ConnectionInfo aCnxInfos) {
47          conInfos = aCnxInfos;
48      }
49  
50  
51      /////////////// Methods ///////////////////////
52  
53      /***
54       * If not initialized, creates or retrieves the connection
55       * @return <code>true</code> if the connection had to be created/retrieved
56       * (in this case, the calling method <b>must</b> call {@link #disconnect}), <code>false</code> otherwise
57       * @throws TechnicalException error connecting to db.
58       **/
59      protected boolean connect() throws TechnicalException  {
60          if (cnx != null) {
61              return false;
62          } else {
63              cnx = ConnectionFactory.getCnx(conInfos);
64              return true;
65          }
66      }
67  
68      /***
69       * Closes the db connection
70       **/
71      protected void disconnect() {
72          if (cnx != null) {
73              try {
74                  cnx.close();
75                  cnx = null;
76              } catch (SQLException sqle) {
77                  // do nothing
78              }
79          }
80      }
81  
82  
83  }