001: /*
002: * (C) Copyright 2003 Nabh Information Systems, Inc.
003: *
004: * All copyright notices regarding Nabh's products MUST remain
005: * intact in the scripts and in the outputted HTML.
006: * This program is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public License
008: * as published by the Free Software Foundation; either version 2.1
009: * of the License, or (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU Lesser General Public License for more details.
015: *
016: * You should have received a copy of the GNU Lesser General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
019: *
020: */
021:
022: package com.nabhinc.util.db;
023:
024: import java.sql.Connection;
025: import java.sql.PreparedStatement;
026: import java.sql.ResultSet;
027: import java.sql.SQLException;
028:
029: import javax.naming.NamingException;
030:
031: import org.w3c.dom.Element;
032:
033: import com.nabhinc.core.Defaults;
034: import com.nabhinc.util.XMLUtil;
035:
036: /**
037: * Create sequential IDs for database records.
038: *
039: * @author Padmanabh Dabke
040: * (c) 2003 Nabh Information Systems, Inc. All Rights Reserved.
041: */
042: public class SequenceIDCreator implements IDCreator {
043: private String scDataSource = null;
044: private String scSequenceSQL = null;
045:
046: public SequenceIDCreator() {
047: }
048:
049: public SequenceIDCreator(String dataSource, String sequenceSQL) {
050: scDataSource = dataSource;
051: scSequenceSQL = sequenceSQL;
052: }
053:
054: /**
055: *
056: * @return
057: * @throws SQLException
058: * @throws NamingException
059: */
060: public int getNextID() throws SQLException, NamingException {
061: Connection conn = null;
062: PreparedStatement st = null;
063: ResultSet results = null;
064: try {
065: conn = DBUtil.getConnection(scDataSource);
066: st = conn.prepareStatement(scSequenceSQL);
067: results = st.executeQuery();
068: results.next();
069: return results.getInt(1);
070: } finally {
071: DBUtil.close(results);
072: DBUtil.close(st);
073: DBUtil.close(conn);
074: }
075: }
076:
077: public int getNextID(Connection conn) throws SQLException {
078: PreparedStatement st = null;
079: ResultSet results = null;
080: try {
081: st = conn.prepareStatement(scSequenceSQL);
082: results = st.executeQuery();
083: results.next();
084: return results.getInt(1);
085: } finally {
086: DBUtil.close(results);
087: DBUtil.close(st);
088: }
089: }
090:
091: public void init(Element config) throws Exception {
092: scDataSource = XMLUtil.getSubElementText(config, "data-source");
093: if (scDataSource == null)
094: scDataSource = Defaults.getDataSourceName();
095: scSequenceSQL = XMLUtil.getSubElementText(config,
096: "sequence-sql");
097: if (scSequenceSQL == null)
098: throw new Exception(
099: "Missing required element: sequence-sql.");
100:
101: }
102:
103: }
|