Tuesday, November 27, 2007

Adding, Replacing, Removing, and Renaming a Binding in the Naming Service

try {
// Add a binding.
ctx.bind("Name", new SampleObjectImpl());

// Replace a binding.
ctx.rebind("Name", new SampleObjectImpl());

// Remove a binding.
ctx.unbind("Name");

// Rename a binding.
ctx.rename("Name", "NewSample");
} catch (NamingException e) {
}

Monday, November 26, 2007

Using a URL as a Name to the Initial Context

This example looks up an object from the naming service by using a URL as a name.
try {
Object obj = new InitialContext().lookup("iiop://localhost/Sample");
} catch (NamingException e) {
}

Wednesday, November 21, 2007

Looking Up an Object from the Naming Service

This example looks up an object from the naming service.
try {
Object obj = ctx.lookup("Sample");
} catch (NamingException e) {
}

Creating an Initial Context to the Naming Service

This example uses the JNDI/COS naming service provider to connect to the local tnameserv.
String url = "iiop://localhost/";
Hashtable env = new Hashtable();
env.put(Context.INITIAL_CONTEXT_FACTORY, "com.sun.jndi.cosnaming.CNCtxFactory");
env.put(Context.PROVIDER_URL, url);
try {
Context ctx = new InitialContext(env);
} catch (NamingException e) {
}

Thursday, November 15, 2007

Listing a Context in the Naming Service

This example lists a context in the naming service.
try {
NamingEnumeration enum = ctx.list("child");
while (enum.hasMore()) {
NameClassPair entry = (NameClassPair)enum.next();
process(entry);
}
} catch (NamingException e) {
}

Sunday, October 28, 2007

Calling a Stored Procedure in a Database

This example demonstrates how to call stored procedures with IN, OUT, and IN/OUT parameters.
CallableStatement cs;
try {
// Call a procedure with no parameters
cs = connection.prepareCall("{call myproc}");
cs.execute();

// Call a procedure with one IN parameter
cs = connection.prepareCall("{call myprocin(?)}");

// Set the value for the IN parameter
cs.setString(1, "a string");

// Execute the stored procedure
cs.execute();

// Call a procedure with one OUT parameter
cs = connection.prepareCall("{call myprocout(?)}");

// Register the type of the OUT parameter
cs.registerOutParameter(1, Types.VARCHAR);

// Execute the stored procedure and retrieve the OUT value
cs.execute();
String outParam = cs.getString(1); // OUT parameter

// Call a procedure with one IN/OUT parameter
cs = connection.prepareCall("{call myprocinout(?)}");

// Register the type of the IN/OUT parameter
cs.registerOutParameter(1, Types.VARCHAR);

// Set the value for the IN/OUT parameter
cs.setString(1, "a string");

// Execute the stored procedure and retrieve the IN/OUT value
cs.execute();
outParam = cs.getString(1); // OUT parameter
} catch (SQLException e) {
}

Saturday, October 20, 2007

Connecting to a SQLServer Database

This example connects to a SQLServer database using the NetDirect JDBC driver. For information about this driver, see e232 Getting JDBC Drivers for a Database.
Connection connection = null;
try {
String driverName = "com.jnetdirect.jsql.JSQLDriver"; // NetDirect JDBC driver
String serverName = "127.0.0.1";
String portNumber = "1433";
String mydatabase = serverName + ":" + portNumber;
String url = "jdbc:JSQLConnect://" + mydatabase; // a JDBC url
String username = "username";
String password = "password";

// Load the JDBC driver
Class.forName(driverName);

// Create a connection to the database
connection = DriverManager.getConnection(url, username, password);
} catch (ClassNotFoundException e) {
// Could not find the database driver
} catch (SQLException e) {
// Could not connect to the database
}