现在的位置: 首页 > 综合 > 正文

JDBC编程学习笔记整理(外传1)—数据源与JNDI

2019年10月12日 ⁄ 综合 ⁄ 共 3701字 ⁄ 字号 评论关闭

JNDI(Java Naming and Directory Interface)即是Java命名和目录接口,是一个为Java程序
提供命名和目录功能的API。为了使用JNDI文件系统,我们要从Sun的Java网站下载
fscontext1_2beta3.zip文件(http://192.18.97.54/ECom/EComTicketServlet/BEGIN836373535F0E1764803B2730E24621A2/-2147483648/672140307/1/392822/392738/672140307/2ts+/westCoastFSEND/7110-jndi-1.2.1-oth-JPR/7110-jndi-1.2.1-oth-JPR:4/fscontext-1_2-beta3.zip),下载后解压,我们会看到一个fscontext.jar和providerutil.jar,
我们把它添加到ClassPath环境变量那里去。
这篇外传说的是通过JDBC使用JNDI是非常有用的,因为这样可以注册(绑定)数据源,然后在程序
中查找这些数据源,而不必提供准确的数据库连接信息。因此,如果数据库连接信息改变了,那么
只需修改JNDI对象,而不必修改程序。当然JNDI不止用于数据源,它也可以用于存储和获取任何
类型的已命名的java对象以及执行标准的目录操作。好了,不多废话了。

一、将数据源绑定JNDI
    可以使用Java语句以程序方式将数据源绑定到JNDI,步骤如下:
    1、创建一个OracleDataSource对象
            OracleDataSource myODS=new OracleDataSource();
    2、设置OracleDataSource对象的属性
            
myODS.setServerName(“localhost“);
            myODS.setDatabaseName(“ORCL“);
            myODS.setPortNumber(1521);
            myODS.setDriverType(“thin“);
            myODS.setUser(“admin“);
            myODS.setPassword(“helloworld“);
    3、创建一个Properties对象
            java.util.Properties myProperties=new Properties();
    4、将JNDI属性添加到Properties对象,使用setProperties方法将JNDI属性添加到
       Properties对象。
            
myProperties.setProperties(Context.INITIAL_CONTEXT_FACTORY,
                “com.sun.jndi.fscontext.RefFSContextFactory“);
            myProperties.setProperties(Context.PROVIDER_URL,
                “file:C:/TEMP“);
       Context.INITIAL_CONTEXT_FACTORY-->该属性指定使用JNDI文件系统存储JNDI绑定信息文件。
       Context.PROVIDER_URL------------->该属性指定文件系统中存储绑定文件的目录。
    5、创建一个JNDI Context对象
            Context myContext=new InitialContext(myProperties);
       InitialContext构造器创建一个Context对象,这个对象引用最初的JNDI命名上下文。
    6、使用Context对象将OracleDataSource对象绑定到JNDI。这里使用Context对象的bind()方法。
       bind()方法接受两个参数:一个是给JNDI对象起的名字,第二个是要绑定的Java对象。
            myContext.bind(“myNamedODS“,myODS);
    以上六个步骤做完,我们就可以在Context.PROVIDER_URL属性指定的目录中找到.bindings文件。
    这个.binding文件包含数据源的细节。以后,可以使用myNamedODS这个名称查找这个JNDI对象,
    并且使用它连接数据库。

二、一个实例程序(待续)  
import java.util.*;
import java.sql.*;
import oracle.jdbc.pool.*;
import javax.naming.*;
/**
 * <p>Title: </p>
 * <p>Description: </p>
 * <p>Copyright: Copyright (c) 2004</p>
 * <p>Company: </p>
 * @author not attributable
 * @version 1.0
 */

public class JNDIExample1 {
  public JNDIExample1() {
  }

  public static void main(String[] args) throws SQLException,NamingException{
    //第一步:生成一个OracleDataSource 对象
    System.out.println("creating an OracleDataSource object");
    OracleDataSource myODS=new OracleDataSource();

    //第二步:对OracleDataSource对象的属性set值
    System.out.println("Setting the attribute of the OracleDataSource object");
    myODS.setServerName("localhost");
    myODS.setDatabaseName("testDb");
    myODS.setPortNumber(1521);
    myODS.setDriverType("thin");
    myODS.setUser("myUser");
    myODS.setPassword("myPassword");

    //第三步:生成一个properties对象
    System.out.println("creating a Properties object");
    Properties myProperties=new Properties();

    //第四步:把JNDI的属性加到Properties对象里面
    System.out.println("adding the JNDI properties");
    myProperties.setProperty(Context.INITIAL_CONTEXT_FACTORY,
                             "com.sun.jndi.fscontext.RefFSContextFactory");
    myProperties.setProperty(Context.PROVIDER_URL,"file:e:/JAVA/tmp");

    //第五步:生成一个JNDI Context对象
    System.out.println("creating a JNDI Context object");
    Context myContext=new InitialContext(myProperties);

    //第六步:把OracleDataSource数据源绑定到JNDI
    System.out.println("binding the OracleDataSource object to JDNI "+
                       "using the name 'myBoundODS'");
    myContext.bind("myBoundODS",myODS);
  }


}

程序运行显示:
creating an OracleDataSource object
Setting the attribute of the OracleDataSource object
creating a Properties object
adding the JNDI properties
creating a JNDI Context object
binding the OracleDataSource object to JDNI using the name 'myBoundODS'

三、使用JNDI查找数据源(TO BE CONTINUED)

抱歉!评论已关闭.