`
laodaobazi
  • 浏览: 273103 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

JDBC连接池

    博客分类:
  • Java
 
阅读更多

application.properties

jdbc.driver=oracle.jdbc.driver.OracleDriver
jdbc.url=jdbc:oracle:thin:@192.168.1.241:1521:orcl
jdbc.username=im
jdbc.password=im

 

 

ConnectionFactory.java

import java.beans.PropertyVetoException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.Map;

import com.mchange.v2.c3p0.ComboPooledDataSource;
import com.targsoft.webim.util.ReadProperties;
  
public class ConnectionFactory {   
  
    private ConnectionFactory(){   
    }       
  
    private static ComboPooledDataSource ds = null;   
  
    static {   
        try {
        	Map<String,String> map = new ReadProperties().readFile(Thread.currentThread().getContextClassLoader().getResource("").getPath()+"application.properties") ;
    		String driverName = map.get("jdbc.driver") ;
    		String dbURL = map.get("jdbc.url") ;
    		String userName = map.get("jdbc.username") ;
    		String userPwd = map.get("jdbc.password") ;
            ds = new ComboPooledDataSource();   // 设置JDBC的Driver类   
            ds.setDriverClass(driverName);  // 参数由 Config 类根据配置文件读取   
            ds.setJdbcUrl(dbURL);    // 设置JDBC的URL   
            ds.setUser(userName);   // 设置数据库的登录用户名   
            ds.setPassword(userPwd);  // 设置数据库的登录用户密码   
            ds.setMaxPoolSize(200);   // 设置连接池的最大连接数    
            ds.setMinPoolSize(20); // 设置连接池的最小连接数     
        } catch (PropertyVetoException e) {   
            e.printStackTrace();   
        }   
    }   
    
    public static synchronized Connection getConnection() {   
        Connection con = null;   
        try {   
            con = ds.getConnection();   
        } catch (SQLException e1) {   
            e1.printStackTrace();   
        }   
        return con;   
    }   
}  

 

 

TestCaseC3P0.java

import java.sql.Connection;   
import java.sql.PreparedStatement;   
import java.sql.ResultSet;   
import java.sql.SQLException;   
  
public class TestCaseC3P0{   
  public static void main(String[] args) {   
  
  PreparedStatement pstmt = null;   // 声明一个数据库操作对象   
  ResultSet rs = null;   // 声明一个结果集对象   
  String sql = null;   // 声明一个SQL变量,用于保存SQL语句
  Connection con = null;   // DataBaseConnection为具体的数据库连接及关闭操作类   
  con = ConnectionFactory.getConnection();   // 连接数据库 
  
  sql = "SELECT a.uuid FROM im_account a ";   // 编写SQL语句   
  try {   
   pstmt = con.prepareStatement(sql);   
   rs = pstmt.executeQuery();   
   if (rs.next()) {   
   }   
   // 依次关闭   
   rs.close();   
   pstmt.close();   
  } catch (Exception e) {   
   System.out.println(e);   
  } finally {   
   // 最后一定要保证数据库已被关闭   
   try {   
    con.close();   
   } catch (SQLException e) {   
    e.printStackTrace();   
   }   
  }   
 }   
}

 

分享到:
评论
2 楼 laodaobazi 2012-01-12  
ReadProperties
痛苦不忧伤 写道
ReadProperties.java这个类没有贴出来?

呵呵 这个类很简单 就是简单的Java操作properties文件,内容如下:

package com.targsoft.webim.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;
/**
 * 
 * @author JLee
 * @Since 2011-3-10
 * @function 
 * 读取系统的properties文件
 */
public class ReadProperties {

	public Map<String,String> readFile(String fileName) {// 传入参数fileName是要读取的资源文件的文件名如(file.properties)
		InputStream in = null;
		Properties pros = new Properties();
		File file = new File(fileName);  
		Map<String,String> map = new HashMap<String,String>() ;
		try {
			if (null != fileName) {
// 			下面这种方式前提是资源文件必须类在同一个包下
//				in = ReadProperties.class.getResourceAsStream(fileName);
				in = new FileInputStream(file);  
				// 得到当前类的路径,并把资源文件名作为输入流
				pros.load(in);
				Enumeration en = pros.propertyNames();// 得到资源文件中的所有key值
				while (en.hasMoreElements()) {
					String key = (String) en.nextElement();
					map.put(key, pros.getProperty(key)) ;
// 				输出资源文件中的key与value值
//					System.out.println("key=" + key + " value=" + pros.getProperty(key));
				}
			}
		} catch (IOException e) {
			e.printStackTrace();
			System.out.println("读取资源文件出错");
		} finally {
			try {
				if (null != in) {
					in.close();
				}
			} catch (IOException e) {
				e.printStackTrace();
				System.out.println("关闭流失败");
			}
		}
		return map ;
	}

//	public static void main(String[] args) {
//		//下面这种方式前提是资源文件必须类在同一个包下
//		//	new ReadProperties().readFile("systemConfig.properties")  ;
//		//下面这种方式前提是资源文件放在classpath下面
//		new ReadProperties().readFile(Thread.currentThread().getContextClassLoader().getResource("").getPath()+"systemConfig.properties") ;
//	}
	
}

1 楼 痛苦不忧伤 2011-11-28  
ReadProperties.java这个类没有贴出来?

相关推荐

Global site tag (gtag.js) - Google Analytics