The sample provided in this section demonstrates the usage of the Attunity JCA Query adapter (supplied as part of AIS). in a non-managed (2-tier) mode.

The user must provide the following parameters before the query adapter starts the process:

  • The IP address of the daemon or server.
  • The port number where the daemon/server is running (press Enter for the default port).
  • The user name to connect to the daemon/server (press Enter when the daemon/server doesn’t request user authentication).
  • The password used to connect to the daemon/server (press Enter when the daemon/server doesn’t request user authentication).
  • An SQL query (e.g: SELECT * from disam:nv_dept).

Note
When working with the query interaction, the SQL statement must be a SELECT statement.

The following sample executes the following steps:

  • A connection request.
  • The query execution.
  • Prints the results to the console.
  • Closes the connection.

Sample JCA Program
/*
*
* This sample demonstrates the usage of the "Connect" JCA
* query adapter in a non-managed (2-tier) mode.
*
* Prerequisites:
* This program uses the default query adapter in "Connect".
*
* THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
* KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
* PURPOSE.
*
*/
import javax.resource.*;
import java.io.*;
import java.util.Vector;
import org.w3c.dom.*;
import com.attunity.adapter.*;
import com.attunity.adapter.core.*;

public class QuerySample
{

public QuerySample()
{
}

public static void main(String[] args) throws ResourceException,java.io.IOException
{
AttuManagedConFactory mcf = new AttuManagedConFactory();
// Ask user for server, user name and password information
configureEis(mcf);

String sSql = askUser("Enter the SELECT statement for execution: ");

AttuConnectionFactory cf =
(AttuConnectionFactory)mcf.createConnectionFactory ();
//-----------------------------------

try {
javax.resource.cci.Connection con = cf.getConnection();
javax.resource.cci.Interaction interaction =
con.createInteraction();
// To execute the query we use "query" interaction
AttuInteractionSpec iSpeq = new AttuInteractionSpec("query");

javax.resource.cci.RecordFactory rf = cf.getRecordFactory();
javax.resource.cci.MappedRecord queryRecord =
rf.createMappedRecord("query");
queryRecord.put("##text", sSql);
javax.resource.cci.Record oRec = interaction.execute(iSpeq,
queryRecord);
Element outEl = ((CoreDomRecord)oRec).getDom();

CoreDOMWriter domW = new CoreDOMWriter(false);
String xml = domW.toXMLString(outEl);

System.out.println("got back " + xml);

System.out.println("now using the get functions of the Record
interface");

/* Here we get an array of sub elements named record */
Vector recordV = (Vector)((CoreMappedRecord)oRec).get("#record[]");

if (recordV == null || recordV.isEmpty()) {
System.out.println("No sub elements named record");
return;
}

/* If the sql was select * from nv_dept and the output columns
* are dept_budget && dept_id here we print them .
*/
for (int objInd = 0; objInd < recordV.size(); objInd++) {

CoreMappedRecord record = (CoreMappedRecord)recordV.get(objInd);
String dept_budget = (String)record.get("@dept_budget");
String dept_id = (String)record.get("@dept_id");
System.out.println("dept_budget is " + dept_budget);
System.out.println("dept_id is " + dept_id);
}

interaction.close();
con.close();
}
catch (ResourceException ex) {
System.out.print("Exception in QuerySample: " + ex.getMessage());
ex.printStackTrace();
throw ex;
}
}

/*
* Configure EIS we are working with.
* Ask user for server, user name and password information
*/
static private void configureEis(AttuManagedConFactory mcf) throws
java.io.IOException, ResourceException
{
System.out.println();
System.out.println();
System.out.println("This sample demonstrates the work against 'query' EIS");
System.out.println("============================== =======================");
System.out.println();
System.out.println();

// The sample demonstrates the work with "query" EIS
mcf.setEisName("query");

String sServer = askUser("Please enter the IP of the daemon or server: ");

String sPort = askUser("Please enter the port number where daemon/server
is running (ENTER for default): ");
int iPort;

// Use default port
if(sPort.length() == 0)
sPort = "";

String sUser = askUser("Please enter the user name to connect the
daemon/server: ");
String sPassword = askUser("Please enter the password to connect the
daemon/server: ");

mcf.setServerName(sServer);
mcf.setPortNumber(sPort);
mcf.setUserName(sUser);
mcf.setPassword(sPassword);
}

/*
* Prints the question onto screen and returns typed answer
*/
static private String askUser(String sQuestion) throws java.io.IOException
{
InputStreamReader isr = new InputStreamReader(System.in);
char inBuf[] = new char[1000];
String sAnswer;

System.out.print(sQuestion);
isr.read(inBuf, 0, 999);
System.out.println();

sAnswer = new String(inBuf);
sAnswer = sAnswer.trim();
return sAnswer;
}
}