ibm-information-center/dist/eclipse/plugins/i5OS.ic.rzatz_5.4.0.1/51/program/datacflk.htm

167 lines
4.8 KiB
HTML
Raw Normal View History

2024-04-02 14:02:31 +00:00
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<META http-equiv="Content-Type" content="text/html; charset=utf-8">
<LINK rel="stylesheet" type="text/css" href="../../../rzahg/ic.css">
<title>Example: Connection factory lookup</title>
</head>
<BODY>
<!-- Java sync-link -->
<SCRIPT LANGUAGE="Javascript" SRC="../../../rzahg/synch.js" TYPE="text/javascript"></SCRIPT>
<h6><a name="datacflk"></a>Example: Connection factory lookup</h6>
<pre> import javax.resource.cci.*;
import javax.resource.ResourceException;
import javax.naming.*;
import java.util.*;
/**
* This class is used to look up a connection factory.
*/
public class ConnectionFactoryLookup {
String jndiName = &quot;java:comp/env/eis/SampleConnection&quot;;
boolean verbose = false;
/**
* main method
*/
public static void main(String[] args) {
ConnectionFactoryLookup cfl = new ConnectionFactoryLookup();
cfl.checkParam(args);
try {
cfl.lookupConnectionFactory();
}
catch(javax.naming.NamingException ne) {
System.out.println(&quot;Caught this &quot; + ne);
ne.printStackTrace(System.out);
}
catch(javax.resource.ResourceException re) {
System.out.println(&quot;Caught this &quot; + re);
re.printStackTrace(System.out);
}
}
/**
* This method does a simple Connection Factory lookup.
*
* After the Connection Factory is looked up, a connection is got from
* the Connection Factory. Then the Connection MetaData is retrieved
* to verify the connection is workable.
*/
public void lookupConnectionFactory()
throws javax.naming.NamingException, javax.resource.ResourceException {
javax.resource.cci.ConnectionFactory factory = null;
javax.resource.cci.Connection conn = null;
javax.resource.cci.ConnectionMetaData metaData = null;
try {
// lookup the connection factory
if (verbose) {
System.out.println(&quot;Look up the connection factory...&quot;);
}
InitialContext ic = new InitialContext();
factory = (ConnectionFactory) ic.lookup(jndiName);
// Get connection
if (verbose) System.out.println(&quot;Get the connection...&quot;);
conn = factory.getConnection();
// Get ConnectionMetaData
metaData = conn.getMetaData();
// Print out the metadata Information.
if (verbose) System.out.println(&quot; ** EISProductName :&quot;
+ metaData.getEISProductName());
if (verbose) System.out.println(&quot; EISProductVersion:&quot;
+ metaData.getEISProductVersion());
if (verbose) System.out.println(&quot; UserName :&quot;
+ metaData.getUserName());
System.out.println(&quot;Connection factory &quot; + jndiName +
&quot; is successfully looked up&quot;);
}
catch (javax.naming.NamingException ne) {
// Connection factory cannot be looked up.
throw ne;
}
catch (javax.resource.ResourceException re) {
// Something wrong with connections.
throw re;
}
finally {
if (conn != null) {
try {
conn.close();
}
catch (javax.resource.ResourceException re) {
}
}
}
}
/**
* Check and gather all the parameters.
*/
private void checkParam(String args[]) {
int i = 0, j;
String arg;
char flag;
boolean help = false;
// parse out the options
while (i &lt; args.length &amp;&amp; args[i].startsWith(&quot;-&quot;)) {
arg = args[i++];
// get the database name
if (arg.equalsIgnoreCase(&quot;-jndiName&quot;)) {
if (i &lt; args.length) {
jndiName = args[i++];
}
else {
System.err.println(&quot;-jndiName requires a &quot;
+ &quot;J2C Connection Factory JNDI name&quot;);
break;
}
}
else { // check for verbose, cmp , bmp
for (j = 1; j &lt; arg.length(); j++) {
flag = arg.charAt(j);
switch (flag) {
case 'v' :
case 'V' :
verbose = true;
break;
case 'h' :
case 'H' :
help = true;
break;
default :
System.err.println(&quot;illegal option &quot; + flag);
break;
}
}
}
}
if ((i != args.length) || help) {
System.err.println(&quot;Usage: java ConnectionFactoryLookup [-v] [-h]&quot;);
System.err.println(&quot; [-jndiName the J2C Connection Factory JNDI name]&quot;);
System.err.println(&quot;-v=verbose&quot;);
System.err.println(&quot;-h=this information&quot;);
System.exit(1);
}
}
}</pre>
</body>
</html>