001: /*
002: * BEGIN_HEADER - DO NOT EDIT
003: *
004: * The contents of this file are subject to the terms
005: * of the Common Development and Distribution License
006: * (the "License"). You may not use this file except
007: * in compliance with the License.
008: *
009: * You can obtain a copy of the license at
010: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
011: * See the License for the specific language governing
012: * permissions and limitations under the License.
013: *
014: * When distributing Covered Code, include this CDDL
015: * HEADER in each file and include the License file at
016: * https://open-esb.dev.java.net/public/CDDLv1.0.html.
017: * If applicable add the following below this CDDL HEADER,
018: * with the fields enclosed by brackets "[]" replaced with
019: * your own identifying information: Portions Copyright
020: * [year] [name of copyright owner]
021: */
022:
023: /*
024: * @(#)SQLDataAccess.java
025: * Copyright 2004-2007 Sun Microsystems, Inc. All Rights Reserved.
026: *
027: * END_HEADER - DO NOT EDIT
028: */
029: package org.openesb.tools.extchart.jfchart.data;
030:
031: import org.openesb.tools.extchart.exception.ChartException;
032:
033: import java.sql.Connection;
034: import java.sql.ResultSet;
035: import java.sql.SQLException;
036: import java.sql.Statement;
037: import java.util.logging.Level;
038: import java.util.logging.Logger;
039: import javax.naming.InitialContext;
040: import javax.naming.NamingException;
041: import javax.sql.DataSource;
042: import org.jfree.data.general.Dataset;
043:
044: /**
045: *
046: * @author rdwivedi
047: */
048: public class SQLDataAccess extends DataAccess {
049:
050: String mQuery = null;
051: String mJNDIName = null;
052: private static Logger mLogger = Logger
053: .getLogger("org.openesb.tools.extchart.jfchart.data.SQLDataAccess");
054:
055: /** Creates a new instance of SQLDataAccess */
056: public SQLDataAccess(String jndiName, String dataSetType,
057: String query) {
058: super (dataSetType);
059: mQuery = query;
060: mJNDIName = jndiName;
061:
062: }
063:
064: public Dataset getDataSet() throws ChartException {
065: Dataset set = null;
066: DataSetCreator creator = DataSetCreator
067: .create(getDatasetType());
068: Connection con = null;
069: Statement stmt = null;
070: ResultSet rset = null;
071: try {
072: con = getConnectionJNDI();
073: stmt = con.createStatement();
074: rset = stmt.executeQuery(mQuery);
075: creator.processResultSet(rset);
076: set = creator.getDefaultDataSet();
077: rset.close();
078: stmt.close();
079:
080: } catch (SQLException e) {
081: throw new ChartException(e);
082:
083: } finally {
084: if (con != null) {
085: try {
086: con.close();
087: } catch (SQLException e) {
088: // do nothing.
089: }
090: }
091:
092: }
093:
094: return set;
095: }
096:
097: private Connection getConnectionJNDI() throws SQLException {
098: try {
099: InitialContext ctx = new InitialContext();
100: DataSource ds = (DataSource) ctx.lookup(mJNDIName);
101: Connection con = ds.getConnection();
102: return con;
103: } catch (NamingException e) {
104: mLogger.log(Level.SEVERE, "Naming Exception:"
105: + e.toString());
106: }
107: return null;
108: }
109:
110: }
|