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.
//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();
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’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() ); } }
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.
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?
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.