SQLServer2005+MySql+Oracle三种数据库连接池的配置(完结篇)

//**本篇介绍了三种数据库SQLServer2005MySqlOracle三种数据库连接池的配置

 *在配置前请确保tomcatlib中已经导入所有的那种数据库驱动包

 */

   

    WEB-INF/web.xml

   

   

 

   

   

                 

             

                                       auth="Container"

                                       type="javax.sql.DataSource"

                                       maxActive="100"

                                       maxIdle="30"

                                       maxWait="10000"

                                       username="root"

                                       password="jakeyouxiang"

                                       driverClassName="com.mysql.jdbc.Driver"

                                       url="jdbc:mysql://127.0.0.1/training"/>

                                      

               

               

                               auth="Container"

                               type="javax.sql.DataSource"

                               maxActive="100"

                               maxIdle="30"

                               maxWait="10000"

                               username="sa"

                               password="jakeyouxiang"

                                       driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver"

                               url="jdbc:sqlserver://127.0.0.1:1433;databasename=blog"/>

 

 

                                      

                 

               

                                       auth="Container"

                                       type="javax.sql.DataSource"

                                       maxActive="100"

                                       maxIdle="30"

                                       maxWait="10000"

                                       username="root"

                                       password="jakeyouxiang"

                                       driverClassName="oracle.jdbc.driver.OracleDriver"

                                       url="jdbc:oracle:thin:@127.0.0.1:1521:ora9"/>

                 

 

 

//数据库得到Connection的程序

package database;

 

import java.sql.*;

import javax.naming.*;

import javax.sql.*;

import java.sql.*;

 

public class DataSource{

      

       public static Connection getConnection()throws Exception{

              //以下是应用线程池的方式

              Connection cn=null;

              DataSource ds=null;

              try{

                     Context con=new InitialContext();

                     //通过con得到数据源,最后的jdbc/mysqlds根据数据库的不同而有所不同

                     ds=(DataSource)con.lookup("java:comp/env/jdbc/mysqlds");

                     cn=ds.getConnection();

              }catch(Exception e){

                     e.printStackTrace();

              }    

              return cn;

       }

}

关键词:SQLServer2005线程池,MySql线程池,Oracle线程池,各种数据库连接池,Content.xml

你可能感兴趣的:(Servlet)