17:34

Setting up a Visual Java Address Book Program Which Accesses A MySQL Database


Click here for a for the source code to MyAddressSQL.java or here to view the source in a web page.
  1. Goto mysql.org and under downloads install the mycommunity mysql server
And download mySQL database server 6.0. After install goto to mysql in your programs and configure the server program. Install a path to mysql in windows.
  1. Goto downloads and download and install MySQL Connector/ODBC 3.51 Downloads which is the windows msi file 3.51.25 .
  2. Goto mysql.org and download and install the mysql connector/j
...may be necessary to add mysql-connector-java-3.1.14-bin.jar to the MySQL directory in Program files and to add a reference to this directory and this file in CLASSPATH in System>Advanced>Environment Variables of the control panel
To Run MySql first navigate to the MySQL commandline Client as shown below and click it.
MySQL CommandLine Client
If your password was setup to be student type student under enter the password.
Enter Password
Create the myaddress4 database as follows (use the password student if you have it set to this).
Enter password: *******
Welcome to the MySQL monitor. Commands end with ; or \g.
Your MySQL connection id is 1 to server version: 4.1.22-community-nt
Type 'help;' or '\h' for help. Type '\c' to clear the buffer.
mysql> show databases;
+----------+
| Database |
+----------+
| mysql |
| test |
+----------+
2 rows in set (0.08 sec)
mysql> drop DATABASE IF EXISTS myaddressbook4;
mysql> create database myaddressbook4;
Query OK, 1 row affected (0.01 sec)
mysql> show databases;
+----------------+
| Database |
+----------------+
| myaddressbook4 |
| mysql |
| test |
+----------------+
3 rows in set (0.00 sec)
mysql> use myaddressbook4;
Database changed
mysql> create table phonelistTable(id INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(id), Last VARCHAR(255), First VARCHAR(255), Phone VARCHAR(255));
Query OK, 0 rows affected (0.17 sec)
mysql> insert into phonelistTable(Last,First,Phone) values ('Krumm','Jim','123-4567');
Query OK, 1 row affected (0.05 sec)
mysql> insert into phonelistTable(Last,First,Phone) values ('Powers','Austin','444-4567');
Query OK, 1 row affected (0.01 sec)
mysql> insert into phonelistTable(Last,First,Phone) values ('Slick','Tom','555-4567');
Query OK, 1 row affected (0.02 sec)
mysql> insert into phonelistTable(Last,First,Phone) values ('Timberlake','Justin
','555-4567');
Query OK, 1 row affected (0.03 sec)
mysql> show tables;
+--------------------------+
| Tables_in_myaddressbook4 |
+--------------------------+
| phonelisttable |
+--------------------------+
1 row in set (0.00 sec)
mysql> select * from phonelistTable;
+------------+--------+----------+
mysql> select * from phonelistTable;
+----+------------+---------+----------+
| id | Last | First | Phone |
+----+------------+---------+----------+
| 1 | Krumm | Jim | 123-4567 |
| 2 | Powers | Austin | 444-4567 |
| 3 | Slick | Tom | 555-4567 |
| 4 | Timberlake | Justin | 555-4567 |
+----+------------+---------+----------+
4 rows in set (0.00 sec)4 rows in set (0.03 sec)
mysql>exit

Next make a connection for phonelistTable by first running odbcad32.exe. (This program is in your windows directory, when you find it send the shortcut to your desktop.) Select the System Tab as shown and click add.
ODBC Data Source Administrator
Next Select the MySQL ODBC 3.51 Driver as shown below.
Create New Data Source
Set up the following MySQL window as follows (set user to root and password to student and test the connection) and click OK.
MySQL Connector/ODBC
Now that the database is configured, as shown in the following window, click OK.
ODBC Data Source Administrator
3: Make a new Java project in eclipse or your favorite Java IDE and click here to view the code to AddressBookMySql in a web page or click here to download the source file.
4. Below is a screen shot of the program running.
MySQL Database Java Application

0 comments: