to dbsc: 这部分的参数都有,我没贴出来,因为些配置参数和其他人配置的一样 我就是不明白javabean中如何可以找到JNDI(factory,url,driverClassName等参数值)呢? 是不是需要环境变量的支持?否则ClassLoader怎么找到指定的资源?
你猜得正确,初始化方法:1种 用参数,如: Hashtable env = new Hashtable(); env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory"); .... Context ctx = new InitialContext(env);2 用配置文件,缺省jndi.properties。(好像新规范要用xml,我不确定),如: Context.INITIAL_CONTEXT_FACTORY="com.sun.jndi.fscontext.RefFSContextFactory"
谢谢bdsc,之后看了一下 Sample Connection Pool Manager for Use with Microsoft SQL Server 2000 Driver for JDBChttp://support.microsoft.com/default.aspx?scid=KB;en-us;313173&更清楚了 :)
通过Create new host 作为自己的工作目录(如 d:\tomcat5\myweb),在该host下Create New Context 配置了Context Properties (Use Naming为true等)conf/server.xml:
...
<Resource name="sqlServer" type="javax.sql.DataSource"/>
<ResourceParams name="UserDatabase">
<parameter>
<name>factory</name>
<value>org.apache.catalina.users.MemoryUserDatabaseFactory</value>
</parameter>
<parameter>
<name>pathname</name>
<value>conf/tomcat-users.xml</value>
</parameter>
</ResourceParams>
<ResourceParams name="sqlServer">
<parameter>
<name>factory</name>
<value>org.apache.commons.dbcp.BasicDataSourceFactory</value>
</parameter>
<parameter>
<name>url</name>
<value>jdbc:microsoft:sqlserver://127.0.0.1:1433;DatabaseName=Northwind</value>
</parameter>
...
<Engine defaultHost="localhost" name="Catalina">
<Host appBase="D:\Tomcat5\myweb" name="web">
</Host>
...并且在conf\Catalina\web下产生了web.xml:
<?xml version='1.0' encoding='utf-8'?>
<Context crossContext="true" docBase="web" path="/web" reloadable="true" workDir="work\Catalina\web\web">
<Resource auth="Container" description="SQLServer DataBase" name="jdbc/sqlServer" type="javax.sql.DataSource"/>
</Context>在JAVABEAN中做测试,就产生,如前面提到的异常!
=================================================
package com.wzr.test;import java.sql.*;
import javax.naming.*;
import javax.sql.*;
import com.wzr.util.MyLogger;
import java.util.Properties;public class DBTest { private String foo ="null";
private int bar = -1;
private static DBTest dbtest=new DBTest();
private MyLogger logger=new MyLogger(this.getClass()); public DBTest(){ } public void init()throws Exception{
try{
Context ctx = new InitialContext();
if(ctx == null ){
throw new Exception("Boom - No Context");
} DataSource ds = (DataSource)ctx.lookup("java:comp/env/jdbc/sqlServer"); if (ds != null) {
Connection conn = ds.getConnection();
if(conn != null) { foo = "Got Connection "+conn.toString();
Statement stmt = conn.createStatement();
ResultSet rst =
stmt.executeQuery("select * from orders");
if(rst.next()) {
foo=rst.getString("CustomerID");
bar=rst.getInt("OrderID");
}
conn.close();
}
}
}
catch(Exception e) {
logger.log("==============="+e.getMessage());
e.printStackTrace();
}
} public String getFoo() { return foo; }
public int getBar() { return bar;} public static void main(String[] args){
DBTest test=new DBTest();
test.init();
System.out.println("Foo :"+test.getFoo());
}
}.class文件是输出到d:\tomcat5\myweb\web\WEB-INF\classes\com\wzr\test目录下
WEB-INF下的web.xml,如下:<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd"><web-app>
<resource-ref>
<description>SQLServer DataBase</description>
<res-ref-name>jdbc/sqlServer</res-ref-name>
<res-type>javax.sql.DataSource</res-type>
<res-auth>Container</res-auth>
</resource-ref>
</web-app>
<name>driverClassName</name>
<value>xxx.xxx.xxx</value>
</parameter>
这部分的参数都有,我没贴出来,因为些配置参数和其他人配置的一样
我就是不明白javabean中如何可以找到JNDI(factory,url,driverClassName等参数值)呢?
是不是需要环境变量的支持?否则ClassLoader怎么找到指定的资源?
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.fscontext.RefFSContextFactory");
....
Context ctx = new InitialContext(env);2 用配置文件,缺省jndi.properties。(好像新规范要用xml,我不确定),如:
Context.INITIAL_CONTEXT_FACTORY="com.sun.jndi.fscontext.RefFSContextFactory"
Sample Connection Pool Manager for Use with Microsoft SQL Server 2000 Driver for JDBChttp://support.microsoft.com/default.aspx?scid=KB;en-us;313173&更清楚了 :)