此回复为自动发出,仅用于显示而已,并无任何其他特殊作用
楼主【YiYang15848184726】截止到2008-06-30 15:29:56的历史汇总数据(不包括此帖):
发帖数:4                  发帖分:80                 
结贴数:2                  结贴分:40                 
未结数:2                  未结分:40                 
结贴率:50.00 %            结分率:50.00 %            
楼主加油

解决方案 »

  1.   

    想说的是,连接池非常复杂,不要重复发明轮子了,知道原理就成。
    具体使用的话,可以用开源的 DBCP、C3P0、Proxool 这些都是很不错的。
      

  2.   


    package com.db; 
    import java.sql.*; 
    import java.io.*; public final class DataBaseOperator {     public DataBaseOperator() { 
        } 
        /** 
        *创建一个连接公用的 
        */ 
        public static Connection createConnection(String driver,String url,String user,String pwd){ 
            Connection con=null; 
            try{ 
                Class.forName(driver); 
                con=DriverManager.getConnection(url,user,pwd); 
            }catch(ClassNotFoundException ce){ 
                ce.printStackTrace(); 
            }catch(SQLException se){ 
                se.printStackTrace(); 
            } 
            return con; 
        } 
        /** 
        *创建一个连接纯驱动 
        */ 
        public static Connection createConnection(){ 
            String driver="com.microsoft.jdbc.sqlserver.SQLServerDriver"; 
            String url="jdbc:microsoft:sqlserver://127.0.0.1:1433;databaseName=pubs"; 
            String user="sa"; 
            String pwd=""; 
            return createConnection(driver,url,user,pwd); 
        } 
        /** 
        *创建一个连接通过配制文件 
        */ 
        public static Connection createConnection(String fileName){ 
        String driver=""; 
        String url=""; 
        String user=""; 
        String pwd=""; 
        BufferedReader in = null; 
        
        try{ 
          in =  new BufferedReader(new FileReader(fileName)); 
                driver = in.readLine(); 
          url = in.readLine(); 
          user = in.readLine(); 
          pwd = in.readLine(); 
          in.close(); 
        }catch(IOException ioe){ 
          ioe.printStackTrace(); 
          
        } 
        return createConnection(driver,url,user,pwd); 
        } 
        /** 
        *创建一个连接桥驱动 
        */ 
        public static Connection createConnection(String driver,String url){ 
          driver = "sun.jdbc.odbc.JdbcOdbcDriver"; 
          url = "jdbc:odbc:myodbc"; 
        String user = "sa"; 
        String pwd = ""; 
        return createConnection(driver,url,user,pwd); 
        } 
        
        /** 
        *关闭一个连接con,pstmt,rs 
        */ 
        public static void closeAll(Connection con,PreparedStatement pstmt,ResultSet rs){ 
            try{ 
                if(con != null) con.close(); 
                if(pstmt != null) pstmt.close(); 
                if(rs != null)rs.close(); 
            }catch(SQLException se){ 
                se.printStackTrace(); 
            } 
        } 
        /** 
        *关闭一个连接con,pstmt 
        */ 
        public static void closeAll(Connection con,PreparedStatement pstmt){ 
            try{ 
                if(con != null) con.close(); 
                if(pstmt != null) pstmt.close(); 
            }catch(SQLException se){ 
                se.printStackTrace(); 
            } 
        } 
        /** 
        *关闭一个连接con 
        */ 
        public static void closeAll(Connection con){ 
            try{ 
                if(con != null) con.close(); 
            }catch(SQLException se){ 
                se.printStackTrace(); 
            } 
        } 
        /** 
        *关闭一个连接con,stmt,rs 
        */ 
        public static void closeAll(Connection con,Statement stmt,ResultSet rs){ 
        try{ 
          if(con != null) con.close(); 
          if(stmt != null) stmt.close(); 
          if(rs != null) rs.close(); 
        }catch(SQLException se){ 
          se.printStackTrace(); 
        } 
        } 
        /** 
        *关闭一个连接con,stmt 
        */ 
        public static void closeAll(Connection con,Statement stmt){ 
        try{ 
          if(con != null) con.close(); 
          if(stmt != null) stmt.close(); 
        }catch(SQLException se){ 
          se.printStackTrace(); 
        } 
        } 
    }
      

  3.   

    首先在tomcat根目录\conf\contex.xml节点中添加<Resource>信息
    如下:
    <Resource name="jdbc/book" auth="Container" type="javax.sql.DataSource"
    maxActive="100" maxIdle="30" maxWait="10000" username="sa" password=""
    driverClassName="com.microsoft.jdbc.sqlserver.SQLServerDriver"
    url="jdbc:microsoft:sqlserver://localhost:1433; DatabaseName=book"/>
    然后在wel.xml中同样配置
    如下:
    <resource-ref>
    <description>Books DataSource</description>
    <res-ref-name>jdbc/book</res-ref-name>
    <res-ref-type>javax.sql.DataSource</res-ref-type>
    <res-auth>Container</res-auth>
    </resource-ref>配置好文件以后.使用lookup()查找.
    javax.naming.Contex提供了查找JNDI Resource的接口.
    如下:
    Context ctx=new InitialContext();
    DataSource ds=(DataSource )ctx.lookup("java:comp/env/jdbc/book");
    Connection con=ds.getConnection();