orz.neptune.prospring3.ch8.JdbcContactDaoSample.java Source code

Java tutorial

Introduction

Here is the source code for orz.neptune.prospring3.ch8.JdbcContactDaoSample.java

Source

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package orz.neptune.prospring3.ch8;

import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;
import org.springframework.context.support.GenericXmlApplicationContext;

/**
 *
 * @author Administrator
 */
public class JdbcContactDaoSample {

    public static void main(String[] args) {
        GenericXmlApplicationContext ctx = new GenericXmlApplicationContext();
        ctx.load("classpath:orz/neptune/prospring3/ch8/config/datasource-drivermanager.xml");
        ctx.refresh();
        ContactDao contactDao = ctx.getBean("contactDao", ContactDao.class);
        // Find first name by id
        //        System.out.println("First name for contact id 1 is: "
        //                + contactDao.findFirstNameById(1l));
        //
        //        System.out.println("Last name using named parameter for contact id 1 is: "
        //                + contactDao.findLastNameByIdUsingNamedParameter(1l));

        //findAll ???
        //        List<Contact> contacts = contactDao.findAll();
        //
        //        for (Contact contact : contacts) {
        //            System.out.println(contact);
        //            if (contact.getContactTelDetails() != null) {
        //                for (ContactTelDetail contactTelDetail
        //                        : contact.getContactTelDetails()) {
        //                    System.out.println("---" + contactTelDetail);
        //                }
        //            }
        //            System.out.println();
        //        }
        //        Contact contact = new Contact();
        //
        //        contact.setId(1L);
        //        contact.setFirstName("Clarence");
        //        contact.setLastName("Peter");
        //        contact.setBirthDate(new Date((new GregorianCalendar(1977, 10,
        //                1)).getTime().getTime()));
        //        contactDao.update(contact);
        //        List<Contact> contacts = contactDao.findAll();
        //        listContacts(contacts);

        //insertTest(contactDao);

        System.out.println(contactDao.getFirstNameById(1L));
    }

    private static void insertTest(ContactDao contactDao) {
        Contact contact = new Contact();
        contact.setFirstName("peng");
        contact.setLastName("zhou");
        contact.setBirthDate(new Date((new GregorianCalendar(1984, 1, 28)).getTime().getTime()));
        contactDao.insert(contact);
        List<Contact> contacts = contactDao.findAll();
        listContacts(contacts);
    }

    //???contact
    private static void listContacts(List<Contact> contacts) {

        for (Contact contact : contacts) {
            System.out.println(contact);
            if (contact.getContactTelDetails() != null) {
                for (ContactTelDetail contactTelDetail : contact.getContactTelDetails()) {
                    System.out.println("---" + contactTelDetail);
                }
            }
            System.out.println();
        }
    }
}