1. 程式人生 > >JNDI、DBCP、C3P0三種資料來源的簡單使用

JNDI、DBCP、C3P0三種資料來源的簡單使用

首先無論使用哪種資料來源都需要使用資料庫連線,就要匯入相對應的資料庫連線的jar,這裡使用mysql資料庫,所以匯入:

mysql-connector-java-5.1.43-bin.jar

1.JNDI資料來源

package com.yyy.utils;

import java.sql.Connection;
import java.sql.SQLException;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;

//使用JNDI獲取資料來源
public class JNDIUtil {
	//1.定義一個數據源
	private static DataSource ds;
	
	//2.給資料來源賦值
	static {
		try {
			Context initCtx = new InitialContext();
			ds = (DataSource) initCtx.lookup("java:comp/env/jdbc/struts2");//"jdbc/struts2"它是資料來源的名稱
		} catch (NamingException e) {
			throw new ExceptionInInitializerError("初始化連線失敗");
		}
	}
	
	//3.提供一個獲取資料來源的方法
	public static DataSource getDataSource() {
		return ds;
	}
	
	//4.提供一個獲取連線的方法
	public static Connection getConnection() {
		try {
			return ds.getConnection();
		} catch (SQLException e) {
			throw new RuntimeException(e);
		}
	}
}

2.DBCP資料來源

a.需要匯入的包

commons-dbcp2-2.1.1.jar

commons-pool2-2.4.2.jar

b.配置檔案資訊:

檔名:以.properties結尾

檔案存放位置:src根目錄

具體配置內容可以根據需要新增,參照DBCP文件查閱具體配置資訊

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/struts2?useSSL=true
username=root
password=123456

initialSize=10

maxActive=50

maxIdle=20

minIdle=5

maxWait=60000

connectionProperties=useUnicode=true;characterEncoding=utf8

defaultAutoCommit=true

c.具體程式碼

package com.yyy.utils;


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

import javax.sql.DataSource;

import org.apache.commons.dbcp.BasicDataSourceFactory;

//使用DBCP資料來源
public class DBCPUtil {
	
	//1.定義一個數據源
	private static DataSource ds;
	
	//2.給資料來源賦值
	static {
		Properties prop = new Properties();
		try {
			prop.load(DBCPUtil.class.getClassLoader().getResourceAsStream("dbcpconfig.properties"));
			ds = BasicDataSourceFactory.createDataSource(prop);//得到資料來源
		} catch (Exception e) {
			throw new ExceptionInInitializerError("初始化資料來源錯誤");
		}
	}
	
	//3.獲取資料來源
	public static DataSource getDataSource() {
		return ds;
	}
	
	//4.獲取連線
	public static Connection getConnection() {
		try {
			return ds.getConnection();
		} catch (SQLException e) {
			throw new RuntimeException("獲取連線失敗");
		}
	}
}

3.C3P0資料來源

a.需要匯入的包

mchange-commons-java-0.2.11.jar

c3p0-0.9.5.2.jar

b.配置檔案:

檔名:c3p0-config.xml

檔案存放位置:src根目錄

<c3p0-config>
  <default-config>
     <property name="driverClass">com.mysql.jdbc.Driver</property>
     <property name="jdbcUrl">jdbc:mysql://localhost:3306/struts2?useSSL=true</property>
     <property name="user">root</property>
     <property name="password">123456</property>
        
    <property name="initialPoolSize">10</property>
    <property name="maxIdleTime">30</property>
    <property name="maxPoolSize">100</property>
    <property name="minPoolSize">10</property>
    <property name="maxStatements">200</property>

    <user-overrides user="test-user">
      <property name="maxPoolSize">10</property>
      <property name="minPoolSize">1</property>
      <property name="maxStatements">0</property>
    </user-overrides>

  </default-config>

</c3p0-config>

也可以在程式碼中使用set方法配置,但此種方法繁瑣,且維護性差,所以一般不用

static {
		ds.setDriverClass( "com.mysql.jdbc.Driver" ); //loads the jdbc driver            
		ds.setJdbcUrl( "jdbc:mysql://localhost:3306/date809user" );
		ds.setUser("root");                                 
	        ds.setPassword("123456");
		ds.setMaxPoolSize(20);
	}

c.具體程式碼

package com.yyy.utils;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import com.mchange.v2.c3p0.ComboPooledDataSource;

public class C3P0Util {
	// 1.定義一個數據源
	private static  DataSource  ds = new ComboPooledDataSource();
	// 2.獲取資料來源
	
	public static void setDataSource(DataSource dataSource) {
		C3P0Util.ds = dataSource;
	
	}	
	//3.獲取資料來源方法
	public static DataSource getDataSource() {
		return ds;
	}
	//4.獲取連線方法
	public static Connection getConnection() {
		try {
			return ds.getConnection();
		} catch (SQLException e) {
			throw new RuntimeException("獲取連線失敗");
		}
	}
}