亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb

首頁 > 學院 > 開發設計 > 正文

java連接sqlserver實例

2019-11-18 11:19:14
字體:
來源:轉載
供稿:網友

package sanpai.db;import java.io.*;import java.sql.*;import java.util.*;import java.util.Date;/** * This class is a Singleton that PRovides access to one or many * connection pools defined in a Property file. A client gets * access to the single instance through the static getInstance() * method and can then check-out and check-in connections from a pool. * When the client shuts down it should call the release() method * to close all open connections and do other clean up. */public class DBConnectionManager {	static private DBConnectionManager instance;       // The single instance	static private int clients;	private Vector drivers = new Vector();	private PrintWriter log;	private Hashtable pools = new Hashtable();		/**	 * This inner class represents a connection pool. It creates new	 * connections on demand, up to a max number if specified.	 * It also makes sure a connection is still open before it is	 * returned to a client.	 */	class DBConnectionPool {		private int checkedOut;		private Vector freeConnections = new Vector();		private int maxConn;		private String name;		private String passWord;		private String URL;		private String user;				/**		 * Creates new connection pool.		 *		 * @param name The pool name		 * @param URL The JDBC URL for the database		 * @param user The database user, or null		 * @param password The database user password, or null		 * @param maxConn The maximal number of connections, or 0		 *   for no limit		 */		public DBConnectionPool(String name, String URL, String user, String password, 				int maxConn) {			this.name = name;			this.URL = URL;			this.user = user;			this.password = password;			this.maxConn = maxConn;		}				/**		 * Checks in a connection to the pool. Notify other Threads that		 * may be waiting for a connection.		 *		 * @param con The connection to check in		 */		public synchronized void freeConnection(Connection con) {			// Put the connection at the end of the Vector			freeConnections.addElement(con);			checkedOut--;			notifyAll();		}				/**		 * Checks out a connection from the pool. If no free connection		 * is available, a new connection is created unless the max		 * number of connections has been reached. If a free connection		 * has been closed by the database, it's removed from the pool		 * and this method is called again recursively.		 */		public synchronized Connection getConnection()throws SQLException {			Connection con = null;			if (freeConnections.size() > 0) {				// Pick the first Connection in the Vector				// to get round-robin usage				con = (Connection) freeConnections.firstElement();				freeConnections.removeElementAt(0);				try {					if (con.isClosed()) {						log("Removed bad connection from " + name);						// Try again recursively						con = getConnection();					}				}				catch (SQLException e) {					log("Removed bad connection from " + name);					// Try again recursively					con = getConnection();				}			}			else if (maxConn == 0  checkedOut < maxConn) {				con = newConnection();			}			if (con != null) {				checkedOut++;			}			return con;		}				/**		 * Checks out a connection from the pool. If no free connection		 * is available, a new connection is created unless the max		 * number of connections has been reached. If a free connection		 * has been closed by the database, it's removed from the pool		 * and this method is called again recursively.		 * <P>		 * If no connection is available and the max number has been 		 * reached, this method waits the specified time for one to be		 * checked in.		 *		 * @param timeout The timeout value in milliseconds		 */		public synchronized Connection getConnection(long timeout) throws SQLException {			long startTime = new Date().getTime();			Connection con;			while ((con = getConnection()) == null) {				try {					wait(timeout);				}				catch (InterruptedException e) {}				if ((new Date().getTime() - startTime) >= timeout) {					// Timeout has eXPired					return null;				}			}			return con;		}				/**		 * Closes all available connections.		 */		public synchronized void release() {			Enumeration allConnections = freeConnections.elements();			while (allConnections.hasMoreElements()) {				Connection con = (Connection) allConnections.nextElement();				try {					con.close();					log("Closed connection for pool " + name);				}				catch (SQLException e) {					log(e, "Can't close connection for pool " + name);				}			}			freeConnections.removeAllElements();		}				/**		 * Creates a new connection, using a userid and password		 * if specified.		 */		private Connection newConnection() throws SQLException {			Connection con = null;			try {				if (user == null) {					con = DriverManager.getConnection(URL);				}				else {					con = DriverManager.getConnection(URL, user, password);				}				log("Created a new connection in pool " + name);			}			catch (SQLException e) {				log(e, "Can't create a new connection for " + URL);				throw e;			}			return con;		}	}	/**	 * A private constrUCtor since this is a Singleton	 */	private DBConnectionManager() {		init();	}	/**	 * Creates instances of DBConnectionPool based on the properties.	 * A DBConnectionPool can be defined with the following properties:	 * <PRE>	 * &lt;poolname&gt;.url         The JDBC URL for the database	 * &lt;poolname&gt;.user        A database user (optional)	 * &lt;poolname&gt;.password    A database user password (if user specified)	 * &lt;poolname&gt;.maxconn     The maximal number of connections (optional)	 * </PRE>	 *	 * @param props The connection pool properties	 */	private void createPools(Properties props) {		Enumeration propNames = props.propertyNames();		while (propNames.hasMoreElements()) {			String name = (String) propNames.nextElement();			if (name.endsWith(".url")) {				String poolName = name.substring(0, name.lastIndexOf("."));				String url = props.getProperty(poolName + ".url");				if (url == null) {					log("No URL specified for " + poolName);					continue;				}				String user = props.getProperty(poolName + ".user");				String password = props.getProperty(poolName + ".password");				String maxconn = props.getProperty(poolName + ".maxconn", "0");				int max;				try {					max = Integer.valueOf(maxconn).intValue();				}				catch (NumberFormatException e) {					log("Invalid maxconn value " + maxconn + " for " + poolName);					max = 0;				}				DBConnectionPool pool = 					new DBConnectionPool(poolName, url, user, password, max);				pools.put(poolName, pool);				log("Initialized pool " + poolName);			}		}	}	/**	 * Returns a connection to the named pool.	 *	 * @param name The pool name as defined in the properties file	 * @param con The Connection	 */	public void freeConnection(String name, Connection con) {		DBConnectionPool pool = (DBConnectionPool) pools.get(name);		if (pool != null) {			pool.freeConnection(con);		}	}	/**	 * Returns an open connection. If no one is available, and the max	 * number of connections has not been reached, a new connection is	 * created.	 *	 * @param name The pool name as defined in the properties file	 * @return Connection The connection or null	 */	public Connection getConnection(String name) throws SQLException {		DBConnectionPool pool = (DBConnectionPool) pools.get(name);		if (pool != null) {			return pool.getConnection();		}		return null;	}	/**	 * Returns an open connection. If no one is available, and the max	 * number of connections has not been reached, a new connection is	 * created. If the max number has been reached, waits until one	 * is available or the specified time has elapsed.	 *	 * @param name The pool name as defined in the properties file	 * @param time The number of milliseconds to wait	 * @return Connection The connection or null	 */	public Connection getConnection(String name, long time) throws SQLException {		DBConnectionPool pool = (DBConnectionPool) pools.get(name);		if (pool != null) {			return pool.getConnection(time);		}		return null;	}	/**	 * Returns the single instance, creating one if it's the	 * first time this method is called.	 *	 * @return DBConnectionManager The single instance.	 */	static synchronized public DBConnectionManager getInstance() {		if (instance == null) {			instance = new DBConnectionManager();		}		clients++;		return instance;	}	/**	 * Loads properties and initializes the instance with its values.	 */	private void init() {		InputStream is = getClass().getResourceAsStream("/db.properties");		Properties dbProps = new Properties();		try {			dbProps.load(is);		}		catch (Exception e) {			System.err.println("Can't read the properties file. " +				"Make sure db.properties is in the CLASSPATH");			return;		}		String logFile = dbProps.getProperty("logfile", "DBConnectionManager.log");		try {			log = new PrintWriter(new FileWriter(logFile, true), true);		}		catch (IOException e) {			System.err.println("Can't open the log file: " + logFile);			log = new PrintWriter(System.err);		}		loadDrivers(dbProps);		createPools(dbProps);	}	/**	 * Loads and registers all JDBC drivers. This is done by the	 * DBConnectionManager, as opposed to the DBConnectionPool,	 * since many pools may share the same driver.	 *	 * @param props The connection pool properties	 */	private void loadDrivers(Properties props) {		String driverClasses = props.getProperty("drivers");		StringTokenizer st = new StringTokenizer(driverClasses);		while (st.hasMoreElements()) {			String driverClassName = st.nextToken().trim();			try {				Driver driver = (Driver) 					Class.forName(driverClassName).newInstance();				DriverManager.registerDriver(driver);				drivers.addElement(driver);				log("Registered JDBC driver " + driverClassName);			}			catch (Exception e) {				log("Can't register JDBC driver: " +					driverClassName + ", Exception: " + e);			}		}	}	/**	 * Writes a message to the log file.	 */	private void log(String msg) {		log.println(new Date() + ": " + msg);	}	/**	 * Writes a message with an Exception to the log file.	 */	private void log(Throwable e, String msg) {		log.println(new Date() + ": " + msg);		e.printStackTrace(log);	}	/**	 * Closes all open connections and deregisters all drivers.	 */	public synchronized void release() {		// Wait until called by the last client		if (--clients != 0) {			return;		}				Enumeration allPools = pools.elements();		while (allPools.hasMoreElements()) {			DBConnectionPool pool = (DBConnectionPool) allPools.nextElement();			pool.release();		}		Enumeration allDrivers = drivers.elements();		while (allDrivers.hasMoreElements()) {			Driver driver = (Driver) allDrivers.nextElement();			try {				DriverManager.deregisterDriver(driver);				log("Deregistered JDBC driver " + driver.getClass().getName());			}			catch (SQLException e) {				log(e, "Can't deregister JDBC driver: " + driver.getClass().getName());			}		}	}/** * release all resources when this object is destroyed. */public void finalize() {	release();}}



發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
亚洲香蕉成人av网站在线观看_欧美精品成人91久久久久久久_久久久久久久久久久亚洲_热久久视久久精品18亚洲精品_国产精自产拍久久久久久_亚洲色图国产精品_91精品国产网站_中文字幕欧美日韩精品_国产精品久久久久久亚洲调教_国产精品久久一区_性夜试看影院91社区_97在线观看视频国产_68精品久久久久久欧美_欧美精品在线观看_国产精品一区二区久久精品_欧美老女人bb
日韩经典第一页| 欧美激情视频给我| 欧美影院成年免费版| 日韩电影中文字幕在线| 久久久久久亚洲精品| 久久深夜福利免费观看| 97香蕉久久夜色精品国产| 国自产精品手机在线观看视频| 国自在线精品视频| 美日韩丰满少妇在线观看| www.国产精品一二区| 欧美性生交大片免费| 国产香蕉97碰碰久久人人| 精品伊人久久97| 色综合91久久精品中文字幕| 国产精品普通话| 成人免费在线视频网址| 91免费高清视频| 国产精品久久一区| 国产日韩中文字幕在线| 热久久这里只有精品| 国产欧美精品在线| 69影院欧美专区视频| 日韩av在线不卡| 懂色av影视一区二区三区| 97久久精品视频| 91精品国产91久久| 久久精品一区中文字幕| 亚洲成人激情视频| 久久男人资源视频| 日本精品免费观看| 日韩在线观看网站| 91精品在线国产| 日韩在线免费视频观看| 久热精品在线视频| 中文字幕在线观看日韩| 国产精品人人做人人爽| 亚洲精品电影网站| 亚洲高清免费观看高清完整版| 日韩av在线不卡| 国产成+人+综合+亚洲欧洲| 欧美日韩成人免费| 日韩高清电影免费观看完整版| 日韩欧美a级成人黄色| 亚洲九九九在线观看| 91精品国产高清久久久久久91| 97超碰国产精品女人人人爽| 国产精品免费福利| 精品精品国产国产自在线| 欧美成年人在线观看| 国产剧情日韩欧美| 91日本在线观看| 性夜试看影院91社区| 精品久久香蕉国产线看观看gif| 欧美激情欧美激情| 成人福利视频在线观看| 精品视频在线播放色网色视频| 欧美性jizz18性欧美| 久久久天堂国产精品女人| 欧美性猛交99久久久久99按摩| 亚洲欧美日韩在线高清直播| 国产精品久久久久影院日本| 日本中文字幕不卡免费| 97精品视频在线观看| 中文字幕亚洲欧美| 久久在线视频在线| 51久久精品夜色国产麻豆| 亚洲影院在线看| 成人精品福利视频| 国内外成人免费激情在线视频网站| 日韩在线观看免费av| 在线观看精品国产视频| 最近免费中文字幕视频2019| 日韩在线观看精品| 韩国三级电影久久久久久| 国产精品91久久久久久| 欧美丝袜第一区| 538国产精品一区二区免费视频| 国产精品91一区| 色综合久久精品亚洲国产| 亚洲伊人成综合成人网| 日本久久久久久久久| 亚洲精品欧美一区二区三区| 欧美三级xxx| 成人激情视频免费在线| 亚洲精品日韩丝袜精品| 热99在线视频| 岛国av午夜精品| 国产拍精品一二三| 亚洲自拍小视频免费观看| 懂色aⅴ精品一区二区三区蜜月| 亚洲国语精品自产拍在线观看| 日本精品一区二区三区在线播放视频| 亚洲一区二区中文字幕| 国产精品美女久久| 国产精品老女人精品视频| 欧美激情aaaa| 亚洲黄色在线观看| 日韩有码在线视频| 久久夜色精品国产亚洲aⅴ| 国产精品亚洲综合天堂夜夜| 亚洲欧美一区二区三区久久| 98视频在线噜噜噜国产| 国产成人精品视频| 欧美成人在线免费视频| 亚洲免费视频观看| 欧美黑人一区二区三区| 精品久久久视频| 疯狂做受xxxx高潮欧美日本| 亚洲国产精品字幕| 国产欧美va欧美va香蕉在| 国产精品色午夜在线观看| 日韩av影片在线观看| 26uuu国产精品视频| 国产精品久久久久影院日本| 亚洲娇小xxxx欧美娇小| 久久福利视频网| 欧美性受xxx| 亚洲少妇中文在线| 国产美女搞久久| 91天堂在线观看| 精品久久久久久久久久ntr影视| 免费97视频在线精品国自产拍| 国产精品视频最多的网站| 欧美日韩国产综合视频在线观看中文| 少妇高潮久久77777| 久久精品91久久久久久再现| 欧美在线视频观看| 精品日韩视频在线观看| 91久久久久久久久久久久久| 欧美天堂在线观看| 欧美在线日韩在线| 性金发美女69hd大尺寸| 日产日韩在线亚洲欧美| 欧美孕妇孕交黑巨大网站| 影音先锋欧美精品| 91超碰中文字幕久久精品| 91中文在线观看| 国产极品jizzhd欧美| 亚洲高清久久网| 亚洲人成在线电影| 欧美电影院免费观看| 伦伦影院午夜日韩欧美限制| 欧美日韩国产91| 国产精品自产拍在线观| 亚洲欧美综合另类中字| 91精品免费看| 久久国产精品久久精品| 亚洲成人精品久久| 91香蕉嫩草神马影院在线观看| 精品偷拍一区二区三区在线看| 国产极品精品在线观看| 亚洲在线免费视频| 欧美寡妇偷汉性猛交| 怡红院精品视频| 国产精品亚发布| 亚洲精品www| 欧美色videos| 亚洲美女精品成人在线视频| 2019av中文字幕| 久久九九有精品国产23| 日韩小视频在线| 国产欧美精品在线| 亚洲另类欧美自拍|