KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > caucho > jslib > Database


1 /*
2  * Copyright (c) 1998-2006 Caucho Technology -- all rights reserved
3  *
4  * This file is part of Resin(R) Open Source
5  *
6  * Each copy or derived work must preserve the copyright notice and this
7  * notice unmodified.
8  *
9  * Resin Open Source is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * Resin Open Source is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, or any warranty
17  * of NON-INFRINGEMENT. See the GNU General Public License for more
18  * details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with Resin Open Source; if not, write to the
22  * Free SoftwareFoundation, Inc.
23  * 59 Temple Place, Suite 330
24  * Boston, MA 02111-1307 USA
25  *
26  * @author Scott Ferguson
27  */

28
29 package com.caucho.jslib;
30
31 import com.caucho.util.Exit;
32 import com.caucho.util.ExitListener;
33
34 import javax.naming.InitialContext JavaDoc;
35 import javax.sql.DataSource JavaDoc;
36 import java.sql.Connection JavaDoc;
37 import java.sql.PreparedStatement JavaDoc;
38 import java.sql.ResultSet JavaDoc;
39 import java.sql.SQLException JavaDoc;
40 import java.sql.Statement JavaDoc;
41
42 /**
43  * Encapsulates a database connection for JavaScript.
44  *
45  * <code><pre>
46  * var conn = new Database("jdbc/test");
47  * var rs = conn.query("select NAME from HOUSES");
48  *
49  * while (rs.next())
50  * out.writeln(rs.get(1) + "&lt;br/>");
51  *
52  * conn.close();
53  * </pre></code>
54  */

55 public class Database {
56   private DataSource JavaDoc dataSource;
57   private Connection JavaDoc conn;
58   private Statement JavaDoc stmt;
59   private ResultSet JavaDoc rs;
60
61   /**
62    * Creates a new database looking up the DataSource in JNDI.
63    *
64    * @param name jndi name to the data source
65    */

66   public Database(String JavaDoc name)
67     throws Exception JavaDoc
68   {
69     InitialContext JavaDoc cxt = null;
70
71     try {
72       cxt = new InitialContext JavaDoc();
73     } catch (Exception JavaDoc e) {
74     }
75     
76     try {
77       if (cxt != null)
78         dataSource = (DataSource JavaDoc) cxt.lookup(name);
79     } catch (Exception JavaDoc e) {
80     }
81
82     try {
83       if (dataSource == null && cxt != null)
84         dataSource = (DataSource JavaDoc) cxt.lookup("java:comp/env/" + name);
85     } catch (Exception JavaDoc e) {
86     }
87
88     try {
89       if (dataSource != null && cxt != null)
90         dataSource = (DataSource JavaDoc) cxt.lookup("java:comp/env/jdbc/" + name);
91     } catch (Exception JavaDoc e) {
92     }
93
94     if (dataSource == null)
95       throw new SQLException JavaDoc("no data source: " + name);
96
97     // Automatically reclaim the database when the script ends
98
Exit.addExit(exitHandler, this);
99   }
100
101   /**
102    * Execute the sql as a query.
103    */

104   public ResultSet JavaDoc query(String JavaDoc sql)
105     throws SQLException JavaDoc
106   {
107     if (rs != null) {
108       ResultSet JavaDoc rs = this.rs;
109       this.rs = null;
110       rs.close();
111     }
112     
113     Statement JavaDoc stmt = getStatement();
114
115     rs = stmt.executeQuery(sql);
116
117     return rs;
118   }
119
120   /**
121    * Execute the sql as a query.
122    */

123   public int update(String JavaDoc sql)
124     throws SQLException JavaDoc
125   {
126     if (rs != null) {
127       ResultSet JavaDoc rs = this.rs;
128       this.rs = null;
129       rs.close();
130     }
131
132     Connection JavaDoc conn = dataSource.getConnection();
133
134     try {
135       Statement JavaDoc stmt = conn.createStatement();
136     
137       int count = stmt.executeUpdate(sql);
138
139       stmt.close();
140
141       return count;
142     } finally {
143       conn.close();
144     }
145   }
146
147   /**
148    * Returns the JDBC DataSource. Applications that
149    * need direct access to the data source.
150    */

151   public DataSource JavaDoc getDataSource()
152     throws SQLException JavaDoc
153   {
154     return dataSource;
155   }
156
157   /**
158    * Returns the JDBC Connection for the database. Applications that need
159    * direct access to the Connection can use this.
160    */

161   public Connection JavaDoc getConnection()
162     throws SQLException JavaDoc
163   {
164     if (conn == null) {
165       conn = dataSource.getConnection();
166     }
167
168     return conn;
169   }
170
171   /**
172    * Commits the current connection.
173    */

174   public void commit()
175     throws SQLException JavaDoc
176   {
177     if (this.conn != null) {
178       Connection JavaDoc conn = this.conn;
179       this.conn = null;
180       this.stmt = null;
181       this.rs = null;
182       conn.close();
183     }
184   }
185
186   /**
187    * Returns the JDBC Statement for the database. Applications that
188    * need direct access to the Java Statement can use this.
189    */

190   public Statement JavaDoc getStatement()
191     throws SQLException JavaDoc
192   {
193     if (stmt == null) {
194       Connection JavaDoc conn = getConnection();
195     
196       stmt = conn.createStatement();
197     }
198
199     return stmt;
200   }
201
202   /**
203    * Returns the JDBC Statement for the database. Applications that
204    * need direct access to the Java Statement can use this.
205    */

206   public PreparedStatement JavaDoc prepare(String JavaDoc sql)
207     throws SQLException JavaDoc
208   {
209     Connection JavaDoc conn = getConnection();
210     
211     return conn.prepareStatement(sql);
212   }
213
214   /**
215    * Close the connection. Automatically closes the ResultSet, Statement
216    * and Connection.
217    */

218   public void close()
219     throws SQLException JavaDoc
220   {
221     try {
222       if (rs != null) {
223         ResultSet JavaDoc rs = this.rs;
224         this.rs = null;
225         rs.close();
226       }
227       
228       if (stmt != null) {
229         Statement JavaDoc stmt = this.stmt;
230         this.stmt = null;
231         stmt.close();
232       }
233     } finally {
234       this.stmt = null;
235       this.rs = null;
236       if (conn != null) {
237         Connection JavaDoc conn = this.conn;
238         this.conn = null;
239         conn.close();
240       }
241     }
242   }
243
244   /**
245    * Listens for the exit event. When Database is used in JavaScript,
246    * the database will be automatically closed on exit.
247    */

248   private static ExitListener exitHandler = new ExitListener() {
249     public void handleExit(Object JavaDoc o)
250     {
251       Database db = (Database) o;
252
253       try {
254     db.close();
255       } catch (SQLException JavaDoc e) {
256       }
257     }
258   };
259 }
260
Popular Tags