Properties类与DataResource类的解释及使用

参数文档配置的方法,使用Properties 类跟DataSource 

Properties 类的使用方法:

在根目录底下建以.properties 或者.xml结尾的文件

配置参数

#driver=oracle.jdbc.driver.OracleDriver

#jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl

#user=scott

#password=java

driverClass=com.mysql.jdbc.Driver

jdbcUrl=jdbc:mysql://localhost:3306/qh_db

user=root

password=123456

在使用的类上声明

Properties properties= new Properties();

InputStream inputStream=TicketDAOImpl.class.getClassLoader.getResourceAsStream("db.properties");

try{

properties.load(inputStream);

}

要使用的时候

user=properties.getProperty("user");

password=properties.getProperty("password");

driverClass=properties.getProperty("driverClass");

jdbcUrl=properties.getProperty("jdbcUrl");

DataRource的用法:

在根目录底下建立c3p0-config.xml文件

配置参数

<c3p0-config>

  <named-config name="antBank"> 

  

    <property name="user">root</property>

    <property name="password">123456</property>

    <property name="jdbcUrl">jdbc:mysql:///qh_db</property>

    <property name="driverClass">com.mysql.jdbc.Driver</property>

  

    <property name="acquireIncrement">2</property>

    <property name="initialPoolSize">5</property>

    <property name="minPoolSize">5</property>

    <property name="maxPoolSize">10</property>

    <property name="maxStatements">20</property> 

    <property name="maxStatementsPerConnection">5</property>

   

  </named-config>

  

</c3p0-config>

定义一个私有的静态DataResource变量

private Static DataResource dataSource=null;

static{

 dataSource=new ComboPooledDataSource("antBank");

}

需要用的时候

Connection conn=dataSource.getConnection();

使用ThreadLocal(线程局部变量)绑定Cnnection

新建一个类

在类里面定义一个ThreadLocal的私有变量

private ThreadLocal<Connection> connectionThreadLocal=new ThreadLocal<>();

定义connectionThreadLocalbind方法

public void bindConnection connection

{

 connectionThreadLocal.set(connection);

}

定义get()方法

public Connection get()

{

return connectionThreadLocal.get();

}

定义remove()方法

public void remove()

{

connectionThreadLocal.remove();

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值