JNDI、DBCP、C3P0三种数据源的简单使用

来源:互联网 发布:c语言中各类数据 编辑:程序博客网 时间:2024/06/06 01:34

首先无论使用哪种数据源都需要使用数据库连接,就要导入相对应的数据库连接的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.Driverurl=jdbc:mysql://localhost:3306/struts2?useSSL=trueusername=rootpassword=123456initialSize=10maxActive=50maxIdle=20minIdle=5maxWait=60000connectionProperties=useUnicode=true;characterEncoding=utf8defaultAutoCommit=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("获取连接失败");}}}



原创粉丝点击