HomeDigital EditionSys-Con RadioSearch Java Cd
Advanced Java AWT Book Reviews/Excerpts Client Server Corba Editorials Embedded Java Enterprise Java IDE's Industry Watch Integration Interviews Java Applet Java & Databases Java & Web Services Java Fundamentals Java Native Interface Java Servlets Java Beans J2ME Libraries .NET Object Orientation Observations/IMHO Product Reviews Scalability & Performance Security Server Side Source Code Straight Talking Swing Threads Using Java with others Wireless XML
 

"A Three-Way Query"
Vol. 6, Issue 11, p. 74

	


Listing 1 



import java.util.*;
import java.io.*;
import java.net.URL;
import java.net.MalformedURLException;

public class Util
{
 static public URL createURL(String name)
  {
  // Turn a URL-string or filename 
  into a URL object
  URL url = null;
  try
  {
  url 
  = new URL(name);
  return 
  url;
  }
  catch (MalformedURLException 
  ex) {;}
 // If it 
  wasn't a URL, it must be a file...
  try
  {
  File 
  file = new File(name);
  url 
  = new URL("file:" + file.getAbsolutePath());
  return 
  url;
  }
  catch (MalformedURLException 
  ex)
  {
  System.out.println 
  ("Malformed URL " );
  return 
  null;
  }
  }
 public static Properties loadProperties(String fileName)
  {
  try
  {
  FileInputStream 
  fis = new FileInputStream(fileName);
  Properties 
  properties = new Properties();
  properties.load(fis);
  return 
  properties;
  }
  catch(FileNotFoundException e)
  {
  System.out.println 
  ("File not found " + fileName);
  return 
  null;
  }
  catch(IOException e)
  {
  System.out.println 
  ( e.getMessage());
  return 
  null;
  }
  }
  }
  
 Listing 2

import java.util.*;
import java.sql.*;
 public class DBImpl implements EmployeeQuery
  {
  private Connection connection;
  private String URL="jdbc:sybase:Tds:whitehorn-dev:6600";
  private String PROPERTIES="db.properties";
  private String DRIVER="com.sybase.jdbc.SybDriver";
 public void init()
  {
  try
  {
  Properties 
  p = Util.loadProperties(PROPERTIES);
  Class.forName(DRIVER);
  connection 
  = DriverManager.getConnection(URL, p);
  }
  catch (ClassNotFoundException 
  e)
  {
  System.out.println 
  ("Cannot find Sybase driver ");
  }
  catch 
  (SQLException e)
  {
  System.out.println 
  ("Error logging into database ");
  }
  }
 public java.util.List search(Conditions c)
  {
  String sql = "select id, lastName, 
  firstName, " +
  "phone, 
  e-mail from Employee" ;
  try
  {
  Statement 
  stmt = connection.createStatement();
  ResultSet 
  s = stmt.executeQuery(sql);
  List 
  employeeList = new ArrayList();
  while 
  (s.next())
  {
  Employee 
  emp = new Employee (s.getString("id"));
  emp.setLastName 
  (s.getString("lastName"));
  emp.setFirstName(s.getString("firstName"));
  employeeList.add(emp);
  }
  return 
  employeeList;
  }
  catch (SQLException e)
  {
  e.printStackTrace();
  return null;
  }
  }
  }
   
 Listing 3

