Avatar billede carstensm Nybegynder
15. februar 2002 - 12:39 Der er 9 kommentarer og
1 løsning

Transaktioner

Hvordan laver jeg en transaktion i Access fra hhv. Access egen manager og fra Java.

Findes der indbyggede integritetscheck (= constraints) i Access, og hvordan bruger man det?
Avatar billede terry Ekspert
15. februar 2002 - 13:00 #1
Your constraints should be done in Access. Open the realtionships windows and then add your tables by right clicking on the window and seelctingshow tabels. Then you can make th  reltioinships using drag and drop from one field to the other. Tenyou can modify the relationship by righ clicking on the thin line and following the instructions.
Avatar billede terry Ekspert
15. februar 2002 - 13:07 #2
// BeginBeginTransJ
import com.ms.wfc.data.*;
import java.io.*;

public class BeginTransX
{
  // The main entry point for the application.

  public static void main (String[] args)
  {
      BeginTransX();
      System.exit(0);
  }

  // BeginTransX function
  static void BeginTransX()
  {
      // Define ADO Objects.
      Connection cnConn1 = null;
      Recordset rstTitles = null;

      //Declarations.
      String strCnn = "Provider=sqloledb;Data Source=MyServer;"
        + "Initial Catalog=pubs;User Id=sa;Password=;";
      String strTitle;
      String strType;
      BufferedReader in =
        new BufferedReader (new InputStreamReader (System.in));
      String line = null;
      String strMessage="";
      int intChoice = 0;
      int recordDisplaySize = 15;
      int recordCount = 0;

      try
      {
        // Open a connection.

        cnConn1 = new Connection();
        cnConn1.open(strCnn,"","",AdoEnums.CommandType.UNSPECIFIED);

        // Open the Titles table.
        rstTitles = new Recordset ();
        rstTitles.setCursorType(AdoEnums.CursorType.DYNAMIC);
        rstTitles.setLockType(AdoEnums.LockType.PESSIMISTIC);
        rstTitles.open("titles",cnConn1,AdoEnums.CursorType.DYNAMIC ,
            AdoEnums.LockType.PESSIMISTIC ,AdoEnums.CommandType.TABLE );

        rstTitles.moveFirst();
        cnConn1.beginTrans();

        // Loop through recordset and ask user if he/she wants
        // to change the type for a specified title.

        while (!rstTitles.getEOF())
        {
            strType = rstTitles.getField("type").getString().trim();
            if ((rstTitles.getField("type").getString().
              trim()).compareTo("psychology")== 0)
            {
              strTitle = rstTitles.getField("title").getString().trim();
              System.out.println("\nTitle : " + strTitle + "\n\n"
                  +  "Change type to self help (1 -> Yes / 2 -> No) ?");
              // Change the title for the specified book.
              line = in.readLine().trim();
              intChoice = Integer.parseInt(line);
              if ( intChoice == 1)
              {
                  rstTitles.getField("type").setString("self_help");
                  rstTitles.update();
              }
            }
            rstTitles.moveNext();
        }

        // Ask if the user wants to commit to all the
        // changes made above.
        System.out.println
            ("\n\nSave all changes (1 -> Yes / 2 -> No) ?");
        line = in.readLine().trim();
        intChoice = Integer.parseInt(line);
        if ( intChoice == 1)
            cnConn1.commitTrans();
        else
            cnConn1.rollbackTrans();

        // Print current data in recordset.
        rstTitles.requery();
        rstTitles.moveFirst();

        while(true)
        {
            strMessage = strMessage +"\n " +
              rstTitles.getField("title").getString()+" - "
              + rstTitles.getField("type").getString() ;

            if ( recordCount == recordDisplaySize)
            {
              System.out.println(strMessage);
              System.out.println("\n\nPress <Enter> key to continue..");
              line = in.readLine();
              strMessage = "";
              recordCount = 0;
            }
            recordCount++;
            rstTitles.moveNext();
            if(rstTitles.getEOF())
            {
              System.out.println(strMessage);
              break;
            }
        }
        System.out.println("\n\nPress <Enter> key to continue..");
        line = in.readLine();

        // Restore original data because this
        // is a demonstration.
        rstTitles.moveFirst();
        while (!rstTitles.getEOF())
        {
            if ((rstTitles.getField("type").getString().
              trim().compareTo("self_help"))== 0)
            {
              rstTitles.getField("type").setString("psychology");
              rstTitles.update();
            }
            rstTitles.moveNext();
        }
        //Cleanup objects before exit.
        rstTitles.close();
        cnConn1.close();
      }
      catch( AdoException ae )
      {
        // Notify user of any errors that result from ADO.

        // Check for null pointer for connection object.
        if(rstTitles.getActiveConnection()==null)
            System.out.println("Exception: " + ae.getMessage());

        // As passing a Recordset, check for null pointer first.
        if (rstTitles != null)
        {
            PrintProviderError(rstTitles.getActiveConnection());
        }
        else
        {
            System.out.println("Exception: " + ae.getMessage());
        }
      }
      // System Read requires this catch.
      catch( java.io.IOException je )
      {
        PrintIOError(je);
      }
  }

