Chinaunix首页 | 论坛 | 博客
  • 博客访问: 408760
  • 博文数量: 114
  • 博客积分: 7010
  • 博客等级: 少将
  • 技术积分: 1395
  • 用 户 组: 普通用户
  • 注册时间: 2008-07-05 17:54
文章分类

全部博文(114)

文章存档

2011年(2)

2009年(1)

2008年(111)

我的朋友

分类: Java

2008-08-15 19:24:17

建立连接池工厂类:
 

package com.viita;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;

public class ConnectionFactory {
    private static int min = 1;
    private static int max = 10;
    private static ArrayList colls = new ArrayList();
    
    static
    {
        for (int i = 0; i < min; i++) {
            MyConnection my = new MyConnection(getNewConnection());
            colls.add(my);
        }
    }
    
    private static Connection getNewConnection()
    {
        Connection conn = null;
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            conn = DriverManager.getConnection("jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=C:\\accp.mdb","","");
        } catch (Exception e) {
            // TODO Auto-generated catch block

            e.printStackTrace();
        }
        return conn;
    }
    
    public static void increment()
    {
        if(colls.size()<max)
        {
            MyConnection my = new MyConnection(getNewConnection());
            colls.add(my);
        }
    }
    
    
    public static Connection getConnection()
    {
        Connection conn = null;
        for (int i = 0; i < colls.size(); i++) {
            MyConnection my = (MyConnection)colls.get(i);
            if(!my.isIsuserd())
            {
                my.setIsuserd(true);
                conn = my;
                return conn;
            }
        }
        increment();
        if(colls.size()>=10) return null;
        return getConnection();
    }
}

 

建立自己连接类:

 

package com.viita;

import java.sql.CallableStatement;
import java.sql.Connection;
import java.sql.DatabaseMetaData;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.SQLWarning;
import java.sql.Savepoint;
import java.sql.Statement;
import java.util.Map;

public class MyConnection implements Connection{
    private Connection conn ;
    private boolean isuserd = false;
    public MyConnection(Connection conn)
    {
        this.conn = conn;
    }

    public void clearWarnings() throws SQLException {
        // TODO Auto-generated method stub

        
    }
    
    

    public void close() throws SQLException {
        // TODO Auto-generated method stub

        this.isuserd = false;
    }

    public void commit() throws SQLException {
        // TODO Auto-generated method stub

        
    }

    public Statement createStatement() throws SQLException {
        // TODO Auto-generated method stub

        return conn.createStatement();
    }

    public Statement createStatement(int arg0, int arg1) throws SQLException {
        // TODO Auto-generated method stub

        return conn.createStatement(arg0,arg1);
    }

    public Statement createStatement(int arg0, int arg1, int arg2) throws SQLException {
        // TODO Auto-generated method stub

        return null;
    }

    public boolean getAutoCommit() throws SQLException {
        // TODO Auto-generated method stub

        return false;
    }

    public String getCatalog() throws SQLException {
        // TODO Auto-generated method stub

        return null;
    }

    public int getHoldability() throws SQLException {
        // TODO Auto-generated method stub

        return 0;
    }

    public DatabaseMetaData getMetaData() throws SQLException {
        // TODO Auto-generated method stub

        return null;
    }

    public int getTransactionIsolation() throws SQLException {
        // TODO Auto-generated method stub

        return 0;
    }

    public Map<String, Class<?>> getTypeMap() throws SQLException {
        // TODO Auto-generated method stub

        return null;
    }

    public SQLWarning getWarnings() throws SQLException {
        // TODO Auto-generated method stub

        return null;
    }

    public boolean isClosed() throws SQLException {
        // TODO Auto-generated method stub

        return false;
    }

    public boolean isReadOnly() throws SQLException {
        // TODO Auto-generated method stub

        return false;
    }

    public String nativeSQL(String arg0) throws SQLException {
        // TODO Auto-generated method stub

        return null;
    }

    public CallableStatement prepareCall(String arg0) throws SQLException {
        // TODO Auto-generated method stub

        return null;
    }

