web启动,quartz 关联的servlet 启动,得到Spring的bean ,servletContext 获取数据源

package com.cal.servlet;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import org.quartz.Scheduler;
import org.quartz.SchedulerException;
import org.quartz.SchedulerFactory;
import org.quartz.impl.StdSchedulerFactory;
import com.cal.utils.DBOperation;

public class QuartzJobServlet extends HttpServlet {

	/**
	 * serialVersionUID
	 */
	private static final long serialVersionUID = 199173078896194253L;

//	private ServletContext sc;
	/**
	 * Constructor of the object.
	 */
	public QuartzJobServlet() {
		super();
	}

	/**
	 * Destruction of the servlet. <br>
	 */
	public void destroy() {
		super.destroy(); // Just puts "destroy" string in log
		// Put your code here
	}

	/**
	 * Initialization of the servlet. <br>
	 *
	 * @throws ServletException if an error occurs
	 */
	public void init(ServletConfig config) throws ServletException {
		ServletContext sc = config.getServletContext();
		SchedulerFactory sf = new StdSchedulerFactory();
		Scheduler sched;
		try {
			DBOperation o = new DBOperation();
			o.excuteUpdate("select * from userinfo ", null, sc);
			sched = sf.getScheduler();
			sched.start();
		} catch (SchedulerException e) {
			e.printStackTrace();
		} 
	}

}


DBOperation :
package com.cal.utils;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import javax.servlet.ServletContext;
import org.apache.log4j.Logger;

public class DBOperation {
	private PreparedStatement stsm;
	private static Logger log = Logger.getLogger(DBOperation.class);
	public void excuteUpdate(String sql,Object[]values,ServletContext sc){
		try {
			stsm = DBConnection.getConnection(sc).prepareStatement(sql);
			if(values!=null && values.length>0)
			{
				for(int i=0 ; i<values.length ;i++)
				{
					stsm.setObject(i+1, values[i]);
				}
			}
			stsm.executeUpdate();
		} catch (SQLException e) {
			log.error("数据更新错误!",e);
		} finally {
			DBConnection.closeConnection();
		}

	}
}


DBConnection:
package com.cal.utils;
import java.sql.Connection;
import java.sql.SQLException;

import javax.servlet.ServletContext;


import org.apache.log4j.Logger;
import org.logicalcobwebs.proxool.ProxoolDataSource;
import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class DBConnection {
	private static Connection conn;
	private static Logger log = Logger.getLogger(DBConnection.class);

	/**
	 * 获得数据库连接
	 * 
	 * @return
	 */
	public static synchronized Connection getConnection(ServletContext sc) {
		init(sc);
		return conn;
	}

	/**
	 * 初始化数据库连接
	 */
	private static void init(ServletContext sc) {
		try {
			//PropUtils p = new PropUtils();
			//p.loadFile("jdbc.properties");
			ApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(sc);
			//new FileSystemXmlApplicationContext();
			ProxoolDataSource prox = (ProxoolDataSource)ctx.getBean("dataSource");
			
			// 获取连接
			conn =prox.getConnection();
			// Class.forName(p.getValue("db.driver"));
			// conn =
			// DriverManager.getConnection(p.getValue("db.url"),p.getValue("db.user"),p.getValue("db.password"));
		} catch (SQLException e) {
			log.error("数据库连接错误!", e);
		}catch(Exception e){
			log.error("数据库连接错误!", e);
		}
	}

	/**
	 * 关闭数据库连接
	 */
	public static void closeConnection() {
		if (conn != null) {
			try {
				conn.close();
			} catch (SQLException e) {
				log.error("数据库关闭错误!", e);
			}
		}
	}
//	public static void main(String[] args) {
//		init();
//	}
}

你可能感兴趣的:(spring,bean,Web,quartz,servlet)