KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openuss > utility > ConnectionFactory


1 /**
2  * Title: OpenUSS - Open Source University Support System
3  * Description: Utility for OpenUSS
4  * Copyright: Copyright (c) B. Lofi Dewanto
5  * Company: University of Muenster
6  * @author B. Lofi Dewanto
7  * @version 1.0
8  */

9 package org.openuss.utility;
10
11 import java.io.*;
12
13 import java.rmi.RemoteException JavaDoc;
14
15 import java.sql.*;
16
17 import java.util.*;
18
19 //import javax.ejb.*;
20

21 import javax.naming.*;
22
23 import javax.rmi.*;
24
25 import javax.sql.*;
26
27 //import javax.transaction.UserTransaction;
28

29
30 /**
31  * Factory for all connections use in OpenUSS.
32  *
33  * @author B. Lofi Dewanto
34  * @version 1.0
35  */

36 public class ConnectionFactory {
37     // Context
38
private static Context initialContext = null;
39
40     /**
41      * @return the connection from the dataSource
42      * @param env. for Jdbc
43      * @exception EJBException Thrown by the method if the dataSource is not found
44      * in the naming.
45      * @exception SQLException may be thrown by dataSource.getConnection()
46      */

47     public static Connection getConnection(String JavaDoc envJdbc)
48                                     throws Exception JavaDoc {//EJBException, SQLException
49
// Do we need to create the initial context?
50
if (initialContext == null) {
51             // Create the initial context
52
try {
53                 initialContext = new InitialContext();
54             } catch (Exception JavaDoc e) {
55                 System.out.println("Error getting initial context from JNDI: " +
56                                    e.toString());
57             }
58         }
59
60         // Get the datasource
61
DataSource dataSource = null;
62
63         try {
64             // Finds DataSource from JNDI
65
dataSource = (DataSource) initialContext.lookup(envJdbc);
66
67             // Return the result
68
return dataSource.getConnection();
69         } catch (Exception JavaDoc e) {
70             System.out.println("Cannot lookup data source " + e.toString());
71             throw new Exception JavaDoc("Cannot lookup data source ");//javax.ejb.EJBException
72
}
73     }
74 }
Popular Tags