abstractdealdatabaseinfo.java

来自「J2EE eclipse 下开发数据库一个插件」· Java 代码 · 共 201 行

JAVA
201
字号
package com.tanghan.db.logic;

import com.tanghan.db.util.*;
import com.tanghan.db.*;
import java.util.List;
import java.sql.Connection;
import com.tanghan.util.PageList;
import com.tanghan.util.Log;
import com.tanghan.util.TanghanException;

import org.apache.log4j.Logger;

import java.sql.*;

/** 得到数据库各种信息说需要的方法.
 * 
 * @author Jerry Tang
 * @version v0.1.0
 * @createdate 2003-3-25
 * @copyright  (C) 2003 Tanghan工作组
 */
public abstract class AbstractDealDatabaseInfo {
	
	/**存放继承了DealDatabaseInfo的所以实现类的资源文件*/
    private static java.util.ResourceBundle resBundle = null;
	/**存放一个JDBC连接的信息*/
	protected DBConnection dbConnection = null;

	protected static Logger log = Log.getInstanse().getLogger("db.deal");
	
	/**数据库连接*/
	protected Connection con = null;

	/**数据库连接的计数器*/
	protected int connectedCount = 0;

	/**
	 * Constructor for DealDatabaseInfo.
	 */
	protected AbstractDealDatabaseInfo() {
	}

	/**
	 * 根据传入的数据得到一个DealDatabaseInfo的实例
	 * @param which 那个具体类用来生成DealDatabaseInfo的实例
	 * @return DealDatabaseInfo DealDatabaseInfo的实例
	 */
	public static AbstractDealDatabaseInfo getInstance(String which){
		loadResBundle();
        AbstractDealDatabaseInfo obj = null;
        try{
            obj =  (AbstractDealDatabaseInfo)Class.forName(resBundle.getString(which).trim()).newInstance();
        }catch(Exception e){
        	log.error(e.toString(),e);
//        	Debug.dealException(e);
        }
        return obj;		
	}
	/**
	 * 根据传入的数据得到一个DealDatabaseInfo的实例
	 * @param which 那个具体类用来生成DealDatabaseInfo的实例
	 * @param conn DBConnection的一个实例
	 * @return DealDatabaseInfo DealDatabaseInfo的实例
	 */	
	public static AbstractDealDatabaseInfo getInstance(String which,DBConnection conn){
		loadResBundle();
        AbstractDealDatabaseInfo obj = null;
        try{
            obj = (AbstractDealDatabaseInfo)Class.forName(resBundle.getString(which).trim()).newInstance();
            obj.setDbConnection(conn);
        }catch(Exception e){
			log.error(e.toString(),e);
        }
        return obj;		
	}	
	
	/**
	 * 载入资源文件
	 */
    private static void loadResBundle(){
    	if(resBundle==null){
            resBundle = java.util.ResourceBundle.getBundle("com.tanghan.db.logic.DealDatabaseResource");
    	}
    }
    	
	/**
	 * 根据table名,得到这个表的所有属性,如果table为空,则该表不存在
	 * @param tableName 表名
	 * @return Table 该表的所有属性
	 */
	public abstract Table getTableInfo(String tableName);
	
	/**
	 * 得到在当前库中,所有表和视图的列表
	 * @return List 表和视图的列表
	 * @throws 列外
	 */
	public abstract List getTableList() throws TanghanException;
	
	/**
	 * 根据table名,得到这个表的所有字段
	 * @param tableName  表名
	 * @return List 表的所有字段
	 */
	public abstract List getTableField(String tableName);
	
	/**
	 * 根据表名和字段名,得到该表的这个字段的所有属性
	 * @param tableName 表名
	 * @param fieldName 字段名
	 * @return Field 字段
	 */
	public abstract Field getTableFieldInfo(String tableName,String fieldName);

	/**
	 * 根据表名,得到该表的数据,
	 * @param tableName 表名
	 * @param pageList 每页显示的数据的多少
	 * @return String[][] 数据列表
	 */
	public abstract String[][] getNotes(String tableName,PageList pageList);
	
	/**
	 * 得到该数据库连接的Schema
	 * @return String[] Schema列表
	 */
	public abstract List getSchemas() throws TanghanException;
	/**
	 * 执行SQL语句
	 * @return 执行SQL语句的结果
	 * @throws TanghanException 
	 */
	public abstract SQLResult executeSQL(String sql) throws TanghanException;
		
	/**
	 * 根据DBConnection得到一个连接
	 * @return Connection 一个连接, 可能为null
	 */
	protected Connection getConnection() throws TanghanException{
		try {
			boolean check = false;
			if(con==null){
				check =true;
			}else if(con.isClosed()){
				check =true;
			}
			if(check){
				Class.forName(this.dbConnection.getJdbcDriverClass()).newInstance();
				con = DriverManager.getConnection(
						this.dbConnection.getDatabaseURL(),
						this.dbConnection.getUserName(),
						this.dbConnection.getPassword());
				log.info("getConnection() complite successfully!");
				connectedCount = 0;
			}
		} catch (Exception e) {
			throw new TanghanException(e);
		}
		connectedCount++;
		return con;	
	}
	
	/**
	 * 关闭连接
	 * @param conn 需要关闭的连接
	 */
	protected void closeConnection(){
		connectedCount--;
		if(connectedCount<1){
			connectedCount = 0;
			try{
				if(con!=null){
					con.close();
				}
				con = null;
			}catch(Exception e){
				log.error(e.toString(),e);
			}
		}
	}
	
	/**
	 * Returns the conn.
	 * @return DBConnection
	 */
	public DBConnection getDbConnection() {
		return dbConnection;
	}

	/**
	 * Sets the conn.
	 * @param conn The conn to set
	 */
	public void setDbConnection(DBConnection conn) {
		this.dbConnection = conn;
		connectedCount = 0;
		closeConnection();
	}

}

⌨️ 快捷键说明

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