KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > net > killingar > forum > internal > managers > AbstractManager


1 /* Copyright 2000-2005 Anders Hovmöller
2  *
3  * The person or persons who have associated their work with
4  * this document (the "Dedicator") hereby dedicate the entire
5  * copyright in the work of authorship identified below (the
6  * "Work") to the public domain.
7  *
8  * Dedicator makes this dedication for the benefit of the
9  * public at large and to the detriment of Dedicator's heirs
10  * and successors. Dedicator intends this dedication to be an
11  * overt act of relinquishment in perpetuity of all present
12  * and future rights under copyright law, whether vested or
13  * contingent, in the Work. Dedicator understands that such
14  * relinquishment of all rights includes the relinquishment of
15  * all rights to enforce (by lawsuit or otherwise) those
16  * copyrights in the Work.
17  *
18  * Dedicator recognizes that, once placed in the public
19  * domain, the Work may be freely reproduced, distributed,
20  * transmitted, used, modified, built upon, or otherwise
21  * exploited by anyone for any purpose, commercial or non-
22  * commercial, and in any way, including by methods that have
23  * not yet been invented or conceived.
24  */

25
26 /**
27  * Base class for managers.
28  */

29
30 package net.killingar.forum.internal.managers;
31
32 import javax.naming.Context JavaDoc;
33 import javax.naming.InitialContext JavaDoc;
34 import javax.naming.NamingException JavaDoc;
35 import javax.sql.DataSource JavaDoc;
36 import java.sql.Connection JavaDoc;
37 import java.sql.ResultSet JavaDoc;
38 import java.sql.SQLException JavaDoc;
39 import java.sql.Statement JavaDoc;
40
41 public abstract class AbstractManager implements java.io.Serializable JavaDoc
42 {
43     private static final String JavaDoc datasource = "java:comp/env/jdbc/SKForumDS";
44     private static Context JavaDoc ctx;
45     private static DataSource JavaDoc ds;
46
47   protected ForumManager manager;
48
49
50   /**
51    * Sets the ForumManager variable.
52    */

53   public void initialize(ForumManager m)
54   {
55     manager = m;
56   }
57
58     /**
59      * Get a new connection.
60      */

61     public static Connection JavaDoc getNewConnection() throws SQLException JavaDoc
62     {
63         try
64         {
65             if (ctx == null)
66                 ctx = new InitialContext JavaDoc();
67             if (ds == null)
68         ds = (DataSource JavaDoc) ctx.lookup(datasource);
69             return ds.getConnection();
70         }
71         catch (NamingException JavaDoc e)
72         {
73             e.printStackTrace();
74             throw new SQLException JavaDoc(e.getMessage());
75         }
76     }
77
78     /**
79      * Close objects
80      */

81     public static void closeAll(Connection JavaDoc c, Statement JavaDoc statement, ResultSet JavaDoc result) throws SQLException JavaDoc
82     {
83         if (result != null)result.close();
84         if (statement != null)statement.close();
85         if (c != null)c.close();
86     }
87     public static void closeAll(Connection JavaDoc c, Statement JavaDoc statement) throws SQLException JavaDoc { closeAll(c, statement, null); }
88     public static void closeAll(Connection JavaDoc c) throws SQLException JavaDoc { closeAll(c, null, null); }
89
90
91   /**
92    * Event handler.
93    */

94   public void actionPerformed(String JavaDoc action){}
95
96     public ForumManager getManager() { return manager; }
97 }
Popular Tags