Consultor Eletrônico



Kbase 21458: JDBC -- Sample Java Code To Bind Data Source
Autor   Progress Software Corporation - Progress
Acesso   Público
Publicação   16/10/2008
Status: Unverified

GOAL:

How to use Sun's LdapCtxFactory class to bind Merant's JdbcProgressDataSource.

FIX:

The following Sample code demonstrates how to use Sun's LdapCtxFactory class to bind Merant's JdbcProgressDataSource so the client program can then look it up and request a connection:

import javax.naming.*;
import javax.naming.directory.*;
import java.sql.*;
import java.util.Hashtable;
import com.progress.sql.jdbcx.datasource.*;

class MBind {
public static void main(String[] args) {
// Set up the environment for creating the initial context
Hashtable env = new Hashtable(11);
env.put(Context.INITIAL_CONTEXT_FACTORY,
com.sun.jndi.ldap.LdapCtxFactory");
env.put(Context.PROVIDER_URL, "ldap://localhost:389");

try {
// Create the initial context
DirContext ctx = new InitialDirContext(env);

// Create the DataSource object to be bound
JdbcProgressDataSource ds = new JdbcProgressDataSource();
// Set attributes within the new DataSource
ds.setDatabaseName("test");
ds.setUser("administrator");
ds.setPassword("");
ds.setDescription("Test database for JNDI");
ds.setServerName("localhost");
ds.setPortNumber(5000);

// Perform the bind
ctx.bind("cn=mydb, ou=Databases, o=progress.com", ds);

// Check that it is bound
Object obj=ctx.lookup("cn=mydb,ou=Databases,o=progress.com");
System.out.println(obj);

// Close the context when we're done
ctx.close();
} catch (NamingException e) {
System.out.println("Operation failed: " + e);
}
}
}