📄 genericsqlsource.java
字号:
/** * This file is part of the jcrontab package * Copyright (C) 2001-2003 Israel Olalla * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free * Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, * MA 02111-1307, USA * * For questions, suggestions: * * iolalla@yahoo.com * */package org.jcrontab.data;import java.sql.Connection;import java.sql.Driver;import java.sql.DriverManager;import java.sql.SQLException;import java.util.Vector;import javax.naming.Context;import javax.naming.InitialContext;import javax.naming.NameNotFoundException;import javax.naming.NamingException;import org.jcrontab.Crontab;import org.jcrontab.log.Log;/** * This class is only a generic example and doesn't aim to solve all the needs * for the differents system's. if you want to make this class to fit your needs * feel free to do it and remember the license. * On of the things this class does is to open a connection to the database * , this is nasty and very expensive, y you want to integrate jcrontab with a * pool like poolman or jboss it's quite easy, should substitute connection logic * with particular one. * @author $Author: iolalla $ * @version $Revision: 1.37 $ */public class GenericSQLSource implements DataSource { private CrontabParser cp = new CrontabParser(); /** This is the database driver being used. */ private static Object dbDriver = null; private static GenericSQLSource instance; /** This Query gets all the Crontab entries from the * events table */ public static String queryAll = "SELECT id, second, minute, hour, dayofmonth, " + " month," + " dayofweek, " + " year, task, extrainfo, businessDays " + " FROM events"; /** This Query gets all the Crontab entries from the * events table but searching by the name */ public static String querySearching = "SELECT id, second, minute, hour, " + " dayofmonth, month," + " dayofweek, " + " year, task, extrainfo, businessDays " + " FROM events" + " WHERE task = ? "; /** This Query stores the Crontab entries */ public static String queryStoring = "INSERT INTO events(" + " id, second, minute, hour, dayofmonth," + " month, dayofweek, year, " + " task, extrainfo, businessDays) " + " VALUES(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)"; /** This Query removes the given Crontab Entries */ public static String queryRemoving = "DELETE FROM events WHERE " + " id = ? "; /** This Query finds the next value in the sequence */ public static String nextSequence = "SELECT MAX(id) id FROM EVENTS " ; /** Creates new GenericSQLSource */ protected GenericSQLSource() { } /** This method grants this class to be a singleton * and grants data access integrity * @return returns the instance */ public DataSource getInstance() { if (instance == null) { instance = new GenericSQLSource(); } return instance; } /** * This method searches the Crontab Entry that the class has the given name * @param CrontabEntryBean bean this method only lets store an * entryBean each time. * @throws CrontabEntryException when it can't parse the line correctly * @throws ClassNotFoundException cause loading the driver can throw an * ClassNotFoundException * @throws SQLException Yep can throw an SQLException too */ public CrontabEntryBean find(CrontabEntryBean ceb) throws CrontabEntryException, ClassNotFoundException, SQLException, DataNotFoundException { CrontabEntryBean[] cebra = findAll(); for (int i = 0; i < cebra.length ; i++) { if (cebra[i].equals(ceb)) { return cebra[i]; } } throw new DataNotFoundException("Unable to find :" + ceb); } /** * This method searches all the CrontabEntries from the DataSource * @return CrontabEntryBean[] the array of CrontabEntryBeans. * @throws CrontabEntryException when it can't parse the line correctly * @throws ClassNotFoundException cause loading the driver can throw an * ClassNotFoundException * @throws SQLException Yep can throw an SQLException too */ public CrontabEntryBean[] findAll() throws CrontabEntryException, ClassNotFoundException, SQLException, DataNotFoundException { Vector list = new Vector(); Connection conn = null; java.sql.Statement st = null; java.sql.ResultSet rs = null; try { conn = getConnection(); st = conn.createStatement(); rs = st.executeQuery(queryAll); if(rs!=null) { while(rs.next()) { boolean[] bSeconds = new boolean[60]; boolean[] bYears = new boolean[10]; int id = rs.getInt("id"); String second = rs.getString("second"); String minute = rs.getString("minute"); String hour = rs.getString("hour"); String dayofmonth = rs.getString("dayofmonth"); String month = rs.getString("month"); String dayofweek = rs.getString("dayofweek"); String year = rs.getString("year"); String task = rs.getString("task"); String extrainfo = rs.getString("extrainfo"); String line = minute + " " + hour + " " + dayofmonth + " " + month + " " + dayofweek + " " + task + " " + extrainfo; String sBusinessDays = rs.getString("businessDays"); boolean businessDays = false ; if (sBusinessDays != null && sBusinessDays.equalsIgnoreCase("true")) { businessDays = true ; } CrontabEntryBean ceb = cp.marshall(line); cp.parseToken(year, bYears, false); ceb.setId(id); ceb.setBYears(bYears); ceb.setYears(year); cp.parseToken(second, bSeconds, false); ceb.setBSeconds(bSeconds); ceb.setSeconds(second); ceb.setBusinessDays(businessDays); list.add(ceb); } rs.close(); } else { throw new DataNotFoundException("No CrontabEntries available"); } } finally { try { st.close(); } catch (Exception e) {} try { conn.close(); } catch (Exception e2) {} } CrontabEntryBean[] result = new CrontabEntryBean[list.size()]; for (int i = 0; i < list.size(); i++) { result[i] = (CrontabEntryBean)list.get(i); } return result; } /** * This method removes the given Crontab Entries * @param CrontabEntryBean bean this method only lets store an
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -