ADO Tutorial (VJ++)

See Also   

This tutorial features the new ADO/WFC.

import com.ms.wfc.data.*;

/**
* ADOTutorial:
* Purpose: Demonstrates the usage of Ado in Java. 
*          opens a recordset through a command object
*          and illustrates update within a transaction
*/
public class ADOTutorial
{
   public static String strConn = "Driver={SQL Server};SERVER=JDO_ODIN;DATABASE=JetQA;UID=testmod;PWD=testmod;";//odbc type conn string

   public static void main(String args[] )
   {
      try
      {
         Connection conn = new Connection();
         Command cmd = new Command();
         Recordset rs = new Recordset();
      
         int actErrorNum = 0;
         Field fld;
         AdoProperties fldProps;

//STEP1 : open the connection

         conn.open(strConn);
            

//STEP 2: Create a command

         cmd.setActiveConnection(conn);
         cmd.setCommandText("SELECT * from authors");

//Step 3- Open the recordset with the source as a command object

         rs.setCursorLocation(AdoEnums.CursorLocation.CLIENT); 
         rs.setCursorType(AdoEnums.CursorType.DYNAMIC);
         rs.setLockType(AdoEnums.LockType.BATCHOPTIMISTIC);
         rs.open (cmd);
         

//STEP 4 - Manipulate the data

         fldProps  = rs.getField("au_lname").getProperties();
         
                           fldProps.getItem("Optimize").setBoolean(true);

         rs.setSort("au_lname");

         rs.setFilter("phone like '415*'"); 

         rs.moveFirst();

         while ( !rs.getEOF())

         { 
            StringBuffer strBuf = new StringBuffer();
            System.out.println( " Name: " + rs.getField("au_fname").getString() + 
               "  " + rs.getField("au_lname").getString() +
               "   Phone : " + rs.getField("phone").getString() );
 
            //change the area code 415 to 779 for the field 
            fld =  rs.getField("phone");

            strBuf.append( fld.getString());

            strBuf.setCharAt(0, '7');
            strBuf.setCharAt(1, '7');
            strBuf.setCharAt(2,  '9');


            //set the field to the new value
            fld.setString(strBuf.toString());
            
            rs.moveNext();
                  
         }

// STEP5 - update the set field values

         conn.beginTrans();

                 //STEP6 Part A: Conclude the Update
         try
         {

            rs.updateBatch();
            conn.commitTrans();

         }

//STEP6: Conclude the Update

         

         catch(com.ms.wfc.data.AdoException ex)
         {
                 
           //error has occured we have to rollback 
           //transaction
            rs.setFilter(new Integer                        (AdoEnums.FilterGroup.CONFLICTINGRECORDS));
            rs.moveFirst();

            while(!rs.getEOF())
            {
               //print conflicting records
               System.out.println(" Conflict : Name : "+ rs.getField("au_fname").getString() + " " +
                  rs.getField("au_lname").getString() );
               
               rs.moveNext();
            }
            conn.rollbackTrans();
         }
             System.out.println("type any character to continue...");
         System.in.read();

      }
      catch(Exception ex)
      {
         ex.printStackTrace();
      }
   }
}

This is the end of the VJ++ tutorial.