Home > Articles > Data > MySQL

  • Print
  • + Share This
Like this article? We recommend

Like this article? We recommend

Checking the Contents of the Database

There are a number of ways to check the database contents after running the Java code. The approach I tend to employ is to use the MySQL command prompt, as illustrated in Listing 8.

Listing 8—Displaying the Database Table (Person) Contents

mysql> select * from PERSON;
mysql> select * from PERSON;
+-----------+-----------+----------+---------+
| PERSON_ID | firstName | lastName | friends |
+-----------+-----------+----------+---------+
|         1 | Einstein  | Albert   | David   |
|         2 | Hilbert   | David    | Albert  |
|         3 | Dactyll   | Terry    | Albert  |
+-----------+-----------+----------+---------+
3 rows in set (0.00 sec)

Notice in Listing 8 that the Person table contains three rows—i.e., three people. What about the Department table? Listing 9 illustrates the Department table.

Listing 9—Displaying the Database table (Department) Contents

mysql> select * from DEPARTMENT;
+---------+----------+
| DEPT_ID | name     |
+---------+----------+
|       2 | ACCOUNTS |
|       1 | SOFTWARE |
+---------+----------+
2 rows in set (0.00 sec)

In Listing 9, we see two Department rows.

Finally, let's look at the contents of the DEPARTMENT_PERSON table in Listing 10.

Listing 10—Displaying the Database Table (Department) Contents

mysql> select * from DEPARTMENT_PERSON;
+--------------------+-------------------+
| Department_DEPT_ID | persons_PERSON_ID |
+--------------------+-------------------+
|                  1 |                 1 |
|                  1 |                 2 |
|                  2 |                 3 |
+--------------------+-------------------+
3 rows in set (0.00 sec)

Listing 10 is what's called a join table, which is used to model the relationship between the Person and Department instances. Try comparing the contents of Listing 10 with Listings 8 and 9 to see if you can figure out the relationships.

Explaining the Relationship Between the Entities

The relationship I wanted to model is a unidirectional one-to-many association—i.e., a department contains zero, one, or more persons. To create the association, I call code such as that illustrated in Listing 11.

Listing 11—Entity and Collection Creation

Person person1 = 
   createEntity.createPerson("Albert", "Einstein", "David");
Person person2 = 
   createEntity.createPerson("David", "Hilbert", "Albert");
Person person3 = 
   createEntity.createPerson("Terry", "Dactyll", "Albert");
createEntity.createDepartment("SOFTWARE");
createEntity.createDepartment("ACCOUNTS");
createEntity.addPersonToDept(person1, "SOFTWARE");
createEntity.addPersonToDept(person2, "SOFTWARE");
createEntity.addPersonToDept(person3, "ACCOUNTS");

Notice in Listing 11 that I create three instances of Person (person1, person2, and person3, respectively). I then create two instances of Department, and I then add each of the Person instances to one of the Department instances. That's all that's required to build the required relationships you saw in Listing 10.

  • + Share This
  • 🔖 Save To Your Account