How to Create a Java Visual Application that Links to an Access 2008 Database in Windows XP and Java: The Address Book Project
,






Exit your database.





This completes making the connection. Close Microsoft ODBC.
The following shows how to
create an Access 2008 database controlled by a visual Java Application.
To get a copy of the source code and the databases created using Access
2008 and Access 2003 format click here. To view the source code click here: copy of the source AddressBookDataBase.java.
Snapshot of the finished program
A. Make the Database
Open Access (Access 2008 in this tutorial, but it is
similar to Access 2003) and create a database called Address in your
root directory on your C: drive. Save this database to your root directory. I do not advise normally doing this on a routine basis. To
do this you will first have to go the top left corner of Access and
click on the colored circle with the 4 squares on it to trigger the new
option in the menu as shown below:
Click on the browse Button As shown below:
Navigate to your root directory as shown below and save
the file as Address.accdb (if you have Access 2008) or Address.mdb (if
you have Access 2003).
In the first column, right click at the very top of the
column (where it says add a new field) and select rename column and
rename this column Last (for last name). Leave the ID column alone. If
you are using Access 2003 you won't see an ID column and when you
finish, the last thing you will be asked is if you wish a numerical key
to be created, say yes.
Do not rename the ID column. Rename the next column
First and the next Phone. If you make any mistakes you probably will
want to start over. It is often easier to recreate the database and make
it perfect than it is to change the code. Enter some initial data in
the table which could look as follows:
Next go to file > save > and name your table AddressTable.
At this point your database file is saved. At this point your database should look as follows:
Exit your database.
B. Make the Connection
Close Microsoft Access (a connection cannot be
established with Access running). Locate the odbcad32.exe using windows
explorer found in your c:\windows\system32 directory and double click on
it. The ODBC Data Source Administrator program will open. Click the
System tab. Click the Add Button.
A “Create New Data Source” window will open, select Microsoft Access Driver (*.mdb, accdb) from the list and click finish:
In the ODBC Microsoft Access Setup window that appears
next, type myAddressBook in the field for Data Source Name and click the
Select Button.
In the Select Database window that opens navigate to and
select Address.accdb (if you are in Access 2008) or Address.mdb if you
are using Access 2003 and click OK.
Then click OK in the ODBC Microsoft Access Setup
window. Finally click OK in the Microsoft ODBC Data Source
Administrator Window.
This completes making the connection. Close Microsoft ODBC.
C. Make the program
Start Eclipse or TextPad (or some other java text
editor). Start a new java application project/program called
AddressBookDataBase which looks as follows. The following program
actually works, you can simply copy and paste it as source. You can
also click here for a copy of the source AddressBookDataBase.java.
A couple words of caution: Before coding close Access. Also, this
code is not easily adaptable to an applet. Applications have more
permissions to write and save to file than do applets. It is however
possible to convert this program if you know what you are doing by
adjusting your computer's file permissions using the Java policy editor.
0 comments:
Post a Comment