?? database.java
字號:
/* * Copyright (c) 1998-2001 Caucho Technology -- all rights reserved * * Caucho Technology permits redistribution, modification and use * of this file in source and binary form ("the Software") under the * Caucho Developer Source License ("the License"). The following * conditions must be met: * * 1. Each copy or derived work of the Software must preserve the copyright * notice and this notice unmodified. * * 2. Redistributions of the Software in source or binary form must include * an unmodified copy of the License, normally in a plain ASCII text * * 3. The names "Resin" or "Caucho" are trademarks of Caucho Technology and * may not be used to endorse products derived from this software. * "Resin" or "Caucho" may not appear in the names of products derived * from this software. * * 4. Caucho Technology requests that attribution be given to Resin * in any manner possible. We suggest using the "Resin Powered" * button or creating a "powered by Resin(tm)" link to * http://www.caucho.com for each page served by Resin. * * This Software is provided "AS IS," without a warranty of any kind. * ALL EXPRESS OR IMPLIED REPRESENTATIONS AND WARRANTIES, INCLUDING ANY * IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. * * CAUCHO TECHNOLOGY AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES * SUFFERED BY LICENSEE OR ANY THIRD PARTY AS A RESULT OF USING OR * DISTRIBUTING SOFTWARE. IN NO EVENT WILL CAUCHO OR ITS LICENSORS BE LIABLE * FOR ANY LOST REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, * CONSEQUENTIAL, INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND * REGARDLESS OF THE THEORY OF LIABILITY, ARISING OUT OF THE USE OF OR * INABILITY TO USE SOFTWARE, EVEN IF HE HAS BEEN ADVISED OF THE POSSIBILITY * OF SUCH DAMAGES. * * @author Scott Ferguson */package com.caucho.jslib;import java.io.*;import java.sql.*;import java.util.*;import javax.sql.*;import javax.naming.*;import com.caucho.util.*;/** * Encapsulates a database connection for JavaScript. * * <code><pre> * var conn = new Database("jdbc/test"); * var rs = conn.query("select NAME from HOUSES"); * * while (rs.next()) * out.writeln(rs.get(1) + "<br/>"); * * conn.close(); * </pre></code> */public class Database { private DataSource dataSource; private Connection conn; private Statement stmt; private ResultSet rs; /** * Creates a new database looking up the DataSource in JNDI. * * @param name jndi name to the data source */ public Database(String name) throws Exception { InitialContext cxt = null; try { cxt = new InitialContext(); } catch (Exception e) { } try { if (cxt != null) dataSource = (DataSource) cxt.lookup(name); } catch (Exception e) { } try { if (dataSource == null && cxt != null) dataSource = (DataSource) cxt.lookup("java:comp/env/" + name); } catch (Exception e) { } try { if (dataSource != null && cxt != null) dataSource = (DataSource) cxt.lookup("java:comp/env/jdbc/" + name); } catch (Exception e) { } if (dataSource == null) dataSource = com.caucho.sql.DBPool.getPool(name); if (dataSource == null) throw new SQLException("no data source: " + name); // Automatically reclaim the database when the script ends Exit.addExit(exitHandler, this); } /** * Execute the sql as a query. */ public ResultSet query(String sql) throws SQLException { if (rs != null) { ResultSet rs = this.rs; this.rs = null; rs.close(); } Statement stmt = getStatement(); rs = stmt.executeQuery(sql); return rs; } /** * Execute the sql as a query. */ public int update(String sql) throws SQLException { if (rs != null) { ResultSet rs = this.rs; this.rs = null; rs.close(); } Statement stmt = getStatement(); return stmt.executeUpdate(sql); } /** * Returns the JDBC DataSource. Applications that * need direct access to the data source. */ public DataSource getDataSource() throws SQLException { return dataSource; } /** * Returns the JDBC Connection for the database. Applications that need * direct access to the Connection can use this. */ public Connection getConnection() throws SQLException { if (conn == null) conn = dataSource.getConnection(); return conn; } /** * Returns the JDBC Statement for the database. Applications that * need direct access to the Java Statement can use this. */ public Statement getStatement() throws SQLException { if (stmt == null) { Connection conn = getConnection(); stmt = conn.createStatement(); } return stmt; } /** * Close the connection. Automatically closes the ResultSet, Statement * and Connection. */ public void close() throws SQLException { try { if (rs != null) { ResultSet rs = this.rs; this.rs = null; rs.close(); } if (stmt != null) { Statement stmt = this.stmt; this.stmt = null; stmt.close(); } } finally { if (conn != null) { Connection conn = this.conn; this.conn = null; conn.close(); } } } /** * Listens for the exit event. When Database is used in JavaScript, * the database will be automatically closed on exit. */ private static ExitListener exitHandler = new ExitListener() { public void handleExit(Object o) { Database db = (Database) o; try { db.close(); } catch (SQLException e) { } } };}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -