日期:2014-05-16  浏览次数:20489 次

与数据库连接的封装

前几天的时候一直在做与数据库的连接,虽然也是用到了数据库连接池进行连接,但是与数据库连接这方面很混乱,所以在这儿学习一下。如何将数据库的链接做的更加简洁和清楚。

?

1,首先先定义一个Exception。建立一个package(org.hrs.report.Exception)。

package org.hrs.report.Exception;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hrs.report.listener.AppListener;


public class DAOException extends Exception {
	private static Log log = LogFactory.getLog(AppListener.class.getName());
	public String toSting(){
		String string = "????t???sql?????";
		log.info(string);
		return string;
	}

}

?

2,建立一个package(org.hrs.report.common.inf)和一个接口。

package org.hrs.report.common.inf;

import java.sql.Connection;
import org.hrs.report.Exception.DAOException;


public interface DAO {

	public Connection getConnection() throws DAOException;
}

?

?

3,定义数据库连接池

package org.hrs.report.common;

import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;


public class Cache {
private static Cache instance;
private DataSource dataSource;

  static {
	  try {
		instance=new Cache();
	} catch (NamingException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}
	  
  }
  private Cache() throws NamingException{
	 Context context=null; 
	 

		try {
			context = new InitialContext();
			dataSource = (DataSource) context
					.lookup("java:/comp/env/HRS");
		} catch (NamingException e) {
			e.printStackTrace();
		}		
		
	
  }
  public static Cache getInstance(){
	  
	  return instance;
  }
  
  public DataSource getDataSource(){
	  
	  return dataSource;
  }
	
}

?

4,将第二步的接口,实现它。

package org.hrs.report.common;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.DataSource;

import org.hrs.report.Exception.DAOException;
import org.hrs.report.common.inf.DAO;


public class DAOBASE implements DAO {

	public Connection getConnection() throws DAOException {
		Connection connection=null;
		DataSource dataSource=Cache.getInstance().getDataSource();
		if(dataSource!=null)
		{
			try {
				connection=dataSource.getConnection();
			} catch (SQLException e) {
				 e.printStackTrace();
			}
		}
		
		return connection;
	}

}

?

5,定义一些数据库的操作方法。

package org.hrs.report.common;
import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.sql.PreparedStatement;

import org.hrs.report.Exception.DAOException;

public final class DBoperator extends DAOBASE{
    
	private Connection connection = null;
	private Statement cstmt = null;
	private ResultSet rstRet = null;
   
   
	public void Execute(String sql) throws SQLException {
		try {
			connection = getConnection();
			cstmt = connection.createStatement();

			cstmt.executeQuery(sql);
//			dbclose();

		} catch (DAOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		} finally {
			dbclose();
		}

	}
	
	
	
	
    public ArrayList sqlExecute(String sql) throws SQLException {

		ArrayList alResult = new ArrayList();
		try {
			connection = getConnection();
			cstmt = connection.createStatement();
			rstRet = (ResultSet) cstmt.executeQuery(sql);
			ResultSetMetaData rsmd = rstRet.getMetaData();
			int numCols = rsmd.getColumnCount();

			while (rstRet.next()) {
				String strTempArray[] = new String[numCols];
				for (int i = 1; i <=