import java.util.*;
import javax.naming.*;
import javax.naming.directory.*;
 public class LDAPImpl implements EmployeeQuery
  {
  private String PROPERTIES="ldap.properties";
  private DirContext ctx = null;
 public void init()
  {
  try
  {
  Properties 
  p = Util.loadProperties(PROPERTIES);
  ctx = new 
  InitialDirContext(p);
  }
  catch(NamingException e)
  {
  System.out.println 
  ("Failed getting context " );
  }
  }
 public java.util.List search(Conditions c)
  {
  List employeeList = new 
  ArrayList();
 String searchRoot = 
  "ou=People";
  String filter = "(uid=*)";
  String[] attrIDs = {"uid", 
  "givenname", "sn",
  "telephonenumber", "mail"};
  SearchControls ctls = 
  new SearchControls();
  ctls.setReturningAttributes(attrIDs);
 try
  {
  NamingEnumeration 
  list =
  ctx.search(searchRoot, 
  filter, ctls);
 while (list.hasMore())
  {
  SearchResult 
  nc = (SearchResult)list.next();
  Attributes 
  attrs = nc.getAttributes();
  Attribute 
  attrId = (Attribute) attrs.get("uid");
  String id 
  = (String) attrId.get();
  Employee emp 
  = new Employee(id);
 Attribute 
  attFN=(Attribute)attrs.get("givenname");
  String firstName=(String) 
  attFN.get();
  emp.setFirstName(firstName);
 Attribute 
  attLN=(Attribute)attrs.get("sn");
  String lastName=(String) 
  attLN.get();
  emp.setLastName(lastName);
 employeeList.add(emp);
  }
  return employeeList;
  }
  catch (NamingException e)
  {
  System.out.println ( e.getMessage());
  return null;
  }
  }
  }
  
 Listing 4
 <?xml version='1.0'?>
  <Employees>
  <Employee id='100012' >
  <lastName>Doe</lastName>
  <firstName>John</firstName>
  <phone>212-333-1111</phone>
  </Employee>
  <Employee id='100112' >
  <lastName>Sanders</lastName>
  <firstName>Jackson</firstName>
  <phone>415-633-1111</phone>
  </Employee>
  <Employee id='77112' >
  <lastName>Yeung</lastName>
  <firstName>Mike</firstName>
  <phone>201-333-1111</phone>
  </Employee>
  <Employee id='44421' >
  <lastName>Zee</lastName>
  <firstName>Mary</firstName>
  <phone>201-333-1981</phone>
  <e-mail>zee@system.com</e-mail>
  </Employee>
  <Employee id='77198' >
  <lastName>Burn</lastName>
  <firstName>Abby</firstName>
  <phone>501-443-1981</phone>
  <e-mail>abby@system.com</e-mail>
  </Employee>
  <Employee id='087198' >
  <lastName>Kingston</lastName>
  <firstName>Steve</firstName>
  <phone>917-743-1989</phone>
  <e-mail>sking@hotmail.com</e-mail>
  </Employee>
  <Employee id='8976198' >
  <lastName>King</lastName>
  <firstName>David</firstName>
  <phone>917-443-1319</phone>
  <e-mail>king@aabbcc.com</e-mail>
  </Employee>
  <Employee id='430898' >
  <lastName>Chen</lastName>
  <firstName>Irene</firstName>
  <phone>912-243-1985</phone>
  <e-mail>ichen@hotmail.com</e-mail>
  </Employee>
  <Employee id='222222' >
  <lastName>Osaka</lastName>
  <firstName>Yosi</firstName>
  <phone>917-333-1119</phone>
  <e-mail>osaka@hotmail.com</e-mail>
  </Employee>
  </Employees>
  
 Listing 5 

import java.util.*;
import java.net.URL;
import javax.xml.parsers.*;
import org.w3c.dom.*;
import org.apache.xpath.*;
 public class XMLImpl implements EmployeeQuery
  {
  private String XML_FILE="employees.xml";
  private DocumentBuilder domParser;
 public void init()
  {
  try
  {
  DocumentBuilderFactory 
  factory =
  DocumentBuilderFactory.newInstance();
  domParser 
  = factory.newDocumentBuilder(); 
  }
  catch (Exception 
  e)
  {
  System.out.println 
  ("Failed creating dom parser ");
  e.printStackTrace();
  }
  }
  /* Return a node from the nodelist which 
  has name */
  private Node lookup (NodeList nl, String name)
  {
  for (int i = 0; 
  i< nl.getLength(); i++)
  {
  Node 
  n = nl.item(i);
  if 
  (n.getNodeName().equals(name))
  return 
  n;
  }
  return null;
  }
 public java.util.List search(Conditions c)
  {
  try
  {
  URL cfgURL = Util.createURL(XML_FILE);
  Document d = domParser.parse(cfgURL.toString());
  String xpath = "//Employee";
  List employeeList 
  = new ArrayList();
  NodeList nl = XPathAPI.selectNodeList(d, 
  xpath );
  for (int i = 0; 
  i< nl.getLength(); i++)
  {
  Node 
  n = nl.item(i);
 // 
  id is an attribute
  Node 
  nm = n.getAttributes().getNamedItem("id");
  Employee 
  emp = new Employee(nm.getNodeValue());
 // 
  Other fields are elements
  NodeList 
  childNodes = n.getChildNodes();
  Node 
  lname = lookup (childNodes, "lastName");
  String ln = lname.getFirstChild().getNodeValue();
  emp.setLastName(ln);
  Node fname = lookup (childNodes, "firstName");
  String fn = fname.getFirstChild().getNodeValue();
  emp.setFirstName(fn);
  employeeList.add (emp);
  }
  return employeeList;
  }
  catch (Exception e)
  {
  e.printStackTrace();
  return null;
  }
 }
  }

  
 
 

All Rights Reserved
Copyright ©  2004 SYS-CON Media, Inc.
  E-mail: info@sys-con.com

Java and Java-based marks are trademarks or registered trademarks of Sun Microsystems, Inc. in the United States and other countries. SYS-CON Publications, Inc. is independent of Sun Microsystems, Inc.