  // PrintProviderError Function

  static void PrintProviderError( Connection Cnn1 )
  {
      // Print Provider errors from Connection object.
      // ErrItem is an item object in the Connection&#8217;s Errors collection.
      com.ms.wfc.data.Error  ErrItem = null;
      long nCount = 0;
      int  i      = 0;

      nCount = Cnn1.getErrors().getCount();

      // If there are any errors in the collection, print them.
      if( nCount > 0);
      {
        // Collection ranges from 0 to nCount - 1
        for (i = 0; i< nCount; i++)
        {
            ErrItem = Cnn1.getErrors().getItem(i);
            System.out.println("\t Error number: " + ErrItem.getNumber()
              + "\t" + ErrItem.getDescription() );
        }
      }

  }

  //.PrintIOError Function
 
  static void PrintIOError( java.io.IOException je)
  {
      System.out.println("Error \n");
      System.out.println("\tSource = " + je.getClass() + "\n");
      System.out.println("\tDescription = " + je.getMessage() + "\n");
  }
}

// EndBeginTransJ
Avatar billede terry Ekspert
15. februar 2002 - 13:08 #3
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/ado270/htm/mdmscadocodeexamples.asp

and then look under beginTrans in the programming language of your choice1
Avatar billede terry Ekspert
15. februar 2002 - 17:05 #4
carstensm>hows it going?
Avatar billede terry Ekspert
15. februar 2002 - 17:09 #5
the lines of importance are:

cnConn1.beginTrans();
Then
cnConn1.commitTrans();
or
cnConn1.rollbackTrans();
Avatar billede carstensm Nybegynder
15. februar 2002 - 18:07 #6
Thanks a lot. I don't have time to check it out now, but you are sure to get the points.

I was surprised that a small database such as Access has support for transactions and constraints. I'm more used to MySQL, but from now on Access may be my database of choice.

Thanks a lot for your rapid and elaborate response.
Avatar billede terry Ekspert
15. februar 2002 - 18:17 #7
hi again carstensm> Yes Access isnt as bad as many think! The above example is using ADO and should have no problem with Access+transactions. Are you also using ADO?
Avatar billede carstensm Nybegynder
15. februar 2002 - 18:22 #8
I have no idea about ADO, but I use a ODBC-connection. Are there situations where I cannot use transactions etc.?
Avatar billede terry Ekspert
15. februar 2002 - 18:27 #9
I dont think so!
Avatar billede terry Ekspert
19. februar 2002 - 19:34 #10
cartstensm>hows it going?
Avatar billede Ny bruger Nybegynder

Din løsning...

Tilladte BB-code-tags: [b]fed[/b] [i]kursiv[/i] [u]understreget[/u] Web- og emailadresser omdannes automatisk til links. Der sættes "nofollow" på alle links.

Loading billede Opret Preview
Kategori
Dyk ned i databasernes verden på et af vores praksisnære Access-kurser

Log ind eller opret profil

Hov!

For at kunne deltage på Computerworld Eksperten skal du være logget ind.

Det er heldigvis nemt at oprette en bruger: Det tager to minutter og du kan vælge at bruge enten e-mail, Facebook eller Google som login.

Du kan også logge ind via nedenstående tjenester