Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.BufferedReader;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.FileReader;
- import java.io.IOException;
- import java.io.PrintStream;
- import java.util.Enumeration;
- import java.util.Hashtable;
- import java.util.Set;
- import java.io.*;
- import java.sql.Connection;
- import java.sql.DriverManager;
- import java.sql.ResultSet;
- import java.sql.SQLException;
- import java.sql.Statement;
- import net.ucanaccess.jdbc.JackcessOpenerInterface;
- /*
- * Model data for the phone book application.
- */
- abstract public class PhoneBookModel {
- protected PhoneBookView phonebookview;
- // The following are various states captured by the model
- public static String ADD_NAME_STATE = "ADD_NAME";
- public static String ADD_NUMBER_STATE = "ADD_NUMBER";
- public static String SEARCH_STATE = "SEARCH";
- public static String IDLE_STATE = "IDLE";
- public static String SEARCH_RESULT_STATE = "SEARCH_RESULT";
- public static String ERROR_STATE = "ERROR";
- public static String EXIT_STATE = "EXIT";
- public static String MODIFY_SELECT_NAME_STATE = "SELECT_NAME";
- public static String MODIFY_NUMBER_STATE = "MODIFY_NUMBER";
- public static String DELETE_STATE = "DELETE";
- // Private fields used to track various model data
- protected String state = IDLE_STATE;
- protected String searchResult = null;
- protected Hashtable<String,String> phoneBook = null;
- /**
- * set the state
- * @param aState
- */
- public void setState(String aState) {
- state = aState;
- phonebookview.stateHasChanged(this, state);
- }
- /**
- * add a phone entry
- * @param name
- * @param number
- */
- public void addAnEntry(String name, String number) {
- phoneBook.put(name, number);
- }
- /**
- * search the phone number and set the searchResult field
- * @param name
- */
- public void searchPhoneNumber(String name) {
- searchResult = (String) phoneBook.get(name);
- }
- /**
- * return the search result
- */
- public String getSearchResult() {
- return searchResult;
- }
- public void modifyAnEntry(String name, String number) {
- phoneBook.put(name,number);
- }
- public void deleteAnEntry(String name) {
- phoneBook.remove(name);
- }
- /**
- * get the state
- */
- public String getState() {
- return state;
- }
- /**
- * constructor
- * @param view
- * @return
- */
- public PhoneBookModel(PhoneBookView view) {
- phonebookview = view;
- phoneBook = new Hashtable<String,String>();
- }
- protected abstract void restoreAllEntry();
- protected abstract void saveAllEntry();
- }
- class TextDataBase extends PhoneBookModel {
- public TextDataBase(PhoneBookView view) {
- super(view);
- // TODO Auto-generated constructor stub
- }
- public void saveAllEntry(){
- try {
- FileWriter fw = null;
- BufferedWriter bw = null;
- PrintWriter out = null;
- fw = new FileWriter("fileText.txt", true);
- bw = new BufferedWriter(fw);
- out = new PrintWriter(bw);
- Set<String> keys = phoneBook.keySet();
- for(String key: keys){
- out.println(key+" "+phoneBook.get(key));
- }
- out.close();
- } catch (IOException e) {}
- }
- public void restoreAllEntry(){
- try{
- File file = new File("fileText.txt");
- FileReader fileReader = new FileReader(file);
- BufferedReader bufferedReader = new BufferedReader(fileReader);
- String line;
- while ((line = bufferedReader.readLine()) != null) {
- String[] tokens = line.split(" ");
- phoneBook.put(tokens[0], tokens[1]);
- }
- fileReader.close();
- }catch(Exception e){}
- }
- }
- class AccessDataBase extends PhoneBookModel {
- public AccessDataBase(PhoneBookView view) {
- super(view);
- // TODO Auto-generated constructor stub
- }
- public void saveAllEntry(){
- Connection connDB = null;
- try {
- //建立驅動程式,連結odbc至Microsoft Access
- Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
- //下列字串的://之後要加上access檔案存放的地方
- String dataSource = "jdbc:ucanaccess://Database1.accdb";
- //連結資料庫
- connDB = DriverManager.getConnection(dataSource);
- //SQL共有 INSERT、SELECT、UPDATE、DELETE,以下分別列舉
- Statement st = connDB.createStatement();
- //全選
- st.execute("SELECT * FROM phoneBook");
- ResultSet rs = st.getResultSet();
- //delete all
- while(rs.next())
- {
- st.executeUpdate("DELETE FROM phoneBook where contactName='"+rs.getString("contactName")+"'");
- }
- // insert all
- Set<String> keys = phoneBook.keySet();
- for(String key: keys){
- String phone=phoneBook.get(key);
- st.executeUpdate("INSERT INTO phoneBook (contactName, phoneNumber) VALUES ('"+key+"','"+phone+"')");
- //System.out.println(key+" "+phoneBook.get(key));
- }
- st.close();
- connDB.close();
- } catch (ClassNotFoundException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void restoreAllEntry(){
- Connection connDB = null;
- try {
- //建立驅動程式,連結odbc至Microsoft Access
- Class.forName("net.ucanaccess.jdbc.UcanaccessDriver");
- //下列字串的://之後要加上access檔案存放的地方
- String dataSource = "jdbc:ucanaccess://Database1.accdb";
- //連結資料庫
- connDB = DriverManager.getConnection(dataSource);
- //SQL共有 INSERT、SELECT、UPDATE、DELETE,以下分別列舉
- Statement st = connDB.createStatement();
- //全選
- st.execute("SELECT * FROM phoneBook");
- ResultSet rs = st.getResultSet();
- //放到hashTable
- while(rs.next())
- {
- phoneBook.put(rs.getString("contactName"), rs.getString("phoneNumber"));
- }
- st.close();
- connDB.close();
- } catch (ClassNotFoundException e1) {
- // TODO Auto-generated catch block
- e1.printStackTrace();
- } catch (SQLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement