KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > JDBCConnection


1 /**
2  * MEDOR: Middleware Enabling Distributed Object Requests
3  *
4  * Copyright (C) 2001-2002 France Telecom R&D
5  * Contact: alexandre.lefebvre@rd.francetelecom.com
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20  *
21  * Initial developers: M. Alia, A. Lefebvre
22  */

23 import org.objectweb.medor.api.MedorException;
24
25 import java.sql.Connection JavaDoc;
26 import java.sql.DriverManager JavaDoc;
27 import java.sql.SQLException JavaDoc;
28
29 /**
30  * Copyright (C) 2001-2002 France Télécom R&D
31  * DTL/ASR
32  * @author <A HREF="mailto:alia.mourad@rd.francetelecom.com><b>
33  * Mourad ALIA
34  * </b></A>
35  */

36
37 /**
38  * This class contains one function.It loads the JDBC driver and returns
39  * a connection object.
40  */

41 public class JDBCConnection {
42     public Connection JavaDoc getConnection(String JavaDoc dbURL, String JavaDoc driver) throws MedorException {
43         try {
44             // Connecting to the JDBC DataSource and recupere the Connection Object
45
System.out.print("Loading driver: " + driver + "...");
46             Class.forName(driver).newInstance();
47             System.out.println("success");
48         } catch (Exception JavaDoc e) {
49             System.out.println("failed. Error: " + e);
50         }
51         // Then we connect to a database using the database url and the driver loaded.
52
System.out.println("Connecting to " + dbURL);
53         try {
54             return DriverManager.getConnection(dbURL);
55         } catch (SQLException JavaDoc sqlexp) {
56             throw new MedorException(sqlexp.getMessage());
57         }
58     }
59 }
60
Popular Tags