`
touchmm
  • 浏览: 1002997 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

Java c3p0 oracle 数据库连接池 代码实现 (二)

阅读更多

DatabaseAccessImpl.java
package com.database;
import java.io.*;
import java.sql.*;
import java.util.*;
import org.apache.commons.logging.*;
public class DatabaseAccessImpl implements DatabaseAccessInterface {
private static Log log = LogFactory.getLog(DatabaseAccessImpl.class);
public void executeSQL(String sqlStatement) throws SQLException {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = DBConnectionManager.getConnection();
connection.setAutoCommit(true);
statement = connection.prepareStatement(sqlStatement);
statement.execute();
} catch (SQLException ex) {
ex.printStackTrace();
throw ex;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
statement.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
try {
connection.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
return;
}

public void executeSQL(String sqlStatement, Object parameters[]) throws SQLException {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = DBConnectionManager.getConnection();
connection.setAutoCommit(true);
statement = connection.prepareStatement(sqlStatement);
// 获取执行数据
if (parameters != null && parameters.length > 0) {
for (int i = 0; i < parameters.length; i++) {
statement.setObject(i + 1, parameters[i]);
}
}
statement.execute();
} catch (SQLException ex) {
ex.printStackTrace();
throw ex;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
statement.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
try {
connection.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
return;
}

public void executeSQL(String sqlStatement, List parameters) throws SQLException {
Connection connection = null;
PreparedStatement statement = null;
try {
connection = DBConnectionManager.getConnection();
connection.setAutoCommit(true);
statement = connection.prepareStatement(sqlStatement);
// 获取执行数据
int listsize = parameters.size();
for (int i = 0; i < listsize; i++) { statement.setObject(i + 1, parameters.get(i)); }
statement.execute();
} catch (SQLException ex) {
ex.printStackTrace();
throw ex;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
statement.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
try {
connection.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
return;
}

public void executeSQL(String[] sqlStatement) throws SQLException {
Connection connection = null;
PreparedStatement statement = null;
if (sqlStatement == null || sqlStatement.length == 0) {
throw new SQLException();
}
try {
connection = DBConnectionManager.getConnection();
connection.setAutoCommit(false);
int slength = sqlStatement.length;// 事务中sql语句的个数
for (int i = 0; i < slength; i++) {
statement = connection.prepareStatement(sqlStatement[i]);
statement.execute();
}
connection.commit();
} catch (SQLException ex) {
connection.rollback();
ex.printStackTrace();
throw ex;
} catch (Exception ex) {
connection.rollback();
ex.printStackTrace();
} finally {
try {
statement.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
try {
connection.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
return;
}

public void executeSQL(String[] sqlStatement, List parameters) throws SQLException {
Connection connection = null;
PreparedStatement statement = null;
if (sqlStatement == null || sqlStatement.length == 0) {
throw new SQLException();
}
try {
connection = DBConnectionManager.getConnection();
connection.setAutoCommit(false);
int slength = sqlStatement.length;// 事务中sql语句的个数
for (int i = 0; i < slength; i++) {
statement = connection.prepareStatement(sqlStatement[i]);
if (parameters != null) {
Object[] pm = (Object[]) parameters.get(i);
// 获取执行数据
int pmsize = 0;// 每条sql数据对应的参数个数
if (pm != null) {
pmsize = pm.length;
}
for (int j = 0; j < pmsize; j++) {
statement.setObject(j + 1, pm[j]);
}
}
statement.execute();
}
connection.commit();
} catch (SQLException ex) {
connection.rollback();
ex.printStackTrace();
throw ex;
} catch (Exception ex) {
connection.rollback();
ex.printStackTrace();
} finally {
try {
statement.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
try {
connection.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
return;
}

public Vector executeQuerySQL(String sqlStatement) throws SQLException {
Vector resultVector = new Vector();
Connection connection = null;
PreparedStatement statement = null;
ResultSet rs = null;
try {
connection = DBConnectionManager.getConnection(); // 获取数据库连接对象
connection.setAutoCommit(false);
statement = connection.prepareStatement(sqlStatement); // 获取statement对象
statement.execute();
rs = statement.getResultSet(); // 获取查询记录集
HashMap rowItem;
for (; rs.next(); resultVector.add(rowItem)) {
rowItem = new HashMap();
int columnCount = rs.getMetaData().getColumnCount();
for (int i = 1; i <= columnCount; i++) {
Object cell = rs.getObject(i);
if (cell == null) {
cell = "";
} else if (cell instanceof Blob) {
try {
InputStreamReader in = new InputStreamReader(((Blob) cell).getBinaryStream());
char bufferString[] = new char[5000];
int readCharCount = in.read(bufferString);
StringBuffer bolbStringBuffer = new StringBuffer("");
for (; readCharCount == 5000; readCharCount = in.read(bufferString)) {
bolbStringBuffer.append(bufferString, 0, readCharCount);
}
if (readCharCount != -1) {
bolbStringBuffer.append(bufferString, 0, readCharCount);
}
cell = bolbStringBuffer.toString();
in.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
rowItem.put(rs.getMetaData().getColumnName(i), cell);
}
}
} catch (SQLException ex) {
ex.printStackTrace();
throw ex;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
rs.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
try {
statement.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
try {
connection.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
return resultVector;
}

public Vector executeQuerySQL(String sqlStatement, Object parameters[]) throws SQLException {
Vector resultVector = new Vector();
Connection connection = null;
PreparedStatement statement = null;
ResultSet rs = null;
try {
connection = DBConnectionManager.getConnection(); // 获取数据库连接对象
connection.setAutoCommit(false);
statement = connection.prepareStatement(sqlStatement); // 获取statement对象
// 获取查询参数
if (parameters != null && parameters.length > 0) {
for (int i = 0; i < parameters.length; i++) {
statement.setObject(i + 1, parameters[i]);
}
}
statement.execute();
rs = statement.getResultSet(); // 获取查询记录集
HashMap rowItem;
for (; rs.next(); resultVector.add(rowItem)) {
rowItem = new HashMap();
int columnCount = rs.getMetaData().getColumnCount();
for (int i = 1; i <= columnCount; i++) {
Object cell = rs.getObject(i);
if (cell == null) {
cell = "";
} else if (cell instanceof Blob) {
try {
InputStreamReader in = new InputStreamReader(((Blob) cell).getBinaryStream());
char bufferString[] = new char[5000];
int readCharCount = in.read(bufferString);
StringBuffer bolbStringBuffer = new StringBuffer("");
for (; readCharCount == 5000; readCharCount = in.read(bufferString)) {
bolbStringBuffer.append(bufferString, 0, readCharCount);
}
if (readCharCount != -1) {
bolbStringBuffer.append(bufferString, 0, readCharCount);
}
cell = bolbStringBuffer.toString();
in.close();
} catch (Exception ex) {
ex.printStackTrace();
log.error(ex.getMessage());
}
}
rowItem.put(rs.getMetaData().getColumnName(i), cell);
}
}
} catch (SQLException ex) {
ex.printStackTrace();
throw ex;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
rs.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
try {
statement.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
try {
connection.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
return resultVector;
}

public Vector executeQuerySQL(String sqlStatement, List parameters) throws SQLException {
Vector resultVector = new Vector();
Connection connection = null;
PreparedStatement statement = null;
ResultSet rs = null;
try {
connection = DBConnectionManager.getConnection(); // 获取数据库连接对象
connection.setAutoCommit(false);
statement = connection.prepareStatement(sqlStatement); // 获取statement对象
// 获取查询数据
int listsize = parameters.size();
for (int i = 0; i < listsize; i++) {
statement.setObject(i + 1, parameters.get(i));
}
statement.execute();
rs = statement.getResultSet(); // 获取查询记录集
HashMap rowItem;
for (; rs.next(); resultVector.add(rowItem)) {
rowItem = new HashMap();
int columnCount = rs.getMetaData().getColumnCount();
for (int i = 1; i <= columnCount; i++) {
Object cell = rs.getObject(i);
if (cell == null) {
cell = "";
} else if (cell instanceof Blob) {
try {
InputStreamReader in = new InputStreamReader(((Blob) cell).getBinaryStream());
char bufferString[] = new char[5000];
int readCharCount = in.read(bufferString);
StringBuffer bolbStringBuffer = new StringBuffer("");
for (; readCharCount == 5000; readCharCount = in.read(bufferString)) {
bolbStringBuffer.append(bufferString, 0, readCharCount);
}
if (readCharCount != -1) {
bolbStringBuffer.append(bufferString, 0, readCharCount);
}
cell = bolbStringBuffer.toString();
in.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
rowItem.put(rs.getMetaData().getColumnName(i), cell);
}
}
} catch (SQLException ex) {
ex.printStackTrace();
throw ex;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
rs.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
try {
statement.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
try {
connection.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
return resultVector;
}

public String getSequenceNum(String tableName) throws SQLException {
Connection connection = null;
PreparedStatement statement = null;
ResultSet rs = null;
String squenceNumber = "";
String sequenceStatement = "select " + tableName + "_seq.nextval from dual";
try {
connection = DBConnectionManager.getConnection();
statement = connection.prepareStatement(sequenceStatement);
rs = statement.executeQuery();
rs.next();
squenceNumber = rs.getBigDecimal(1).toString();
} catch (SQLException ex) {
ex.printStackTrace();
throw ex;
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
rs.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
try {
statement.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
try {
connection.close();
} catch (SQLException ex) {
ex.printStackTrace();
}
}
return squenceNumber;
}
}

分享到:
评论

相关推荐

    java oracle 数据库 连接池 小例子

    java oracle 数据库 连接池 小例子 里面很详尽的使用C3p0,配置文件, 可以成功运行, 只需要修改一下配置文件里面的驱动就好了

    c3p0数据库连接池案例

    c3p0数据库连接池案例,java项目,例子结构清晰,适合新手学习

    Java各数据库连接池配置介绍

    详细介绍常用Java各数据库连接池配置,包括C3P0,DBCP,Proxool等。

    c3p0下载 数据库连接池jar包

    数据库连接池jar包 内置了c3p0-config.xml配置文件 jar包在lib中 c3p0-0.9.5.5.jar c3p0-oracle-thin-extras-0.9.5.5.jar mchange-commons-java-0.2.19.jar

    oracle9i 自带连接池Java代码

    oracle9i 自带连接池Java代码 可以帮你快速 进入基于oracle数据库 的j2ee 开发

    C3P0连接池管理类

    C3P0 数据库连接池 管理类 驱动包 只要在DBManager.java内加上自己的oracle地址和用户名密码即可,如果是用别的数据库,则把相应的数据库连接地址改改就成

    JDBC封装类升级版,支持DBCP、C3P0连接池,Java连接数据库带例子

    Java JDBC封装类升级版,带增删改查例子,支持oracle,MySql,hsqldb 等,支持事务,返回数据格式 支持二维数组,MAP格式,以及javabean对象。有利于初学者DbDemo.java为demo,Connect为jdbc封装类,可以作为项目共通类...

    c3p0-0.9.5.2

    c3p0连接池jar包,0.9.5.2版本,包括mchange-commons-java-0.2.11.jar、c3p0-oracle-thin-extras-0.9.5.2.jar、c3p0-0.9.5.2.jar。

    osgi数据库连接demo

    osgi数据库连接一章demo,c3p0配置oracle连接池和ibatis在osgi框架中的使用

    java开发数据库连接

    操作mysql,sqlserver,oracle,所使用到的相关的连接的jar包,同时包含了c3p0连接池的包

    JAVA上百实例源码以及开源项目源代码

    Java源代码实现部分,比较有意思,也具参考性。像坐标控制、旋转矩阵、定时器、生成图像、数据初始化、矩阵乘法、坐标旋转、判断是否是顺时针方向排列、鼠标按下、放开时的动作等,都可在本源码中得以体现。 Java...

    JAVA上百实例源码以及开源项目

    Java源代码实现部分,比较有意思,也具参考性。像坐标控制、旋转矩阵、定时器、生成图像、数据初始化、矩阵乘法、坐标旋转、判断是否是顺时针方向排列、鼠标按下、放开时的动作等,都可在本源码中得以体现。 Java...

    java开源包4

    BoneCP 是一个高性能的开源java数据库连接池实现库。它的设计初衷就是为了提高数据库连接池的性能,根据某些测试数据发现,BoneCP是最快的连接池。BoneCP很小,只有四十几K(运行时需要slf4j和guava的支持,这二者加...

    java开源包10

    BoneCP 是一个高性能的开源java数据库连接池实现库。它的设计初衷就是为了提高数据库连接池的性能,根据某些测试数据发现,BoneCP是最快的连接池。BoneCP很小,只有四十几K(运行时需要slf4j和guava的支持,这二者加...

    java开源包8

    BoneCP 是一个高性能的开源java数据库连接池实现库。它的设计初衷就是为了提高数据库连接池的性能,根据某些测试数据发现,BoneCP是最快的连接池。BoneCP很小,只有四十几K(运行时需要slf4j和guava的支持,这二者加...

    java开源包3

    BoneCP 是一个高性能的开源java数据库连接池实现库。它的设计初衷就是为了提高数据库连接池的性能,根据某些测试数据发现,BoneCP是最快的连接池。BoneCP很小,只有四十几K(运行时需要slf4j和guava的支持,这二者加...

    java开源包11

    BoneCP 是一个高性能的开源java数据库连接池实现库。它的设计初衷就是为了提高数据库连接池的性能,根据某些测试数据发现,BoneCP是最快的连接池。BoneCP很小,只有四十几K(运行时需要slf4j和guava的支持,这二者加...

    java开源包6

    BoneCP 是一个高性能的开源java数据库连接池实现库。它的设计初衷就是为了提高数据库连接池的性能,根据某些测试数据发现,BoneCP是最快的连接池。BoneCP很小,只有四十几K(运行时需要slf4j和guava的支持,这二者加...

    java开源包9

    BoneCP 是一个高性能的开源java数据库连接池实现库。它的设计初衷就是为了提高数据库连接池的性能,根据某些测试数据发现,BoneCP是最快的连接池。BoneCP很小,只有四十几K(运行时需要slf4j和guava的支持,这二者加...

Global site tag (gtag.js) - Google Analytics