📄 genericsqlsource.java
字号:
* 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 void remove(CrontabEntryBean[] beans) throws CrontabEntryException, ClassNotFoundException, SQLException { Connection conn = null; java.sql.PreparedStatement ps = null; try { conn = getConnection(); ps = conn.prepareStatement(queryRemoving); for (int i = 0 ; i < beans.length ; i++) { ps.setInt(1 , beans[i].getId()); ps.executeUpdate(); } } finally { try { ps.close(); } catch (Exception e) {} try { conn.close(); } catch (Exception e2) {} } } /** * This method saves the CrontabEntryBean the actual problem with this * method is that doesn't store comments and blank lines from the * original file any ideas? * @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 void store(CrontabEntryBean[] beans) throws CrontabEntryException, ClassNotFoundException, SQLException { Connection conn = null; java.sql.PreparedStatement ps = null; try { conn = getConnection(); ps = conn.prepareStatement(queryStoring); for (int i = 0 ; i < beans.length ; i++) { if (beans[i].getId() == -1) addId(beans[i], conn); ps.setInt(1, beans[i].getId()); ps.setString(2 , beans[i].getSeconds()); ps.setString(3 , beans[i].getMinutes()); ps.setString(4 , beans[i].getHours()); ps.setString(5 , beans[i].getDaysOfMonth()); ps.setString(6 , beans[i].getMonths()); ps.setString(7 , beans[i].getDaysOfWeek()); ps.setString(8 , beans[i].getYear()); if ("".equals(beans[i].getMethodName())) { ps.setString(9 , beans[i].getClassName()); } else { String classAndMethod = beans[i].getClassName() + "#" + beans[i].getMethodName(); ps.setString(9 , classAndMethod); } String extraInfo[] = beans[i].getExtraInfo(); String extraInfob = new String(); if (extraInfo.length>0) { for (int z = 0; z< extraInfo.length ; z++) { extraInfob += " "+ extraInfo[z]; } } ps.setString(10 , extraInfob); ps.setBoolean(11, beans[i].getBusinessDays()); ps.executeUpdate(); } } finally { try { ps.close(); } catch (Exception e) {} try { conn.close(); } catch (Exception e2) {} } } /** * This method saves the CrontabEntryBean the actual problem with this * method is that doesn't store comments and blank lines from the * original file any ideas? * @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 void store(CrontabEntryBean bean) throws CrontabEntryException, ClassNotFoundException, SQLException { CrontabEntryBean[] list = {bean}; store(list); } /** * Retrieves a connection to the database. May use a Connection Pool * DataSource or JDBC driver depending on the properties. * * @return a <code>Connection</code> * @exception SQLException if there is an error retrieving the Connection. */ protected Connection getConnection() throws SQLException { Crontab crontab = Crontab.getInstance(); String dbUser = crontab.getProperty( "org.jcrontab.data.GenericSQLSource.username"); String dbPwd = crontab.getProperty( "org.jcrontab.data.GenericSQLSource.password"); String dbUrl = crontab.getProperty( "org.jcrontab.data.GenericSQLSource.url"); if(dbDriver == null) { dbDriver = loadDatabaseDriver( crontab.getProperty("org.jcrontab.data.GenericSQLSource.dbDataSource")); } if(dbDriver instanceof javax.sql.DataSource) { if (dbUser != null && dbPwd != null) { return ((javax.sql.DataSource)dbDriver).getConnection(dbUser, dbPwd); } else { return ((javax.sql.DataSource)dbDriver).getConnection(); } } else { return DriverManager.getConnection(dbUrl, dbUser, dbPwd); } } /** * Initializes the database engine/data source. It first tries to load * the given DataSource name. If that fails it will load the database * driver. If the driver cannot be loaded it will check the DriverManager * to see if there is a driver loaded that can server the URL. * * @param srcName is the JDBC DataSource name or null to load the driver. * * @exception SQLExcption if there is no valid driver. */ protected Object loadDatabaseDriver(String srcName) throws SQLException { String dbDataSource = srcName; Crontab crontab = Crontab.getInstance(); if(dbDataSource == null) { String dbDriver = crontab.getProperty("org.jcrontab.data.GenericSQLSource.driver"); Log.info("Loading dbDriver: " + dbDriver); try { return Class.forName(dbDriver).newInstance(); } catch (Exception ie) { Log.error("Error loading " + dbDriver, ie); return DriverManager.getDriver( crontab.getProperty( "org.jcrontab.data.GenericSQLSource.url")); } } else { try { javax.sql.DataSource dataSource = null; Log.info("Loading dataSource: " + dbDataSource); Context ctx = null; ctx = new InitialContext(); try { dataSource = (javax.sql.DataSource)ctx.lookup(dbDataSource); } catch (NameNotFoundException nnfe) { Log.info(nnfe.getExplanation()); Log.info("Checking Tomcat Context"); Context tomcatCtx = (Context)ctx.lookup("java:comp/env"); dataSource = (javax.sql.DataSource)tomcatCtx.lookup(dbDataSource); } Log.debug("DataSource loaded. "); return dataSource; } catch (Exception e) { String msg = e.getMessage(); if(e instanceof NamingException) msg = ((NamingException)e).getExplanation(); Log.debug(msg); Log.info(msg + " will try to use dbDriver..."); return loadDatabaseDriver(null); } } } /** * This method adds the correct id to the Bean. This method is could be * replaced by other methods if you need to do this as protected plz let * me know * * @param CrontabEntryBean The CrontabEntryBean to add Id * @param Connection the conn to access to the data * * @exception SQLExcption if smth is wrong */ private void addId(CrontabEntryBean bean, Connection conn) throws SQLException { java.sql.Statement st = conn.createStatement(); java.sql.ResultSet rs = st.executeQuery(nextSequence); if(rs!=null) { while(rs.next()) { int id = rs.getInt("id"); bean.setId(id + 1 ); } } return; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -