⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connectionmanager.java

📁 oa办公系统
💻 JAVA
字号:
/*
 * FileName: ConnectionManager.java,v 1.0 Oct 24, 2008 6:45:10 AM sean
 * 
 * Copyright (c) 2008 Sean
 * All Rights Reserved.
 * Confidential and for internal use only.
 */
package com.wanczy.dbutil;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Properties;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.mchange.v2.c3p0.DataSources;
/**
 * Description
 * 
 * @author sean
 * @version $Revision: 1.1 $
 * @since 1.0
 */
public class ConnectionManager {
	private static ConnectionManager instance;
	private ComboPooledDataSource ds;
	//private static String c3p0Properties = "c3p0.properties";

	private ConnectionManager() throws Exception {
		ds = new ComboPooledDataSource();
//		Properties p = new Properties();
//		p.load(this.getClass().getResourceAsStream(c3p0Properties));
//		ds.setDriverClass(p.getProperty("driverClass"));
//		ds.setJdbcUrl(p.getProperty("jdbcUrl"));
//		ds.setUser(p.getProperty("user"));
//		ds.setPassword(p.getProperty("password"));
//		ds.setAutoCommitOnClose("true".equals(p.getProperty("autoCommitOnClose")));
//		ds.setMaxStatements(Integer.parseInt(p.getProperty("maxStatements", "150")));
//		ds.setMaxIdleTime(Integer.parseInt(p.getProperty("maxIdleTime","1800")));
//		ds.setMaxPoolSize(Integer.parseInt(p.getProperty("maxPoolSize","20")));
	}

	public synchronized static final ConnectionManager getInstance() {
		if (instance == null) {
			try {
				instance = new ConnectionManager();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return instance;
	}

	public synchronized final Connection getConnection() {
		try {
			return ds.getConnection();
		} catch (SQLException e) {
			throw new RuntimeException("取得数据库连接失败",e);
		}
	}
	public void closeConn(Connection conn){
		if(conn != null)
			try {
				conn.close();
			} catch (SQLException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
	}

	protected void finalize() throws Throwable {
		DataSources.destroy(ds); // 关闭datasource
		super.finalize();
	}
	public static void main(String[] args) {
		ConnectionManager connManager=ConnectionManager.getInstance();
		Connection conn=connManager.getConnection();
		try{
			Statement stmt=conn.createStatement(ResultSet.TYPE_SCROLL_INSENSITIVE,ResultSet.CONCUR_READ_ONLY);
//			Statement stmt=conn.createStatement();
			try{
				String sql="select top 3 * from employee";
				ResultSet rs=stmt.executeQuery(sql);
				//rs.absolute(offset);
//				for(int i=0;i<offset;i++){
//					if(!rs.next()) break;
//				}
				while(rs.next()){
					System.out.printf("fname:%s\n",rs.getString("fname"));
				}
				rs.close();
			}finally{
				stmt.close();
			}
		}catch(SQLException e){
			throw new RuntimeException("查询employee出错",e);
		}finally{
			connManager.closeConn(conn);
		}
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -