开心人生

用今天的努力----实现我所向往的明天

导航

被我修改过的BaseDao

package com.newglobal.sql;

import java.io.File;
import java.io.FileInputStream;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Hashtable;
import java.util.Iterator;
import java.util.List;
import java.util.Properties;

@SuppressWarnings("unchecked")
public class BaseDao {
    //private static final String idTable = "table_sequence";
    //private static BasicDataSource     ds = null;
    private static Hashtable        ht_ds = new Hashtable();
    private static Object            syn_ht_ds = new Object();
    private static int                init_dbid = 0;
    private static Object            syn_init_dbid = new Object();
    private int                       dbid              = 0;
    private DBConn_Info                dbconn_info     = null;
    private DBConn_Info        db_center_conn_info     = null;
    private static int get_dbid(){
        synchronized(syn_init_dbid){
            init_dbid ++;
            if(init_dbid == 0x7fffffff) init_dbid = 1;
        }
        return init_dbid;
    }
    private static int get_dbid_fromtable(DBConn_Info info){
        int             id = -1;
        DBConn_Info        conn_info = null;
        String            key = "";
        synchronized(syn_ht_ds){
            Iterator    iter = ht_ds.keySet().iterator();
            while(iter.hasNext()){
                key = (String)iter.next();
                conn_info = (DBConn_Info)ht_ds.get(key);
                if(conn_info.compare(info)){
                    id = Integer.parseInt(key);
                    break;
                }
            }
        }
        return id;
    }
    private static DBConn_Info get_dbconninfo(int id){
        DBConn_Info        conn_info = null;
        synchronized(syn_ht_ds){
            conn_info = (DBConn_Info)ht_ds.get(""+id);
        }
        return conn_info;
    }
    private static void put_dbconninfo(int id, DBConn_Info info){
        synchronized(syn_ht_ds){
            ht_ds.put(""+id, info);
        }
    }
    public void set_oracle_params(String ip, int port, String dbname, String username, String password,
            int poolsize){
        DBConn_Info    dbc_info = new DBConn_Info();
        dbc_info.driver = "oracle.jdbc.driver.OracleDriver";
        dbc_info.url    = "jdbc:oracle:thin:@"+ip+":"+port+":"+dbname;
        dbc_info.username = username;
        dbc_info.password = password;
        dbc_info.poolsize = poolsize;
        int id = get_dbid_fromtable(dbc_info);
        if(id == -1){
            id = get_dbid();
            put_dbconninfo(id, dbc_info);
        }else{
            DBConn_Info old_info = get_dbconninfo(id);
            if(old_info.poolsize < dbc_info.poolsize){
                old_info.poolsize = dbc_info.poolsize;
                if(old_info.ds != null) old_info.ds.setMaxConnCounts(old_info.poolsize);
            }
        }
        dbid = id;
    }
    public long get_next_keyval(String seqname){
        long id = 0;
        String sql="select "+seqname+".nextval from dual";
        List list = getBySql(sql);
        if(list.size() > 0){
            ResultData rsd = (ResultData)list.get(0);
            id = rsd.getLong("nextval");
        }
        return id;
    }
    public long get_curr_keyval(String seqname){
        long id = 0;
        String sql="select "+seqname+".currval from dual";
        List list = getBySql(sql);
        if(list.size() > 0){
            ResultData rsd = (ResultData)list.get(0);
            id = rsd.getLong("currval");
        }
        return id;
    }
    public ArrayList getBySql(String sql){
        ArrayList            arrlist = new ArrayList();
        Connection            conn = null;
        PreparedStatement    ps     = null;
        ResultSet            rs     = null;
        ResultSetMetaData    rsmd = null;
        try{
            conn    = getConnection();
            ps       = conn.prepareStatement(sql);
            rs        = ps.executeQuery();
            rsmd    = rs.getMetaData();
            while(rs.next()){
                ResultData rsd = new ResultData();
                for(int i = 1; i <= rsmd.getColumnCount(); i ++){
                    String name = rsmd.getColumnName(i).toLowerCase();
                    Object obj  = rs.getObject(i);
                    //System.out.println("name = " + name+" obj = "+obj.toString());
                    rsd.put(name, obj);
                }
                arrlist.add(rsd);
            }
        }catch(Exception e){
            e.printStackTrace();
        }finally{
            close(rs);
            close(ps);
            close(conn);
        }
        return arrlist;
    }
    public int updateBySql(String sql) {

        //System.out.println(sql);
        //long t0 = System.currentTimeMillis();
        Connection conn = null;
        PreparedStatement ps = null;
        int ret = 0;
        try {
            conn = getConnection();
            ps = conn.prepareStatement(sql);
            ret = ps.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            close(ps);
            close(conn);
        }
        /*long t1 = System.currentTimeMillis();
        long elapsed = (t1 - t0) / 1000;
        System.out.println("Affected " + ret + " records in " + elapsed
                + " seconds.");*/
        return ret;
    }
    private DBConn_Info get_conn_info_fromfile(){
        DBConn_Info info = null;
        try{
            String fname = "config/dbconf.properties";
            Properties p = new Properties();
            File file = new File(fname);
            p.load(new FileInputStream(file));
            info = new DBConn_Info();
            info.driver = p.getProperty("driver");
            info.url     = p.getProperty("url");
            info.username = p.getProperty("username");
            info.password = p.getProperty("password");
            info.poolsize = Integer.parseInt(p.getProperty("poolsize"));
            put_dbconninfo(dbid, info);
        }catch(Exception e){
            e.printStackTrace();
        }
        return info;
    }
    private DBConn_Info get_center_conn_info_fromfile(){
        DBConn_Info info = null;
        try{
            String fname = "config/dbcenterconf.properties";
            Properties p = new Properties();
            File file = new File(fname);
            p.load(new FileInputStream(file));
            info = new DBConn_Info();
            info.driver = p.getProperty("driver");
            info.url     = p.getProperty("url");
            info.username = p.getProperty("username");
            info.password = p.getProperty("password");
            info.poolsize = Integer.parseInt(p.getProperty("poolsize"));
            put_dbconninfo(dbid, info);
        }catch(Exception e){
            e.printStackTrace();
        }
        return info;
    }
    public synchronized Connection getConnection() throws SQLException, ClassNotFoundException{
        Connection            conn = null;
        if(dbconn_info == null){
            dbconn_info = get_dbconninfo(dbid);
            if(dbconn_info == null) dbconn_info = get_conn_info_fromfile();
            if(dbconn_info == null) return null;
            if(dbconn_info.ds == null){
                dbconn_info.ds = new BasicDataSource();
                dbconn_info.set_datasource();
            }
        }
        if(dbconn_info.ds != null){
            conn = dbconn_info.ds.getConnection();
        }
        return conn;
    }
    public synchronized Connection getConnectionCenter() throws SQLException, ClassNotFoundException{
        Connection            conn = null;
        if(db_center_conn_info == null){
            db_center_conn_info = get_dbconninfo(dbid);
            if(db_center_conn_info == null) db_center_conn_info = get_center_conn_info_fromfile();
            if(db_center_conn_info == null) return null;
            if(db_center_conn_info.ds == null){
                db_center_conn_info.ds = new BasicDataSource();
                db_center_conn_info.set_datasource();
            }
        }
        if(db_center_conn_info.ds != null){
            conn = db_center_conn_info.ds.getConnection();
        }
        return conn;
    }

    /*public synchronized static Connection getConnectionCenter() throws IOException, SQLException, ClassNotFoundException{
        Connection conn = null;
        if (ds == null) {
            // called in an application instead of a web app. usually for
            // some
            // testing.
            String fname = "config/dbconf.properties";
            Properties p = new Properties();
            File file = new File(fname);
            p.load(new FileInputStream(file));
            String driver = p.getProperty("driver");
            String url = p.getProperty("url");
            String username = p.getProperty("username");
            String password = p.getProperty("password");
            String poolsize = p.getProperty("poolsize");
            ds = BasicDataSource.getBasicDataSource();
        }
        if (ds != null) {
            conn = ds.getConnection();
        }
        return conn;
    }*/

    public void close(ResultSet rs) {
        if (rs != null) {
            try {
                rs.close();
            } catch (SQLException e) {
            }
            rs = null;
        }
    }

    public void close(PreparedStatement ps) {
        if (ps != null) {
            try {
                ps.close();
            } catch (SQLException e) {
            }
            ps = null;
        }
    }

    public void close(Connection conn) {
        if (conn != null) {
            if(dbconn_info != null) {
                if(dbconn_info.ds != null) dbconn_info.ds.close(conn);
            }
            conn = null;
        }
    }
    public void closeCenter(Connection conn) {
        if (conn != null) {
            if(dbconn_info != null) {
                if(dbconn_info.ds != null) dbconn_info.ds.close(conn);
            }
            conn = null;
        }
    }
}

posted on 2008-10-16 09:48  hai  阅读(689)  评论(0)    收藏  举报