    public CallableStatement prepareCall(String arg0, int arg1, int arg2) throws SQLException {
        // TODO Auto-generated method stub

        return null;
    }

    public CallableStatement prepareCall(String arg0, int arg1, int arg2, int arg3) throws SQLException {
        // TODO Auto-generated method stub

        return null;
    }

    public PreparedStatement prepareStatement(String arg0) throws SQLException {
        // TODO Auto-generated method stub

        return null;
    }

    public PreparedStatement prepareStatement(String arg0, int arg1) throws SQLException {
        // TODO Auto-generated method stub

        return null;
    }

    public PreparedStatement prepareStatement(String arg0, int[] arg1) throws SQLException {
        // TODO Auto-generated method stub

        return null;
    }

    public PreparedStatement prepareStatement(String arg0, String[] arg1) throws SQLException {
        // TODO Auto-generated method stub

        return null;
    }

    public PreparedStatement prepareStatement(String arg0, int arg1, int arg2) throws SQLException {
        // TODO Auto-generated method stub

        return null;
    }

    public PreparedStatement prepareStatement(String arg0, int arg1, int arg2, int arg3) throws SQLException {
        // TODO Auto-generated method stub

        return null;
    }

    public void releaseSavepoint(Savepoint arg0) throws SQLException {
        // TODO Auto-generated method stub

        
    }

    public void rollback() throws SQLException {
        // TODO Auto-generated method stub

        
    }

    public void rollback(Savepoint arg0) throws SQLException {
        // TODO Auto-generated method stub

        
    }

    public void setAutoCommit(boolean arg0) throws SQLException {
        // TODO Auto-generated method stub

        
    }

    public void setCatalog(String arg0) throws SQLException {
        // TODO Auto-generated method stub

        
    }

    public void setHoldability(int arg0) throws SQLException {
        // TODO Auto-generated method stub

        
    }

    public void setReadOnly(boolean arg0) throws SQLException {
        // TODO Auto-generated method stub

        
    }

    public Savepoint setSavepoint() throws SQLException {
        // TODO Auto-generated method stub

        return null;
    }

    public Savepoint setSavepoint(String arg0) throws SQLException {
        // TODO Auto-generated method stub

        return null;
    }

    public void setTransactionIsolation(int arg0) throws SQLException {
        // TODO Auto-generated method stub

        
    }

    public void setTypeMap(Map<String, Class<?>> arg0) throws SQLException {
        // TODO Auto-generated method stub

        
    }

    public boolean isIsuserd() {
        return isuserd;
    }

    public void setIsuserd(boolean isuserd) {
        this.isuserd = isuserd;
    }

}

 

测试Main方法

 

package com.viita;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

public class Test {
    private void testpool() {
        // TODO Auto-generated method stub

        Connection conn = ConnectionFactory.getConnection();
        Statement stmt;
        try {
            stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select * from test");
            if(rs.next())
            {
                rs.getString(1);
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block

            e.printStackTrace();
        }finally
        {
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block

                e.printStackTrace();
            }
        }
    }
    
    private void testbupool() {
        // TODO Auto-generated method stub

        Connection conn = null;
        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            conn = DriverManager.getConnection("jdbc:odbc:Driver={MicroSoft Access Driver (*.mdb)};DBQ=C:\\accp.mdb","","");
            
            Statement stmt;
            stmt = conn.createStatement();
            ResultSet rs = stmt.executeQuery("select * from test");
            if(rs.next())
            {
                rs.getString(1);
            }
        } catch (Exception e) {
            // TODO Auto-generated catch block

            e.printStackTrace();
        }finally
        {
            try {
                conn.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block

                e.printStackTrace();
            }
        }
    }
    public static void main(String[] args) throws Exception {
        Test t = new Test();
        long start = System.currentTimeMillis();
        for (int i = 0; i < 100; i++) {
            t.testbupool();
        }
        long end = System.currentTimeMillis();
        System.out.println(end-start);
    }
}

 

效率比较大家在自己的机子上就可看出效果  很明显

阅读(618) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~