home *** CD-ROM | disk | FTP | other *** search
/ Enter 1999 September / ENTER9_2.bin / prog_8 / jbuilder3 / TRIAL / INTRCLNT / DATA.Z / JNDIExample.java < prev    next >
Encoding:
Java Source  |  1999-01-18  |  4.2 KB  |  94 lines

  1. // Copyright InterBase Software Corporation, 1998.
  2. // Written by com.inprise.interbase.interclient.r&d.PaulOstler :-)
  3. //
  4. // An example of using the Java Naming and Directory Interface (JNDI)
  5. // for naming JDBC datasources.
  6. // In a nutshell, a datasource represents a database and its connection
  7. // properties, and is stored by a JNDI name server provider such as LDAP.
  8. // Think of a datasource as a server-side registered database alias,
  9. // in which the name server can manage any number of InterBase servers.
  10. // Besides datasources, JNDI can name other objects as well.
  11.  
  12. public final class JNDIExample
  13. {
  14.   static public void main (String args[])
  15.   {
  16.     // Create an InterClient data source bean manually;
  17.     // beans are normally manipulated by a GUI tool.
  18.     // Bean properties are always set using the setXXX signature.
  19.     javax.sql.DataSource dataSource = new interbase.interclient.DataSource ();
  20.  
  21.     // Set the standard properties
  22.     ((interbase.interclient.DataSource) dataSource).setServerName ("perdy");
  23.     ((interbase.interclient.DataSource) dataSource).setDatabaseName ("d:/databases/employee.gdb");
  24.     ((interbase.interclient.DataSource) dataSource).setDataSourceName ("Employee");
  25.     ((interbase.interclient.DataSource) dataSource).setDescription ("An example database of employees");
  26.     ((interbase.interclient.DataSource) dataSource).setPortNumber (3060);
  27.     ((interbase.interclient.DataSource) dataSource).setNetworkProtocol ("jdbc:interbase:");
  28.     ((interbase.interclient.DataSource) dataSource).setRoleName (null);
  29.     
  30.     // Set the non-standard properties
  31.     ((interbase.interclient.DataSource) dataSource).setServerManagerHost ("perdy");
  32.     ((interbase.interclient.DataSource) dataSource).setCharSet (interbase.interclient.CharacterEncodings.NONE);
  33.     ((interbase.interclient.DataSource) dataSource).setSuggestedCachePages (0);
  34.     ((interbase.interclient.DataSource) dataSource).setSweepOnConnect (false);
  35.  
  36.     // Test our ability to manufacture a JNDI naming reference from the data source
  37.     javax.naming.Reference ref = null;
  38.     try {
  39.       ref = ((javax.naming.Referenceable) dataSource).getReference ();
  40.     }
  41.     catch (javax.naming.NamingException e) {
  42.       System.out.println ("naming exception: " + e.getMessage ());
  43.     }
  44.  
  45.     // Now test our Object Factory's ability to regenerate the data source from the JNDI reference
  46.     interbase.interclient.ObjectFactory factory = new interbase.interclient.ObjectFactory ();
  47.     try {
  48.       dataSource = (javax.sql.DataSource) factory.getObjectInstance (ref, null, null, null);
  49.     }
  50.     catch (java.lang.Exception e) {
  51.       System.out.println ("factory exception: " + e.getMessage ());
  52.     }
  53.  
  54.     // Set up an LDAP environment for LDAP server perdy, this is where references will be stored
  55.     java.util.Hashtable ldapEnv = new java.util.Hashtable();
  56.     ldapEnv.put (javax.naming.Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.ldap.LdapCtxFactory");
  57.     ldapEnv.put (javax.naming.Context.PROVIDER_URL, "ldap://perdy:389");
  58.  
  59.     // Register an InterClient data source with an LDAP server
  60.     try {
  61.       javax.naming.Context context = new javax.naming.InitialContext (ldapEnv);
  62.       System.out.println ("got context");
  63.       context.bind ("jdbc/EmployeeDB", dataSource);
  64.       System.out.println ("bound data source");
  65.     }
  66.     catch (javax.naming.NamingException e) {
  67.       System.out.println ("naming exception: " + e.getMessage ());
  68.     }
  69.  
  70.     // Lookup a registered InterClient data source from an LDAP server
  71.     try {
  72.       javax.naming.Context context = new javax.naming.InitialContext (ldapEnv);
  73.       System.out.println ("got context");
  74.       dataSource = (javax.sql.DataSource) context.lookup ("jdbc/EmployeeDB");
  75.       System.out.println ("found data source");
  76.     }
  77.     catch (javax.naming.NamingException e) {
  78.       System.out.println ("naming exception: " + e.getMessage ());
  79.     }
  80.  
  81.     // Connect to the InterClient DataSource
  82.     try {
  83.       dataSource.setLoginTimeout (10);
  84.       java.sql.Connection c = dataSource.getConnection ("sysdba", "masterkey");
  85.       System.out.println ("got connection");
  86.       c.close ();
  87.     }
  88.     catch (java.sql.SQLException e) {
  89.       System.out.println ("sql exception: " + e.getMessage ());
  90.     }
  91.   }
  92. }
  93.               
  94.