FileDocCategorySizeDatePackage
SQLQuery.javaAPI DocExample4680Mon Dec 02 11:32:58 GMT 2002ora.jwsnut.chapter7.jaxr

SQLQuery

public class SQLQuery extends Object

Fields Summary
private static javax.xml.registry.DeclarativeQueryManager
dqm
Constructors Summary
Methods Summary
private static voiddoQuery()

        // BUG ALERT: according to the spec, we should say "SELECT o.id",
        // but the implementation currently requires us to also get the objectType...
        Query query = dqm.createQuery(Query.QUERY_TYPE_SQL, 
                        "SELECT * FROM Organization o, Name_ nm " +
                        "WHERE nm.value LIKE '%ei%' AND " +
                        "nm.parent = o.id");
        BulkResponse res = dqm.executeQuery(query);

        // Process exceptions (if any)
        Collection exceptions = res.getExceptions();
        if (exceptions != null && !exceptions.isEmpty()){
            System.out.println("Error during query");
            Iterator iter = exceptions.iterator();
            while (iter.hasNext()) {
                ((Exception)iter.next()).printStackTrace(System.out);
            }
        }
        
        // Process results (if any)
        Collection coll = res.getCollection();
        System.out.println("Query found " + coll.size());
        Iterator iter = coll.iterator();
        while (iter.hasNext()) {
            Organization org = (Organization)iter.next();
            System.out.println("\t" + org.getName().getValue());

		// Get the classifications for this organization
		Query classQuery = dqm.createQuery(Query.QUERY_TYPE_SQL,
						"SELECT * FROM Classification c WHERE id IN " +
						"(RegistryObject_classifications('" +
						org.getKey().getId() + "'))");
		BulkResponse cRes = dqm.executeQuery(classQuery);

            // Process exceptions (if any)
            exceptions = cRes.getExceptions();
            if (exceptions != null && !exceptions.isEmpty()){
                System.out.println("Error during classification query");
                iter = exceptions.iterator();
                while (iter.hasNext()) {
                    ((Exception)iter.next()).printStackTrace(System.out);
                }
            }

            // Process results (if any)
            Collection classColl = cRes.getCollection();
            Iterator classIter = classColl .iterator();
            while (classIter.hasNext()) {
                Classification cls = (Classification)classIter.next();
                System.out.print("   " + cls.getName().getValue());
            }
            System.out.println();
        }
    
public static voidmain(java.lang.String[] args)

        
        // Validate arguments
        if (args.length != 1) {
            usage();
        }
        
        String queryURL = args[0];
        
        try {
            // Get the ConnectionFactory
            Properties props = new Properties();
            props.put("javax.xml.registry.queryManagerURL", queryURL);
            
            ConnectionFactory cf = ConnectionFactory.newInstance();
            cf.setProperties(props);
            
            // Get and initialize the connection
            Connection conn = cf.createConnection();
            conn.setSynchronous(true);
            
            // Get the RegistryService and the DeclarativeQueryManager
            RegistryService registry = conn.getRegistryService();
            dqm = registry.getDeclarativeQueryManager();          

            // Run the example code
            doQuery();
            
            // Close connection
            conn.close();           
            
        } catch (Throwable t) {
            System.out.println(t);
            t.printStackTrace(System.out);
            System.exit(1);
        }    
        
        System.exit(0);
    
public static voidusage()

        System.out.println("Usage:\tSQLQuery queryURL");
        System.exit(1);