Web项目配置数据源——c3p0_xml方式

web项目-->WebRoot-->WEB-INF导入C3P0的jar包(c3p0-0.9.1.2.jar)、数据库驱动jar包(ojdbc14.jar)

web项目-->src-->c3p0-config.xml





jdbc:oracle:thin://@localhost:1521:XE
oracle.jdbc.driver.OracleDriver
scott
smq
30000
10
60
100
10




jdbc:oracle:thin://@localhost:1521:XE
oracle.jdbc.driver.OracleDriver
scott
smq
30000
10
30
100
10



jdbc:oracle:thin://@localhost:1521:XE
oracle.jdbc.driver.OracleDriver
scott
smq
30000
10
30
100
10



web项目-->src-->com.niit.util-->DBUtil.Java

package com.niit.util;

import java.beans.PropertyVetoException;

import java.util.Map;

import com.mchange.v2.c3p0.ComboPooledDataSource;


public class DBUtil {


private static ComboPooledDataSource ds = null;


private DBUtil(){}


static{

//2、xml文件方式:
ds = new ComboPooledDataSource("oracle");
}

public synchronized static Connection getConnection(){
Connection con = null;
try {
con = ds.getConnection();
} catch (SQLException e) {
System.out.println("获取数据库连接失败!");
// e.printStackTrace();
}

return con;
}


public synchronized static Connection getConnection(){
Connection con = null;
try {
con = ds.getConnection();
} catch (SQLException e) {
System.out.println("获取数据库连接失败!");
// e.printStackTrace();
}

return con;
}


public static void closeResource(ResultSet rs,Statement stmt,Connection con){

try {
if(rs != null){
rs.close();
}
if(stmt != null){
stmt.close();
}
if(con != null){
con.close();
}
}catch (SQLException e) {
System.out.println("关闭资源失败!");
// e.printStackTrace();
}
}


public static void main(String[] args) {
System.out.println(getConnection());
}


}


你可能感兴趣的:(web,java)