ADO Tutorial (Visual J++)
This is the ADO tutorial written in Microsoft Visual J++, featuring the ADO/WFC classes. See the ADO Tutorial for a description of the purpose of this tutorial.
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
{
//odbc-style connection string
public static String strConn =
"Driver={SQL Server};SERVER=YourServer;DATABASE=JetQA;" +
"UID=testmod;PWD=testmod;";
public static void main(String args[] ) // Tutorial in VJ
{
try
{
Connection conn = new Connection();
Command cmd = new Command();
Recordset rs = new Recordset();
Field fld;
AdoProperties fldProps;
// STEP 1: 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 5*'");
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 area code 415 to 777.
fld = rs.getField("phone");
strBuf.append( fld.getString());
strBuf.setCharAt(0, '7');
strBuf.setCharAt(1, '7');
strBuf.setCharAt(2, '7');
//set the field to the new value
fld.setString(strBuf.toString());
rs.moveNext();
}
rs.setFilter(new Integer(AdoEnums.FilterGroup.NONE));
// STEP 5: Update the data
conn.beginTrans();
try
{
rs.updateBatch();
// STEP 6 PART A: Conclude the Update - Accept changes
conn.commitTrans();
}
// STEP 6 PART B: Conclude the Update - Reject changes
catch(AdoException ex)
{
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();
}
rs.close();
conn.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
}
This is the end of the Visual J++ tutorial.