1、在context.xml中的context标签内加入以下代码:
<Resource name="jdbc/myoracle" auth="Container"
type="javax.sql.DataSource" driverClassName="oracle.jdbc.OracleDriver"
url="jdbc:oracle:thin:@127.0.0.1:1521:orclsid"
username="jike" password="jike" maxActive="20" maxIdle="10"
maxWait="-1"/>
2、在web.xml中web-app下加入以下代码
<resource-ref>
<description>Oracle Datasource example</description>
<res-ref-name>jdbc/myoracle</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
3、在javabean中引用
在网页后台处理程序(JavaBean)的代码中使用JNDI
包引入:
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.naming.NamingException;
import javax.sql.DataSource;
try {
Context envContext = (Context)new InitialContext().lookup("java:/comp/env");
DataSource ds = (DataSource)envContext.lookup("jdbc/myoracle"); //查找配置
Connection conn = ds.getConnection();
Statement st = conn.createStatement();
ResultSet rs = st.executeQuery("select id from pic");
out.println("dsdaffsdffd中");
while(rs.next()){
out.println(rs.getString("id") + "<br>");
}
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
}