日期:2014-05-16 浏览次数:20550 次
<?xml version="1.0" encoding="UTF-8"?> <!-- 连接池配置文件context.xml --> <Context crossContext="true" debug="5" reloadable="true"> <Resource name="jdbc/yourProject"<!-- yourProject为你的项目名,下面的也一样--> type="javax.sql.DataSource" driverClassName="com.microsoft.sqlserver.jdbc.SQLServerDriver" url="jdbc:sqlserver://192.168.0.32;DatabaseName=factory;" username="yf" password="yf456" auth="Container" maxActive="100" maxIdle="30" maxWait="10000" /> </Context>
<resource-ref> <res-ref-name>jdbc/factoryManager</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref>
package com.dfjy.fm.dao; import java.sql.Connection; import java.sql.SQLException; import javax.naming.Context; import javax.naming.InitialContext; import javax.naming.NamingException; import javax.sql.DataSource; import com.dfjy.fm.exception.MyException; /* * 连接池类 */ public class DBPool { private static DataSource m_pool = null; private static Connection m_conn = null; static { Context env = null; try { env = (Context) new InitialContext().lookup("java:comp/env"); m_pool = (DataSource) env.lookup("jdbc/factoryManager"); System.out.println(m_pool); if (m_pool == null) { System.err.println("'DBPool' is an unknown DataSource"); } else { m_conn = m_pool.getConnection(); } } catch (NamingException ne) { ne.printStackTrace(); } catch (SQLException e) { System.out.println("得到连接失败!"); throw new MyException(); } } public static Connection getConnection() { return m_conn; } public static DataSource getPool() { return m_pool; } }