ADO 教程 (VJ++)

目录

本教程用于新版 ADO/WFC。

ASCimport 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;// 步骤 1—打开连接
         conn.open(strConn);
            
// 步骤 2—创建命令
         cmd.setActiveConnection(conn);
         cmd.setCommandText("SELECT * from authors");// 步骤 2—将具有源的记录集作为命令对象打开
         rs.setCursorLocation(AdoEnums.CursorLocation.CLIENT); 
         rs.setCursorType(AdoEnums.CursorType.DYNAMIC);
         rs.setLockType(AdoEnums.LockType.BATCHOPTIMISTIC);
         rs.open (cmd);
         
// 步骤 4—操作数据
         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() );
 
            //将字段的区域代码 415 更改为 779 
            fld =  rs.getField("phone");            strBuf.append( fld.getString());            strBuf.setCharAt(0, '7');
            strBuf.setCharAt(1, '7');
            strBuf.setCharAt(2,  '9');
            //将字段设置为新值
            fld.setString(strBuf.toString());
            
            rs.moveNext();
                  
         }// 步骤 5—更新设置的字段值
         conn.beginTrans();                 //STEP6 Part A: Conclude the Update
         try
         {            rs.updateBatch();
            conn.commitTrans();         }
// 步骤 6—结束更新 
                  catch(com.ms.wfc.data.AdoException ex)
         {
                 
           //出现错误,必须回卷事务
            rs.setFilter(new Integer                        (AdoEnums.FilterGroup.CONFLICTINGRECORDS));
            rs.moveFirst();            while(!rs.getEOF())
            {
               //打印冲突记录
               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();
      }
   }
}

VJ++ tutorial 教程到此结束。

www.2mysite.net