⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 dbaccess.java

📁 用java开发的一些实用的短信通信模块其中包含MD5加密、http发送等信息
💻 JAVA
字号:
package lib.commons.dal;

import java.sql.*;
import java.util.Date;
import java.util.List;
import java.util.ArrayList;
import java.util.Iterator;

import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.logging.Log;

import lib.commons.Utils;

public class DbAccess {
	private Connection connection;

	private List stmtList = new ArrayList();

	public DbAccess(Connection connection) {
		this.connection = connection;
	}

	public Connection getConnection() {
		return this.connection;
	}

	public boolean isClosed() {
		boolean closed = true;
		try {
			closed = (null == this.connection || this.connection.isClosed());
		} catch (SQLException err) {
			Log log = Utils.getLog(DbAccess.class);
			log.error(err.getMessage(), err);
		}
		return closed;
	}

	public void close() throws SQLException {
		Iterator iterator = stmtList.iterator();
		while (iterator.hasNext()) {
			Statement stmt = (Statement) iterator.next();
			if (null != stmt) {
				stmt.close();
			}
		}
		stmtList.clear();
		if (null != connection && !connection.isClosed()) {
			connection.close();
		}
		connection = null;
	}

	public void closeQuietly() {
		try {
			close();
		} catch (SQLException err) {
			Log log = Utils.getLog(DbAccess.class);
			log.error(err.getMessage(), err);
		}
	}

	public void beginTransaction() throws SQLException {
		connection.setAutoCommit(false);
	}

	public void closeTransaction() throws SQLException {
		connection.setAutoCommit(true);
	}

	public void commitTransaction() throws SQLException {
		if (!connection.getAutoCommit()) {
			connection.commit();
		}
	}

	public void rollbackTransaction() throws SQLException {
		if (!connection.getAutoCommit()) {
			connection.rollback();
		}
	}

	public void rollbackTransactionQuietly() {
		try {
			rollbackTransaction();
		} catch (SQLException err) {
			Log log = Utils.getLog(DbAccess.class);
			log.error(err.getMessage(), err);
		}

	}

	public boolean isAutoCommit() {
		boolean autoCommit = true;
		if (null != connection) {
			try {
				autoCommit = connection.getAutoCommit();
			} catch (SQLException err) {
				Log log = Utils.getLog(DbAccess.class);
				log.error(err.getMessage(), err);
			}
		}
		return autoCommit;
	}

	public Statement createStatement() throws SQLException {
		return createStatement(ResultSet.TYPE_FORWARD_ONLY,
				ResultSet.CONCUR_READ_ONLY);
	}

	public Statement createStatement(int resultSetType, int resultSetConcurrency)
			throws SQLException {
		Statement stmt = connection.createStatement(resultSetType,
				resultSetConcurrency);
		stmtList.add(stmt);
		return stmt;
	}

	public PreparedStatement preparedStatement(String sql) throws SQLException {
		return preparedStatement(sql, ResultSet.TYPE_FORWARD_ONLY,
				ResultSet.CONCUR_READ_ONLY);
	}

	public PreparedStatement preparedStatement(String sql, int resultSetType,
			int resultSetConcurrency) throws SQLException {
		PreparedStatement pstmt = connection.prepareStatement(sql,
				resultSetType, resultSetConcurrency);
		stmtList.add(pstmt);
		return pstmt;
	}

	public CallableStatement prepareCall(String sql) throws SQLException {
		CallableStatement cstmt = prepareCall(sql, ResultSet.TYPE_FORWARD_ONLY,
				ResultSet.CONCUR_READ_ONLY);
		return cstmt;
	}

	public CallableStatement prepareCall(String sql, int resultSetType,
			int resultSetConcurrency) throws SQLException {
		CallableStatement cstmt = connection.prepareCall(sql, resultSetType,
				resultSetConcurrency);
		stmtList.add(cstmt);
		return cstmt;
	}

	public static Timestamp dateToTimestamp(String dateString, String dateFormat) {
		Date date = null;
		if (!Utils.StringIsNullOrEmpty(dateString)) {
			try {
				date = DateUtils.parseDate(dateString,
						new String[] { dateFormat });
			} catch (Exception err) {
				Log log = Utils.getLog(DbAccess.class);
				log.error(err.getMessage(), err);
			}
		}
		return dateToTimestamp(date);
	}

	public static Timestamp dateToTimestamp(Date date) {
		Timestamp t = null;
		if (null != date)
			t = new Timestamp(date.getTime());
		return t;
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -