img
System.out.println("Deleting the previous created contact");
contactDao.delete(contact.getId());
System.out.println("Listing contact data after new contact deleted:");
listAllContacts();
}
private static void listAllContacts() {
List<Contact> contacts = contactDao.findAll();
for (Contact contact: contacts) {
System.out.println(contact);
}
}
}
To run the program, you need to add the dependency for MySQL Java into your project, as shown in
Table 8-1.
Table 8-1. Dependency for MySQL
Group ID
Artifact ID
Version
Description
5.1.18
MySQL Java driver library
mysql
mysql-connector-java
Running the program in Listing 8-9 will yield the following result (assuming you have a locally
installed MySQL database, with a database called prospring3_ch8 that has a user name and password set
to prospring3; it should be able to access the database schema, and you should run the scripts
schema.sql and test-data.sql against the database to create the tables and populated the initial data):
Listing
initial contact data:
Contact
- Id: 1, First name: Clarence, Last name: Ho, Birthday: 1980-07-30
Contact
- Id: 2, First name: Scott, Last name: Tiger, Birthday: 1990-11-02
Contact
- Id: 3, First name: John, Last name: Smith, Birthday: 1964-02-28
Insert a new contact
Listing contact data after new contact created:
Contact - Id: 1, First name: Clarence, Last name: Ho, Birthday: 1980-07-30
Contact - Id: 2, First name: Scott, Last name: Tiger, Birthday: 1990-11-02
Contact - Id: 3, First name: John, Last name: Smith, Birthday: 1964-02-28
Contact - Id: 4, First name: Jacky, Last name: Chan, Birthday: 2001-11-01
Deleting the previous created contact
Listing contact data after new contact deleted:
Contact - Id: 1, First name: Clarence, Last name: Ho, Birthday: 1980-07-30
Contact - Id: 2, First name: Scott, Last name: Tiger, Birthday: 1990-11-02
Contact - Id: 3, First name: John, Last name: Smith, Birthday: 1964-02-28
As shown in the output, the first block of lines shows the initial data. The second block of lines shows
that the new record was added. The final block of lines shows that the newly created contact was deleted.
As you can see from Listing 8-8, a lot of code needs to be moved to a helper class or--even worse--
duplicated in each DAO class. This is the main disadvantage of JDBC from the application programmer's
Search WWH :
Custom Search
Previous Page
Spring Framework 3 Topic Index
Next Page
Spring Framework 3 Bookmarks
Home