web启动,quartz 关联的servlet 启动,得到Spring的bean ,servletContext 获取数据源
DBOperation :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();//}}