数据源配置

常用的数据源配置

1DBCP

DBCPApache推出的Database Connection Pool

使用步骤:

> 添加jar包  commons-dbcp-1.4.jar  commons-pool-1.5.6.jar

> 添加属性资源文件  dbcpconfig.properties

driverClassName=com.mysql.jdbc.Driver
url=jdbc:mysql://localhost:3306/day13
username=root
password=abc
#
initialSize=10
#
maxActive=50
#
maxIdle=20
minIdle=5
maxWait=60000
connectionProperties=useUnicode=true;characterEncoding=utf8
defaultAutoCommit=true

defaultReadOnly=

defaultTransactionIsolation=REPEATABLE_READ

> 编写数据源工具类

public class DBCPUtil {
private static DataSource ds = null;
static{
Properties prop = new Properties();
try {
prop.load(DBCPUtil.class.getClassLoader().getResourceAsStream("dbcpconfig.properties"));//根据DBCPUtil的classes的路径,加载配置文件
   ds = BasicDataSourceFactory.createDataSource(prop);//得到一个数据源 
} catch (Exception e) {
throw new ExceptionInInitializerError("初始化错误,请检查配置文件");
}
}

public static Connection getConnection(){
try {
return ds.getConnection();
} catch (SQLException e) {
throw new RuntimeException("服务器忙。。。");
}
}

public static void release(Connection conn,Statement stmt,ResultSet rs){
//关闭资源
if(rs!=null){
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
rs = null;
}
if(stmt!=null){
try {
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
stmt = null;
}
if(conn!=null){
try {
conn.close();//关闭
} catch (Exception e) {
e.printStackTrace();
}
conn = null;
}
}

}

测试类

public class TestJDBC {
@Test
public void test1(){
Connection conn = null;
PreparedStatement ps = null;

try {
conn = DBCPUtil.getConnection();
ps = conn.prepareStatement("...");

// ...
} catch (SQLException e) {
e.printStackTrace();
}finally{
DBCPUtil.release(conn, ps, null);
}

}
}

.2C3P0

 使用步骤:

1、添加jar包   

2、编写配置文件

c3p0-config.xml,放在classpath中,或classes目录中

 <?xml version="1.0" encoding="UTF-8"?>
<c3p0-config>
  <default-config>
<property name="driverClass">com.mysql.jdbc.Driver</property>
<property name="jdbcUrl">jdbc:mysql://localhost:3306/day13</property>
<property name="user">root</property>
<property name="password">abc</property>
    <property name="initialPoolSize">10</property>
    <property name="maxIdleTime">30</property>
    <property name="maxPoolSize">100</property>
    <property name="minPoolSize">10</property>
  </default-config>
</c3p0-config>

3、编写工具类:

public class C3P0Util {
//得到一个数据源
private static DataSource dataSource = new ComboPooledDataSource();

//从数据源中得到一个连接对象
public static Connection getConnection(){
try {
return dataSource.getConnection();
} catch (SQLException e) {
throw new RuntimeException("服务器错误");
}
}

public static void release(Connection conn,Statement stmt,ResultSet rs){
//关闭资源
if(rs!=null){
try {
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
rs = null;
}
if(stmt!=null){
try {
stmt.close();
} catch (Exception e) {
e.printStackTrace();
}
stmt = null;
}
if(conn!=null){
try {
conn.close();//关闭
} catch (Exception e) {
e.printStackTrace();
}
conn = null;
}
}

}

测试类:

public class TestCRUD {

@Test
public void testInsert(){
Connection conn = null;
PreparedStatement ps = null;

try {
conn = C3P0Util.getConnection();
ps = conn.prepareStatement("insert into account(name,money) values('ggg',2000)");
ps.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
}finally{
C3P0Util.release(conn, ps, null);

}


System.out.println(conn.getClass().getName());
}
}

JavaWeb服务器管理数据源:Tomcat

开发JavaWeb应用,必须使用一个JavaWeb服务器,JavaWeb服务器都内置数据源。

Tomcat:(DBCP

数据源只需要配置服务器即可。

 配置数据源的步骤:

1、拷贝数据库连接的jartomcatlib目录下

2、配置数据源XML文件

a)如果把配置信息写在tomcat下的conf目录的context.xml中,那么所有应用都能使用此数据源。

b)如果是在当前应用的META-INF中创建context.xml,编写数据源,那么只有当前应用可以使用。

contxt.xml

<?xml version="1.0" encoding="UTF-8"?>
<Context>
 <Resource name="jdbc/pn" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="root" password="abc" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/pn"/>

</Context>

3、使用连接池

  <%
    Context initContext = new InitialContext();
DataSource ds = (DataSource)initContext.lookup("java:/comp/env/jdbc/databasename");
Connection conn = ds.getConnection();
    out.print(conn);
     %>


JNDIjava nameing directory interface

JNDI容器就是一个Map

keyString

valueObject

path+name

对象

path+"jdbc/databasename"

DataSource对象
























































































































































  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值