17:32

A Java Program Which Makes System Calls to Execute Programs from DOS


The following program will call a Console or DOS program written in the Windows XP environment. To get this program to work put a program called helloworld.exe perhaps compiled in C++ in the same directory as myclass.java. Click here for a copy of a program helloword.exe made in C++ to put in the directory with the file myclass.java.
import java.io.IOException;
class myclass
{
          public static void main(String[] args) throws IOException, InterruptedException
          {
                    Process p = Runtime.getRuntime().exec("cmd /c start /MIN helloworld.exe");
                    p.waitFor();
          }
}

How to Create a Java Visual Application that Links to an Access 2008 Database in Windows XP and Java:  The Address Book Project 
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.

Database program

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:
Access Program,
Click on the browse Button As shown below:
Browse Button in Access
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).
Saving the Address database
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.
Renaming Columns in Access
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:
Finished Database
Next go to file > save > and name your table AddressTable.
Name Table
At this point your database file is saved.  At this point your database should look as follows:
Finished Database
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. 
ODBC
A “Create New Data Source” window will open, select Microsoft Access Driver (*.mdb, accdb) from the list and click finish: 
Create New Data Source
In the ODBC Microsoft Access Setup window that appears next, type myAddressBook in the field for Data Source Name and click the Select Button. 
ODBC Data Source Name
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. 
Select Address accdb in ODBC
Then click OK in the ODBC Microsoft Access Setup window.  Finally click OK in the Microsoft ODBC Data Source Administrator Window. 
Finalize ODBC with OK
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: