KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > easybeans > tests > common > db > DBManager


1 /**
2  * EasyBeans
3  * Copyright (C) 2006 Bull S.A.S.
4  * Contact: easybeans@objectweb.org
5  *
6  * This library is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or any later version.
10  *
11  * This library is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this library; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
19  * USA
20  *
21  * --------------------------------------------------------------------------
22  * $Id: DBManager.java 100 2006-03-02 16:13:54Z pinheirg $
23  * --------------------------------------------------------------------------
24  */

25 package org.objectweb.easybeans.tests.common.db;
26
27 import java.io.PrintWriter JavaDoc;
28 import java.io.Serializable JavaDoc;
29 import java.sql.Connection JavaDoc;
30 import java.sql.DriverManager JavaDoc;
31 import java.sql.SQLException JavaDoc;
32 import java.util.Hashtable JavaDoc;
33
34 import javax.sql.DataSource JavaDoc;
35
36 import org.objectweb.easybeans.log.JLog;
37 import org.objectweb.easybeans.log.JLogFactory;
38
39 /**
40  * Manages the connection with the database. It is make possible to open and
41  * close connections with the database and also, use statements.
42  * @author Gisele Pinheiro Souza
43  * @author Eduardo Studzinski Estima de Castro
44  */

45 public class DBManager implements DataSource JavaDoc, Serializable JavaDoc {
46
47     /**
48      * The serial version.
49      */

50     private static final long serialVersionUID = 1208377914757260026L;
51
52     /**
53      * A Constant that holds the name of the JDBC driver class.
54      */

55     public static final Integer JavaDoc JDBC_DRIVER = new Integer JavaDoc(1);
56
57     /**
58      * Constant that holds the database URL.
59      */

60     public static final Integer JavaDoc URL = new Integer JavaDoc(2);
61
62     /**
63      * Constant that holds the user login used to connect with the database.
64      */

65     public static final Integer JavaDoc LOGIN = new Integer JavaDoc(3);
66
67     /**
68      * Constant that holds the user passwd used to connect with the database.
69      */

70     public static final Integer JavaDoc PASSWD = new Integer JavaDoc(4);
71
72     /**
73      * The database URL.
74      */

75     private String JavaDoc strURL = null;
76
77     /**
78      * The user login used to connect with the database.
79      */

80     private String JavaDoc strLogin = null;
81
82     /**
83      * The user passwd used to connect with the database.
84      */

85     private String JavaDoc strPasswd = null;
86
87     /**
88      * The JDBC driver used to connect with the database.
89      */

90     private String JavaDoc strJDBCDriver = null;
91
92     /**
93      * Logger.
94      */

95     private static JLog logger = JLogFactory.getLog(DBManager.class);
96
97     /**
98      * Creates a instance of DBManager.Loads the database driver and sets the
99      * parameters.
100      * @param dbParameters parameters used to make the connection.
101      * @throws ClassNotFoundException if the jdbc driver class was not found.
102      */

103     public DBManager(final Hashtable JavaDoc<Integer JavaDoc, String JavaDoc> dbParameters) throws ClassNotFoundException JavaDoc {
104
105         Class.forName(dbParameters.get(JDBC_DRIVER));
106         strURL = dbParameters.get(URL);
107         strJDBCDriver = dbParameters.get(JDBC_DRIVER);
108         strLogin = dbParameters.get(LOGIN);
109         strPasswd = dbParameters.get(PASSWD);
110     }
111
112     /**
113      * Starts the connection with the database.
114      * @return the datasource connection.
115      * @throws SQLException if a database access error occurs
116      */

117     public Connection JavaDoc getConnection() throws SQLException JavaDoc {
118         try{
119             Class.forName(strJDBCDriver);
120         }catch(Exception JavaDoc e){
121             logger.debug("Cannot uses the driver {0}", e);
122         }
123         return DriverManager.getConnection(strURL, strLogin, strPasswd);
124
125     }
126
127     /**
128      * Starts the connection with the database.
129      * @param username the name used to connect with the database.
130      * @param password the user password.
131      * @return the datasource connection.
132      * @throws SQLException if a database access error occurs
133      */

134     public Connection JavaDoc getConnection(final String JavaDoc username, final String JavaDoc password) throws SQLException JavaDoc {
135         return DriverManager.getConnection(strURL, username, password);
136     }
137
138     /**
139      * Not used.
140      * @return not used
141      * @throws SQLException if a database access error occurs
142      */

143     public int getLoginTimeout() throws SQLException JavaDoc {
144         throw new SQLException JavaDoc("Not implemented");
145     }
146
147     /**
148      * Not used.
149      * @return not used
150      * @throws SQLException if a database access error occurs
151      */

152     public PrintWriter JavaDoc getLogWriter() throws SQLException JavaDoc {
153         throw new SQLException JavaDoc("Not implemented");
154     }
155
156     /**
157      * Not used.
158      * @param seconds not used
159      * @throws SQLException if a database access error occurs
160      */

161     public void setLoginTimeout(final int seconds) throws SQLException JavaDoc {
162         throw new SQLException JavaDoc("Not implemented");
163     }
164
165     /**
166      * Not used.
167      * @param out not used.
168      * @throws SQLException if a database access error occurs
169      */

170     public void setLogWriter(final PrintWriter JavaDoc out) throws SQLException JavaDoc {
171         throw new SQLException JavaDoc("Not implemented");
172     }
173
174 }
175
Popular Tags