Article directory
1. Design stage
1.1 demand analysis
The address book needs to store name, address, telephone number, postal code, Email, home phone and other information.
The basic management functions that the program shall provide are:
1. Add: add a person's record (including name, address, phone number, zip code, Email, home phone and other information) to the address book.
2. Display: display all contact information (including name, address, phone number, postcode, Email, home phone, etc.) in all contacts in the address book on the screen.
3. Store: save the address book information in the database table.
4. Query: you can search for someone's relevant information according to their name. If you find other information (including name, address, phone number, zip code, Email, home phone, etc.) that displays the contact.
5. Modify: enter a person's name. If the name exists, other contents will be modified. If not, modification failure will be displayed
6. Sort: all entries can be sorted according to an item of the entry, such as name.
1.2 function diagram of address book management system
1.3 use case diagram of address book management system
1.4 E-R diagram of address book management system
2. Database design
3. Program design
2.1function
2.1.1 design ContactPerson class
package function; //Contact human public class ContactPerson { private String name; private String address; private String num; private String postal_code; //Postal Code private String e_mail; private String home_phon; //Home phone public void setName(String name) { this.name = name; } public void setAdd(String address) { this.address = address; } public void setNum(String num) { this.num = num; } public void setPostal_code(String postal_code) { this.postal_code = postal_code; } public void setE_mail(String e_mail) { this.e_mail = e_mail; } public void setHome_phon(String home_phon) { this.home_phon = home_phon; } public String getName() { return name; } public String getAdd() { return address; } public String getNum() { return num; } public String getPostal_code() { return postal_code; } public String getE_mail() { return e_mail; } public String getHome_phon() { return home_phon; } @Override public String toString() { return "ContactPerson{" + "name='" + name + '\'' + ", address='" + address + '\'' + ", num='" + num + '\'' + ", postal_code='" + postal_code + '\'' + ", e_mail='" + e_mail + '\'' + ", home_phon='" + home_phon + '\'' + '}'; } }
2.1.2 design DBUtils class
package function; import java.sql.*; public class DBUtils { private static String url = "jdbc:mysql://127.0.0.1:3306/text?useSSL=false&useUnicode=yes&characterEncoding=UTF-8"; private static String user = "root"; //Database user name private static String password = "123456"; //Database password public static Connection getConn() throws SQLException, ClassNotFoundException { //1. Load driver Class.forName("com.mysql.jdbc.Driver"); //2. Get a connection to the database return DriverManager.getConnection(url, user, password); } public static void close(Statement stat, Connection conn, ResultSet res) { //Close result set if( stat != null){ try { stat.close(); } catch (SQLException e) { e.printStackTrace(); } } //close command if( conn != null){ try { conn.close(); } catch (SQLException e) { e.printStackTrace(); } } //Close connection if( res != null){ try { res.close(); } catch (SQLException e) { e.printStackTrace(); } } } }
2.1.3 design the ContactHandle interface
package function;//Address book function interface import java.sql.SQLException; public interface ContactHandle { public abstract void add(String name,String address,String num,String postal_code,String e_mail,String home_phon); //Add to public abstract void remove(String name) throws SQLException, ClassNotFoundException; //delete public abstract void update(String name,String address,String num,String postal_code,String e_mail,String home_phon); //modify public abstract ContactPerson selectName(String name); //Search by name public abstract ContactPerson selectNum(String num); //Query by phone public abstract void sort() throws SQLException; //sort public abstract void show(); //Show all contacts public void save(); //Save to local }
2.1.4 design ContactFunction class
package function; import com.mysql.jdbc.Connection; import java.io.*; import java.sql.PreparedStatement; import java.sql.ResultSet; import java.sql.SQLException; import java.util.ArrayList; import java.util.List; public class ContactFunction implements ContactHandle { private Connection conn; private PreparedStatement pst; private ResultSet res; @Override public void add(String name, String address, String num, String postal_code, String e_mail, String home_phon) { try { conn = (Connection) DBUtils.getConn(); String sql = "insert into conperson(name,address,num,postal_code,e_mail,home_phon) values (?,?,?,?,?,?)"; pst=conn.prepareStatement(sql); // Precompiled sql statements //This method will load the SQL statement into the driver conn integrator //But it doesn't execute directly, but only when it calls the execute() method pst.setString(1,name); //Assign a value to the first placeholder pst.setString(2,address); pst.setString(3,num); pst.setString(4,postal_code); pst.setString(5,e_mail); pst.setString(6,home_phon); pst.executeUpdate(); // Execute SQL statement } catch (SQLException e) { } catch (ClassNotFoundException e) { }finally { DBUtils.close(pst,conn,res); } } @Override public void remove(String name) { try { conn = (Connection) DBUtils.getConn(); String sql = "delete from conperson where name=?"; pst=conn.prepareStatement(sql);// Precompiled sql statements pst.setString(1,name); pst.executeUpdate();// Execute SQL statement } catch (SQLException e) { } catch (ClassNotFoundException e) { }finally { DBUtils.close(pst,conn,res); } } @Override public void update(String name,String address,String num,String postal_code,String e_mail,String home_phon) { ContactFunction contactFunction = new ContactFunction(); contactFunction.remove(name); contactFunction.add(name,address,num,postal_code,e_mail,home_phon); } @Override public ContactPerson selectName(String name) { ContactPerson person = null; try{ conn = (Connection) DBUtils.getConn(); String sql = "select * from conperson where name = ?"; pst=conn.prepareStatement(sql);// Precompiled sql statements pst.setString(1,name); res = pst.executeQuery();// Execute SQL statement if(res.next()){ person = new ContactPerson(); person.setName(res.getString("name")); person.setAdd(res.getString("address")); person.setNum(res.getString("num")); person.setPostal_code(res.getString("postal_code")); person.setE_mail(res.getString("e_mail")); person.setHome_phon(res.getString("home_phon")); } }catch (SQLException e){ e.printStackTrace(); } catch (ClassNotFoundException e) { e.printStackTrace(); } catch (Throwable e) { e.printStackTrace(); }finally { DBUtils.close(pst,conn,res); return person; } } @Override public ContactPerson selectNum(String num) { ContactPerson person = null; try{ conn = (Connection) DBUtils.getConn(); String sql = "select * from conperson where num = ?"; pst=conn.prepareStatement(sql);// Precompiled sql statements pst.setString(1,num); res = pst.executeQuery();// Execute SQL statement if(res.next()) { person = new ContactPerson(); person.setName(res.getString("name")); person.setAdd(res.getString("address")); person.setNum(res.getString("num")); person.setPostal_code(res.getString("postal_code")); person.setE_mail(res.getString("e_mail")); person.setHome_phon(res.getString("home_phon")); } }catch (SQLException e){ } catch (ClassNotFoundException e) { }finally { DBUtils.close(pst,conn,res); return person; } } @Override public void sort() { List<ContactPerson> list = new ArrayList<>(); try { conn = (Connection) DBUtils.getConn(); String sql = "SELECT * FROM conperson ORDER BY CONVERT(NAME USING GB2312)"; pst = conn.prepareStatement(sql); res = pst.executeQuery(); while(res.next()){ ContactPerson person = new ContactPerson(); person.setName(res.getString("name")); person.setAdd(res.getString("address")); person.setNum(res.getString("num")); person.setPostal_code(res.getString("postal_code")); person.setE_mail(res.getString("e_mail")); person.setHome_phon(res.getString("home_phon")); list.add(person); } for(ContactPerson c:list){ System.out.println(c.toString()); } } catch (SQLException e) { } catch (ClassNotFoundException e) { }finally { DBUtils.close(pst,conn,res); } } @Override public void show() { List<ContactPerson> list = new ArrayList<>(); try { conn = (Connection) DBUtils.getConn(); String sql = "select * from conperson"; pst=conn.prepareStatement(sql); res = pst.executeQuery(); while(res.next()){ ContactPerson person = new ContactPerson(); person.setName(res.getString("name")); person.setAdd(res.getString("address")); person.setNum(res.getString("num")); person.setPostal_code(res.getString("postal_code")); person.setE_mail(res.getString("e_mail")); person.setHome_phon(res.getString("home_phon")); list.add(person); } for(ContactPerson c:list){ System.out.println(c.toString()); } } catch (SQLException e) { } catch (ClassNotFoundException e) { }finally { DBUtils.close(pst,conn,res); } } @Override public void save(){ try { conn = (Connection) DBUtils.getConn(); List<ContactPerson> list = new ArrayList<>(); String sql = "select * from conperson"; pst=conn.prepareStatement(sql); res = pst.executeQuery(); while(res.next()){ ContactPerson person = new ContactPerson(); person.setName(res.getString("name")); person.setAdd(res.getString("address")); person.setNum(res.getString("num")); person.setPostal_code(res.getString("postal_code")); person.setE_mail(res.getString("e_mail")); person.setHome_phon(res.getString("home_phon")); list.add(person); } File writename = new File("data\\test.text"); BufferedWriter out = new BufferedWriter(new FileWriter(writename)); //BufferedWriter character buffer stream //In order to improve the efficiency of character stream reading and writing, buffer mechanism is introduced to read and write characters in batches, which improves the efficiency of single character reading and writing for(ContactPerson c:list){ out.write(c.toString()); out.write("\r\n"); // \r\n is line feed } out.flush(); // Push cache contents into file out.close(); // Remember to close the file at the end } catch (Exception e) { e.printStackTrace(); }finally { DBUtils.close(pst,conn,res); } } }
2.2ContactTest
import function.ContactFunction; import function.ContactPerson; import java.sql.SQLException; import java.util.Scanner; public class ContactTest { public static void main(String[] args) throws SQLException, ClassNotFoundException { ContactFunction address = new ContactFunction(); Scanner sc = new Scanner(System.in); while (true) { System.out.println("***********[A]Add to [B]modify***********"); System.out.println("***********[C]delete [D]display***********"); System.out.println("***********[E]Query by name [F]Query according to mobile number*"); System.out.println("***********[G]Sort by name [H]Save address book*****"); System.out.println("********************[I]Sign out*******************"); System.out.println("**********Please enter your choice case insensitive*********"); String strFountion = sc.nextLine(); if ("a".equalsIgnoreCase(strFountion)) { // Add to System.out.print("Please enter the name you want to add:"); String name = sc.nextLine(); System.out.print("Please enter the address you want to add:"); String adr = sc.nextLine(); System.out.print("Please enter the phone number you want to add:"); String num = sc.nextLine(); System.out.print("Please enter the zip code you want to add:"); String post = sc.nextLine(); System.out.print("Please enter the email you want to add:"); String e = sc.nextLine(); System.out.print("Please enter the home phone you want to add:"); String phone= sc.nextLine(); address.add(name,adr,num,post,e,phone); } else if ("b".equalsIgnoreCase(strFountion)) {// modify System.out.println("Please enter the name of the contact you want to modify?"); String name = sc.nextLine(); if(address.selectName(name)==null){ System.out.println("Revised"+name+"Non-existent\r\n"+"Modification failed!"); }else { System.out.print("Please enter the modified address:"); String adr = sc.nextLine(); System.out.print("Please enter the modified phone number:"); String num = sc.nextLine(); System.out.print("Please enter the modified postal code:"); String post = sc.nextLine(); System.out.print("Please enter the modified email:"); String e = sc.nextLine(); System.out.print("Please input the modified home phone number:"); String phone = sc.nextLine(); address.update(name, adr, num, post, e, phone); } } else if ("c".equalsIgnoreCase(strFountion)) {// delete System.out.println("Please enter the name of the contact you want to delete?"); String name1 = sc.nextLine(); System.out.println("The name of the contact you are sure to delete is" + name1 + "Determined as Y,Indefinite N"); String name2 = sc.nextLine(); if ("y".equalsIgnoreCase(name2)) { if(address.selectName(name1) == null){ System.out.println("No one!!! Delete failed!!"); }else{ address.remove(name1); } } else if ("n".equalsIgnoreCase(name2)) { System.out.println("Exit delete"); break; } else { System.out.println("What you entered is not Y and N,Exit delete function"); break; } } else if ("d".equalsIgnoreCase(strFountion)) {// display address.show(); } else if ("e".equalsIgnoreCase(strFountion)) {// query System.out.println("Please enter the name you want to query"); String name = sc.nextLine(); ContactPerson contactPerson = address.selectName(name); if (contactPerson == null) { System.out.println("Query no person"); } else { System.out.println("Query with this person"); System.out.println(contactPerson); } } else if ("f".equalsIgnoreCase(strFountion)) {// Query according to mobile number System.out.println("Please input the mobile number you want to query"); String num = sc.nextLine(); ContactPerson contactPerson= address.selectNum(num); if (contactPerson != null) { System.out.println("Query with this person"); System.out.println(contactPerson); } else { System.out.println("Query no person"); } } else if ("g".equalsIgnoreCase(strFountion)) {//sort address.sort(); } else if("h".equalsIgnoreCase(strFountion)){ address.save(); System.out.println("Save successfully"); } else if ("i".equalsIgnoreCase(strFountion)){//Sign out System.out.println("Exit successfully"); break; }else {// delete System.out.println("You have entered the wrong option,Please re-enter"); } } sc.close(); } }