在WEB.XML中建立 <resource-ref> <description>Allow the Fast Lane Reader Pattern to Read Catalog Data Directly via JDBC in the Web Tier</description> <res-ref-name>jdbc/Wuliu</res-ref-name> <res-type>javax.sql.DataSource</res-type> <res-auth>Container</res-auth> </resource-ref> 然后在sun-j2ee-ri.xml中 <resource-ref> <res-ref-name>jdbc/Wuliu</res-ref-name> <jndi-name>Wuliu</jndi-name> <default-resource-principal> <name>用户名</name> <password>密码</password> </default-resource-principal> </resource-ref> 在程序中 Context env = (Context)(new InitialContext().lookup("java:comp/env/jdbc/Wuliu"));
javax.naming.NameNotFoundException: Unable to resolve 'app/webapp/DefaultWebApp/2913366/comp/env/WuLiu' Resolved: 'app/webapp/DefaultWebApp/2913366/comp/env' Unresolved:'WuLiu' ; remaining name 'WuLiu'
String url = "t3://localhost:7001";
String user = null;
String password = null;
Properties properties = null;
try {
properties = new Properties();
properties.put(Context.INITIAL_CONTEXT_FACTORY, "weblogic.jndi.WLInitialContextFactory");
properties.put(Context.PROVIDER_URL, url);
if (user != null) {
properties.put(Context.SECURITY_PRINCIPAL, user);
properties.put(Context.SECURITY_CREDENTIALS, password == null ? "" : password);
} return new InitialContext(properties);
}
catch(Exception e) {
System.out.println("Unable to connect to WebLogic server at " + url);
System.out.println("Please make sure that the server is running.");
throw e;
}
}
conn=DriverManager.getConnection("jdbc:weblogic:mssqlserver4:你的数据库名称@localhost","sa","");
Statement stmt = conn.createStatement();
rs = stmt.executeQuery(sql);
<resource-ref>
<description>Allow the Fast Lane Reader Pattern to Read Catalog Data Directly via JDBC in the Web Tier</description>
<res-ref-name>jdbc/Wuliu</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
然后在sun-j2ee-ri.xml中
<resource-ref>
<res-ref-name>jdbc/Wuliu</res-ref-name>
<jndi-name>Wuliu</jndi-name>
<default-resource-principal>
<name>用户名</name>
<password>密码</password>
</default-resource-principal>
</resource-ref>
在程序中
Context env = (Context)(new InitialContext().lookup("java:comp/env/jdbc/Wuliu"));
InitialContext ic=new InitialContext();
DataSource ds=(DataSource)ic.lookup("java:comp/env/jdbc/Wuliu");
try {
Context ctx = new InitialContext();
javax.sql.DataSource ds =
(javax.sql.DataSource)ctx.lookup("Wuliu");
return ds.getConnection();
}
catch (Exception e) {
System.err.println("Could not locate datasource!Reason:");
e.printStackTrace();
throw e;
}
}ejb-jar.xml中:
<resource-ref>
<description />
<res-ref-name>Wuliu</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>我是这样做的:)
<!DOCTYPE weblogic-ejb-jar PUBLIC '-//BEA Systems, Inc.//DTD WebLogic 7.0.0 EJB//EN' 'http://www.bea.com/servers/wls700/dtd/weblogic-ejb-jar.dtd'>
<weblogic-ejb-jar>
<weblogic-enterprise-bean>
<ejb-name>TestBean</ejb-name>
<reference-descriptor>
<resource-description>
<res-ref-name>Wuliu</res-ref-name>
<jndi-name>Wuliu</jndi-name>
</resource-description>
</reference-descriptor>
<jndi-name>TestBean</jndi-name>
</weblogic-enterprise-bean>
</weblogic-ejb-jar>