jdbc通过JNDI获取tomcat6连接池的连接
1.tomcat的server.xml中配置连接池
<Context docBase="HelloWeb" path="/HelloWeb" reloadable="true" source="org.eclipse.jst.jee.server:HelloWeb"> <Resource name="jdbc/msg" auth="Container" type="javax.sql.DataSource" username="root" password="root" driverClassName="com.mysql.jdbc.Driver" url="jdbc:mysql://localhost:3306/test" maxActive="8" maxIdle="4"/> </Context>
Connection conn = null;try {Context initCtx = new InitialContext();Context envCtx = (Context) initCtx.lookup("java:comp/env");DataSource ds = (DataSource) envCtx.lookup("jdbc/test"); conn = ds.getConnection();//conn.close();} catch (NamingException e) {e.printStackTrace();} catch (SQLException e) {// TODO Auto-generated catch block}