Posted on 2009-07-17 15:53 
java小爬虫 阅读(2437) 
评论(2)  编辑  收藏  
			 
			
		 
		 
一:  把 <Resource name="jdbc/TestDB" auth="Container" type="javax.sql.DataSource"
               maxActive="100" maxIdle="30" maxWait="10000"
               username="root" password="sa" driverClassName="com.mysql.jdbc.Driver"
               url="jdbc:mysql://localhost:3306/employee_manager?autoReconnect=true"/>
         粘贴到Tomcat 6.0/conf   context.xml 文件中的<Context></Context>
   二:把 <resource-ref>
      <description>DB Connection</description>
      <res-ref-name>jdbc/TestDB</res-ref-name>
      <res-type>javax.sql.DataSource</res-type>
      <res-auth>Container</res-auth>
     </resource-ref>
       粘贴到web.xml 文件中的<web-app></ web-app > 
   三: 把数据库驱动文件放在Tomcat 6.0/lib 中
   四: 程序中应用:
    public static Connection getConnection() {
       DataSource ds;
       InitialContext cxt ;
       try{
        cxt = new InitialContext();
        ds = (DataSource) cxt.lookup( "java:/comp/env/jdbc/TestDB" );
        con = ds.getConnection();
       }catch(Exception e){
           e.printStackTrace();
       }
SqlServer2000和以上步骤一样:但注意
      url="jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=employee_manager"
      ?autoReconnect=true 应该删除!
上述内容在MyEclipse7.1.1 + MySql5.1/Sqlserver2000+Tomcat6.0.18上测试成功!