Advertisement
Guest User

PhoneBookModel

a guest
Nov 30th, 2018
133
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.90 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.File;
  3. import java.io.FileOutputStream;
  4. import java.io.FileReader;
  5. import java.io.IOException;
  6. import java.io.PrintStream;
  7. import java.util.Enumeration;
  8. import java.util.Hashtable;
  9. import java.util.Set;
  10. import java.io.*;
  11. import java.sql.Connection;
  12. import java.sql.DriverManager;
  13. import java.sql.ResultSet;
  14. import java.sql.SQLException;
  15. import java.sql.Statement;
  16. import net.ucanaccess.jdbc.JackcessOpenerInterface;
  17. /*
  18.  * Model data for the phone book application.  
  19.  */
  20. abstract public class PhoneBookModel {
  21.     protected PhoneBookView phonebookview;
  22.  
  23.     // The following are various states captured by the model
  24.     public static String ADD_NAME_STATE = "ADD_NAME";
  25.     public static String ADD_NUMBER_STATE = "ADD_NUMBER";
  26.     public static String SEARCH_STATE = "SEARCH";
  27.     public static String IDLE_STATE = "IDLE";
  28.     public static String SEARCH_RESULT_STATE = "SEARCH_RESULT";
  29.     public static String ERROR_STATE = "ERROR";
  30.     public static String EXIT_STATE = "EXIT";
  31.     public static String MODIFY_SELECT_NAME_STATE = "SELECT_NAME";
  32.     public static String MODIFY_NUMBER_STATE = "MODIFY_NUMBER";
  33.     public static String DELETE_STATE = "DELETE";
  34.    
  35.    
  36.     // Private fields used to track various model data
  37.     protected String state = IDLE_STATE;
  38.     protected String searchResult = null;
  39.     protected Hashtable<String,String> phoneBook = null;
  40.     /**
  41.      * set the state
  42.      * @param aState
  43.      */
  44.     public void setState(String aState) {
  45.         state = aState;
  46.         phonebookview.stateHasChanged(this, state);
  47.     }
  48.     /**
  49.      * add a phone entry
  50.      * @param name
  51.      * @param number
  52.      */
  53.     public void addAnEntry(String name, String number) {
  54.         phoneBook.put(name, number);
  55.     }
  56.     /**
  57.      * search the phone number and set the searchResult field
  58.      * @param name
  59.      */
  60.     public void searchPhoneNumber(String name) {
  61.         searchResult = (String) phoneBook.get(name);
  62.     }
  63.     /**
  64.      * return the search result
  65.      */
  66.     public String getSearchResult() {
  67.         return searchResult;
  68.     }
  69.     public void modifyAnEntry(String name, String number) {
  70.         phoneBook.put(name,number);
  71.     }
  72.    
  73.     public void deleteAnEntry(String name) {
  74.         phoneBook.remove(name);
  75.     }
  76.     /**
  77.      * get the state
  78.      */
  79.     public String getState() {
  80.         return state;
  81.     }
  82.  
  83.     /**
  84.      * constructor
  85.      * @param view
  86.      * @return
  87.      */
  88.     public PhoneBookModel(PhoneBookView view) {
  89.         phonebookview = view;
  90.         phoneBook = new Hashtable<String,String>();
  91.     }
  92.     protected abstract void restoreAllEntry();
  93.     protected abstract void saveAllEntry();
  94. }
  95.  
  96. class TextDataBase extends PhoneBookModel {
  97.  
  98.     public TextDataBase(PhoneBookView view) {
  99.         super(view);
  100.         // TODO Auto-generated constructor stub
  101.     }
  102.     public void saveAllEntry(){
  103.        
  104.  
  105.         try {
  106.             FileWriter fw = null;
  107.             BufferedWriter bw = null;
  108.             PrintWriter out = null;
  109.             fw = new FileWriter("fileText.txt", true);
  110.             bw = new BufferedWriter(fw);
  111.             out = new PrintWriter(bw);
  112.             Set<String> keys = phoneBook.keySet();
  113.             for(String key: keys){
  114.                out.println(key+" "+phoneBook.get(key));
  115.             }          
  116.             out.close();
  117.         } catch (IOException e) {}
  118.     }
  119.     public void restoreAllEntry(){
  120.         try{
  121.             File file = new File("fileText.txt");
  122.             FileReader fileReader = new FileReader(file);
  123.             BufferedReader bufferedReader = new BufferedReader(fileReader);
  124.             String line;
  125.             while ((line = bufferedReader.readLine()) != null) {
  126.                 String[] tokens = line.split(" ");
  127.                 phoneBook.put(tokens[0], tokens[1]);
  128.             }
  129.             fileReader.close();      
  130.         }catch(Exception e){}
  131.     }  
  132. }
  133. class AccessDataBase extends PhoneBookModel {
  134.  
  135.     public AccessDataBase(PhoneBookView view) {
  136.         super(view);
  137.         // TODO Auto-generated constructor stub
  138.     }
  139.     public void saveAllEntry(){
  140.         Connection connDB = null;
  141.         try {  
  142.             //建立驅動程式,連結odbc至Microsoft Access
  143.             Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
  144.             //下列字串的://之後要加上access檔案存放的地方
  145.             String dataSource = "jdbc:ucanaccess://Database1.accdb";
  146.             //連結資料庫
  147.             connDB = DriverManager.getConnection(dataSource);          
  148.             //SQL共有 INSERT、SELECT、UPDATE、DELETE,以下分別列舉
  149.             Statement st = connDB.createStatement();
  150.             //全選
  151.             st.execute("SELECT * FROM phoneBook");
  152.             ResultSet rs = st.getResultSet();
  153.             //delete all
  154.             while(rs.next())
  155.             {              
  156.                 st.executeUpdate("DELETE FROM phoneBook where contactName='"+rs.getString("contactName")+"'");
  157.             }
  158. //          insert all
  159.             Set<String> keys = phoneBook.keySet();
  160.             for(String key: keys){
  161.                 String phone=phoneBook.get(key);
  162.                 st.executeUpdate("INSERT INTO phoneBook (contactName, phoneNumber) VALUES ('"+key+"','"+phone+"')");
  163.                 //System.out.println(key+" "+phoneBook.get(key));
  164.             }
  165.             st.close();
  166.             connDB.close();
  167.         } catch (ClassNotFoundException e1) {
  168.             // TODO Auto-generated catch block
  169.             e1.printStackTrace();
  170.         } catch (SQLException e) {
  171.             // TODO Auto-generated catch block
  172.             e.printStackTrace();
  173.         }
  174.     }
  175.     public void restoreAllEntry(){
  176.         Connection connDB = null;
  177.         try {  
  178.             //建立驅動程式,連結odbc至Microsoft Access
  179.             Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
  180.             //下列字串的://之後要加上access檔案存放的地方
  181.             String dataSource = "jdbc:ucanaccess://Database1.accdb";
  182.             //連結資料庫
  183.             connDB = DriverManager.getConnection(dataSource);          
  184.             //SQL共有 INSERT、SELECT、UPDATE、DELETE,以下分別列舉
  185.             Statement st = connDB.createStatement();
  186.             //全選
  187.             st.execute("SELECT * FROM phoneBook");
  188.             ResultSet rs = st.getResultSet();
  189.             //放到hashTable
  190.             while(rs.next())
  191.             {
  192.                 phoneBook.put(rs.getString("contactName"), rs.getString("phoneNumber"));
  193.             }      
  194.             st.close();
  195.             connDB.close();
  196.         } catch (ClassNotFoundException e1) {
  197.             // TODO Auto-generated catch block
  198.             e1.printStackTrace();
  199.         } catch (SQLException e) {
  200.             // TODO Auto-generated catch block
  201.             e.printStackTrace();
  202.         }
  203.     }  
  204. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement