Last active
August 29, 2015 13:57
-
-
Save aarchilla84/9758929 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.naming.Context; | |
import javax.naming.InitialContext; | |
import javax.sql.DataSource; | |
import java.sql.Connection; | |
... | |
String datasource = "FNOSDS" | |
Context ctx = new InitialContext(); | |
DataSource ds = (DataSource)ctx.lookup(datasource); | |
Connection dbConnection = ds.getConnection(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.util.Hashtable; | |
import javax.naming.Context; | |
import javax.naming.InitialContext; | |
import javax.naming.NamingException; | |
import javax.naming.spi.InitialContextFactory; | |
import javax.naming.spi.InitialContextFactoryBuilder; | |
import javax.sql.DataSource; | |
public class DatabaseContextFactory implements InitialContextFactory, InitialContextFactoryBuilder | |
{ | |
static DatabaseContext context; | |
public Context getInitialContext(Hashtable<?, ?> environment) throws NamingException | |
{ | |
if(context==null){ | |
context = new DatabaseContext(); | |
} | |
return context; | |
} | |
public InitialContextFactory createInitialContextFactory(Hashtable<?, ?> environment) throws NamingException | |
{ | |
return new DatabaseContextFactory(); | |
} | |
final class DatabaseContext extends InitialContext | |
{ | |
DataSource datasource; | |
DatabaseContext() throws NamingException{ } | |
@Override | |
public void bind(String name, Object obj) throws NamingException | |
{ | |
if("FNOSDS".equals(name)){ | |
datasource = (DataSource)obj; | |
} | |
else{ | |
super.bind(name, obj); | |
} | |
} | |
@Override | |
public Object lookup(String name) throws NamingException | |
{ | |
if("FNOSDS".equals(name)){ | |
return datasource; | |
} | |
return super.lookup(name); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javax.naming.Context; | |
import javax.naming.InitialContext; | |
import javax.naming.spi.NamingManager; | |
import com.ibm.db2.jcc.DB2SimpleDataSource; // db2jcc.jar | |
... | |
DB2SimpleDataSource db2Source=new DB2SimpleDataSource(); | |
db2Source.setServerName("url.host.com"); | |
db2Source.setPortNumber(50000); | |
db2Source.setDatabaseName("FNOS"); | |
db2Source.setDriverType(4); | |
db2Source.setDescription("Filenet DB2"); | |
db2Source.setUser("db2admin"); | |
db2Source.setPassword("db2admin"); | |
NamingManager.setInitialContextFactoryBuilder(new DatabaseContextFactory()); | |
Context ctx = new InitialContext(); | |
ctx.bind("FNOSDS", db2Source); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment