ODBC Example for Visual J++

Take the following steps to create and use a simple ODBC database, containing only images.

Note: The LEADTOOLS ActiveX requires ODBC drivers that can properly handle long binary fields. Examples have been tested and will work with version 3.0 of the Microsoft ODBC Desktop Driver Pack. (This driver pack ships with Access 95 and works on 32-bit systems.) You can download drivers from the LEAD BBS, the CompuServe forum, or the Worldwide Web home page. The CompuServe forum is at GO LEADTECH. The web page is ftp://ftp.leadtools.com/pub/utils/ODBC32.zip. The file to download is ODBC32.ZIP.

1. Make a copy of the project that you created in Loading and Displaying an Image; then load the copied project.

2. Create an Access database named c:\lead\lead.mdb.

3. Create a table with one field as follows:

Name:  ltvbpic
Field Name:  photo
Data Type:  long binary

4. Open the ODBC administrator and click the Add button.

5. Select Microsoft Access driver and click the OK button.

6. Enter the name LEADACCESS.

7. Click the Select directory button and select the LEAD directory.

8. Click the OK button; click the next OK button; then click the Close button.

  1. 9.  In Visual J++, replace the code in the main form's constructor as follows:

public Form1()
{
   // Required for Visual J++ Form Designer support
   initForm();     

   // TODO: Add any constructor code after initForm call

   Rectangle r = SystemInformation.getWorkingArea();

   // Position and size the main form so that it takes up most of the screen.
   setWidth( (int) ( r.width * 0.9f ) );
   setHeight( (int) ( r.height * 0.8f ) );
   setLeft( ( r.width - getWidth() ) / 2 );
   setTop( ( r.height - getHeight() ) / 2 );

   // Set the position and size of the LEAD control.
   // Allow for a border of 1/8 of the form size.
   // The units of measure do not matter, since we are calculating proportions.
   int nHeightAllowed = getSize().y * 3 / 4;
   int nWidthAllowed = getSize().x * 3 / 4;

   LEAD1.setLeft( ( getSize().x - nWidthAllowed ) / 2 );
   LEAD1.setTop( ( getSize().y - nHeightAllowed) / 2 );
   LEAD1.setWidth( nWidthAllowed );
   LEAD1.setHeight( nHeightAllowed );
   // Turn off automatic repainting and turn on scroll bars.
   LEAD1.setAutoRepaint( false );
   LEAD1.setAutoScroll( true );
   // Set the image display size to match the LEAD control
   LEAD1.SetDstRect( 0, 0, LEAD1.getScaleWidth(), LEAD1.getScaleHeight() );
   LEAD1.SetDstClipRect( 0, 0, LEAD1.getScaleWidth(), LEAD1.getScaleHeight() );
   // Display the LEAD control.
   LEAD1.setBorderStyle( (short) 1 );

   // Open the database.
   String strDatabase = "ODBC;DSN=LEADACCESS";
   LEAD1.dbOpen( strDatabase, "ltvbpic", "photo", LTOCXU.dbOpenOptionsConstants.DB_OPENOPTIONS_NONE );
   LEAD1.setDbLoadBits( (short) 24 );
   LEAD1.setDbLockingMode( (short) LTOCXU.dbLockingModeConstants.DB_LOCKINGMODE_OPTIMISTIC );
   LEAD1.ForceRepaint();
   if( LEAD1.getDbIsBOF() && LEAD1.getDbIsEOF() )
   {
      MessageBox.show( "The database is empty.", "Notice", MessageBox.OK );
      Next.setEnabled( false );
      Prev.setEnabled( false );
   }
}

10. Add a common dialog control to your main form.

11. At the top of your main form, add seven Buttons and name them as follows: Add, Update, Delete, First, Next, Prev, and Last.

12. For the Add button, add the following code to the click event:

if( !LEAD1.getDbCanAppend() )
{
   MessageBox.show( "You cannot add to this database.", "Error", MessageBox.OK );
   return;
}

LEAD1.dbAddNew();
openFileDialog1.setFilter( "All Files|*.*" );
openFileDialog1.setCheckFileExists( true );
openFileDialog1.setTitle( "Open File" );
openFileDialog1.showDialog();
String strFileName = openFileDialog1.getFileName();
LEAD1.Load( strFileName, (short) 0, (short) 0, (short) 1 );
short sRet = LEAD1.dbUpdate( (short) LTOCXU.FileConstants.FILE_CMP, (short) 0, 
                             (short) LTOCXU.QFactorConstants.QFACTOR_QMS );
if( sRet != 0 )
{
   MessageBox.show( "You cannot update this database.", "Error", MessageBox.OK );
   return;
}

LEAD1.dbMoveLast();
LEAD1.ForceRepaint();
MessageBox.show( "Image added to the database.", "LEAD", MessageBox.OK );
if( LEAD1.getDbIsBOF() )
   Prev.setEnabled( false );
else
   Prev.setEnabled( true );
if( LEAD1.getDbIsEOF() )
   Next.setEnabled( false );
else
   Next.setEnabled( true );

13. For the Update button, add the following code to the click event:

if( !LEAD1.getDbCanUpdate() )
{
   MessageBox.show( "You cannot update this database.", "Error", MessageBox.OK );
   return;
}

if( LEAD1.getDbIsBOF() || LEAD1.getDbIsEOF() )
{
   MessageBox.show( "There is no current record.", "Error", MessageBox.OK );
   return;
}

if( LEAD1.getDbEditMode() == LTOCXU.dbEditModeConstants.DB_EDITMODE_NONE )
{
   LEAD1.dbEdit();
   LEAD1.Flip();
   LEAD1.ForceRepaint();
}
LEAD1.dbUpdate( (short) LTOCXU.FileConstants.FILE_CMP, (short) 0, (short) LTOCXU.QFactorConstants.QFACTOR_QMS );

14. For the Delete button, add the following code to the click event:

if( !LEAD1.getDbCanUpdate() )
{
   MessageBox.show( "You cannot update this database.", "Error", MessageBox.OK );
   return;
}

if( LEAD1.getDbIsBOF() || LEAD1.getDbIsEOF() )
{
   MessageBox.show( "There is no current record.", "Error", MessageBox.OK );
   return;
}

LEAD1.dbDelete();
LEAD1.dbRequery();
LEAD1.dbMoveFirst();
if( LEAD1.getDbIsBOF() && LEAD1.getDbIsEOF() )
{
   Next.setEnabled( false );
   Prev.setEnabled( false );
}
LEAD1.ForceRepaint();

15. For the First button, add the following code to the click event:

if( LEAD1.getDbIsBOF() && LEAD1.getDbIsEOF() )
{
   MessageBox.show( "The database is empty.", "Notice", MessageBox.OK );
   Next.setEnabled( false );
   Prev.setEnabled( false );
}
else
{
   LEAD1.dbMoveFirst();
   Prev.setEnabled( false );
   Next.setEnabled( true );
   LEAD1.ForceRepaint();
}

16. For the Next button, add the following code to the click event:

if( LEAD1.getDbIsBOF() && LEAD1.getDbIsEOF() )
{
   Prev.setEnabled( false );
   Next.setEnabled( false );
   MessageBox.show( "The database is empty.", "Notice", MessageBox.OK );
}
else
{
   LEAD1.dbMoveNext();
   if( LEAD1.getDbIsEOF() )
   {
      Next.setEnabled( false );
      LEAD1.dbMovePrev();
      MessageBox.show( "Already at last record.", "Notice", MessageBox.OK );
   }
   else
      Prev.setEnabled( true );
   LEAD1.ForceRepaint();
}

17. For the Prev button, add the following code to the click event:

if( LEAD1.getDbIsBOF() && LEAD1.getDbIsEOF() )
{
   Prev.setEnabled( false );
   Next.setEnabled( false );
   MessageBox.show( "The database is empty.", "Notice", MessageBox.OK );
}
else
{
   LEAD1.dbMovePrev();
   if( LEAD1.getDbIsBOF() )
   {
      Prev.setEnabled( false );
      LEAD1.dbMoveNext();
      MessageBox.show( "Already at first record.", "Notice", MessageBox.OK );
   }
   else
      Next.setEnabled( true );
   LEAD1.ForceRepaint();
}

18. For the Last button, add the following code to the click event:

if( LEAD1.getDbIsBOF() && LEAD1.getDbIsEOF() )
{
   MessageBox.show( "The database is empty.", "Notice", MessageBox.OK );
   Next.setEnabled( false );
   Prev.setEnabled( false );
}
else
{
   LEAD1.dbMoveLast();
   Next.setEnabled( false );
   Prev.setEnabled( true );
   LEAD1.ForceRepaint();
}

19. Update the main form's closing method as follows:

private void Form1_closing(Object source, CancelEvent e)
{
   LEAD1.dbClose();
}

20. Run your program to test it.