KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > DBContext


1 /*
2  * Copyright 2000-2001,2004 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16
17 import java.sql.*;
18 import java.io.Serializable JavaDoc;
19 import java.io.*;
20
21 import org.apache.velocity.context.AbstractContext;
22 import org.apache.velocity.context.Context;
23
24 /**
25  * Example context impl that uses a database to store stuff :)
26  *
27  * yes, this is silly
28  *
29  * expects a mysql db test with table
30  *
31  * CREATE TABLE contextstore (
32  * k varchar(100),
33  * val blob
34  * );
35  *
36  * very fragile, crappy code.... just a demo!
37  *
38  * @author <a HREF="mailto:geirm@optonline.net">Geir Magnusson Jr.</a>
39  * @version $Id: DBContext.java,v 1.2.14.1 2004/03/04 00:18:29 geirm Exp $
40  */

41
42 public class DBContext extends AbstractContext
43 {
44     Connection conn = null;
45
46     public DBContext()
47     {
48         super();
49         setup();
50     }
51
52     public DBContext( Context inner )
53     {
54         super( inner );
55         setup();
56     }
57
58     /**
59      * retrieves a serialized object from the db
60      * and returns the living instance to the
61      * caller.
62      */

63     public Object JavaDoc internalGet( String JavaDoc key )
64     {
65         try
66         {
67             String JavaDoc data = null;
68
69             String JavaDoc sql = "SELECT k, val FROM contextstore WHERE k ='"+key+"'";
70             
71             Statement s = conn.createStatement();
72
73             ResultSet rs = s.executeQuery( sql );
74
75             if(rs.next())
76                data = rs.getString("val");
77             
78             rs.close();
79             s.close();
80             
81             ObjectInputStream in = new ObjectInputStream( new ByteArrayInputStream( data.getBytes() ));
82
83             Object JavaDoc o = in.readObject();
84
85             in.close();
86
87             return o;
88         }
89         catch(Exception JavaDoc e)
90         {
91             System.out.println("internalGet() : " + e );
92         }
93
94         return null;
95     }
96
97     /**
98      * Serializes and stores an object in the database.
99      * This is really a hokey way to do it, and will
100      * cause problems. The right way is to use a
101      * prepared statement...
102      */

103     public Object JavaDoc internalPut( String JavaDoc key, Object JavaDoc value )
104     {
105         try
106         {
107             ByteArrayOutputStream baos = new ByteArrayOutputStream();
108             ObjectOutputStream out = new ObjectOutputStream( baos );
109             
110             out.writeObject( value );
111             String JavaDoc data = baos.toString();
112
113             out.close();
114             baos.close();
115           
116             Statement s = conn.createStatement();
117
118             s.executeUpdate( "DELETE FROM contextstore WHERE k = '" + key + "'" );
119             s.executeUpdate( "INSERT INTO contextstore (k,val) values ('"+key+"','" + data + "')" );
120
121             s.close();
122         }
123         catch(Exception JavaDoc e)
124         {
125             System.out.println("internalGet() : " + e );
126         }
127
128         return null;
129     }
130
131     /**
132      * Not implementing. Not required for Velocity core
133      * operation, so not bothering. As we say above :
134      * "very fragile, crappy code..."
135      */

136     public boolean internalContainsKey(Object JavaDoc key)
137     {
138         return false;
139     }
140     
141     /**
142      * Not implementing. Not required for Velocity core
143      * operation, so not bothering. As we say above :
144      * "very fragile, crappy code..."
145      */

146     public Object JavaDoc[] internalGetKeys()
147     {
148         return null;
149     }
150
151     /**
152      * Not implementing. Not required for Velocity core
153      * operation, so not bothering. As we say above :
154      * "very fragile, crappy code..."
155      */

156     public Object JavaDoc internalRemove(Object JavaDoc key)
157     {
158         return null;
159     }
160
161
162     private void setup()
163     {
164         try
165         {
166             Class.forName("org.gjt.mm.mysql.Driver").newInstance();
167             conn = DriverManager.getConnection("jdbc:mysql://localhost/test?user=root");
168         }
169         catch (Exception JavaDoc e)
170         {
171             System. out.println(e);
172         }
173       
174         return;
175     }
176 }
177
178
Popular Tags