Chinaunix首页 | 论坛 | 博客
  • 博客访问: 83017
  • 博文数量: 7
  • 博客积分: 2010
  • 博客等级: 大尉
  • 技术积分: 295
  • 用 户 组: 普通用户
  • 注册时间: 2007-10-20 21:09
文章分类

全部博文(7)

文章存档

2011年(1)

2010年(1)

2009年(4)

2008年(1)

我的朋友

分类: Java

2009-06-05 11:31:59

 

Oracle Tomcat c3p0连接池配置


c3p0等包放入tomcat/lib/


tomcat/conf/下的server.xml web.xml context.xml中添加如下


web.xml:

DB Connection

jdbc/MwealthPool

javax.sql.DataSource

Container


server.xml:


context.xml:









新建类:

package com.soft136.kf.dao;


import java.sql.Connection;

import java.sql.SQLException;


import javax.naming.Context;

import javax.naming.InitialContext;

import javax.sql.DataSource;


public class DataBaseConnection {

private static DataBaseConnection dbPool;

private DataSource dataSource;

static {

dbPool = new DataBaseConnection();

}


public DataBaseConnection() {

try {

Context initCtx = new InitialContext();

if (initCtx == null)

throw new Exception("error");

Context ctx = (Context) initCtx.lookup("java:comp/env");

if (ctx != null)

dataSource = (DataSource) ctx.lookup("jdbc/MwealthPool");

if (dataSource == null)

throw new Exception("error");

} catch (Exception e) {

e.printStackTrace();

}

}


public final static DataBaseConnection getInstance() {

return dbPool;

}


public final Connection getConnection() {

try {

return dataSource.getConnection();

} catch (SQLException e) {

throw new RuntimeException("connection error ", e);

}

}

}


使用:

public OracleCachedRowSet countReMess() throws SQLException {

Connection conn = null;

PreparedStatement ps = null;

ResultSet rs = null;

String sql = "SELECT count(*) FROM T_LOG t";

try {

conn = DataBaseConnection.getInstance().getConnection();

ps = conn.prepareStatement(sql);

rs = ps.executeQuery();

OracleCachedRowSet ocrs = new OracleCachedRowSet();

ocrs.populate(rs);

return ocrs;

} finally {

if (ps != null) {

ps.close();

}

if (conn != null) {

conn.close();

}

}

}

阅读(2797) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~