1、context的配置 <Context path="/ctx" docBase="d:/ctx" debug="0" reloadable="true">
<Resource name="good" type="javax.sql.DataSource"
password="472736"
username="lijuan"
driverClassName="com.jdbc.mysql.Driver"
maxIdle="2" maxWait="5000" username="sa"
url="jdbc:mysql://localhost/good?" maxActive="4"/>
</Context>;
2、 resource-ref子元素的描述如下: ● res-ref-name是资源工厂引用名的名称。该名称是一个与java:comp/env上下文相对应的JNDI名称,并且在整个Web应用中必须是惟一的。 ● res-auth表明:servlet代码通过编程注册到资源管理器,或者是容器将代表servlet注册到资源管理器。该元素的值必须为Application或Container。 ● res-sharing-scope表明:是否可以共享通过给定资源管理器连接工厂引用获得的连接。该元素的值必须为Shareable(默认值)或Unshareable。 3、编写jsp代码 Context initCtx = new InitialContext();
Context ctx = (Context) initCtx.lookup("java:comp/env");
//获取连接池对象
DataSource ds =(DataSource)ctx.lookup("jdbc/rot"); Connection conn = ds.getConnection();
Statement stmt = conn.createStatement();
String strSql ="SELECT * FROM product";
ResultSet rs = stmt.executeQuery(strSql);
|