개발 등/중급

패턴 예제

darkhorizon 2008. 9. 27. 12:15
728x90
반응형
import java.sql.*;
import javax.sql.*;
 
public class DbConn {
    public static Connection getConnection() throws SQLException {
        Connection conn = null;
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
            String url = "jdbc:oracle:thin:@127.0.0.1:1521:ORCL";
            String id = "scott";
            String pass = "tiger";
            conn = DriverManager.getConnection(url, id, pass);
        } catch (SQLException sqle) {
            System.out.println(sqle);
        } catch (ClassNotFoundException cnfe) {
            System.out.println(cnfe);
        }
 
        return conn;
    }
}

import java.awt.*;
 
public class AddCommand implements Command {
    private Frame next;
 
    public AddCommand(Frame next) {
        this.next = next;
    }
 
    public Frame execute(Frame f) {
        AddForm addForm = (AddForm) f;
        MemberRepository mems = MemberRepository.getInstance();
        Member mem = new Member();
        mem.setId(addForm.tf1.getText());
        mem.setPw(addForm.tf2.getText());
        mem.setAddr(addForm.tf3.getText());
        mem.setTel(addForm.tf4.getText());
 
        mems.put(mem);
 
        return next;
    }
}


import java.awt.*;
import java.awt.event.*;
import java.util.*;
 
public class Manager {
    static HashMap commands;
    static {
        commands = new HashMap();
        commands.put("abort", new AbortCommand(new MenuForm()));
        commands.put("add", new NullCommand(new AddForm()));
        commands.put("do-add", new AddCommand(new MenuForm()));
    }
 
    public void service(String command, Frame f) {
        Command cmd = lookupCommand(command);
        Frame next = cmd.execute(f);
        next.setLocation(200, 200);
        next.pack();
        next.setVisible(true);
    }
 
    public Command lookupCommand(String cmd) {
        if (commands.containsKey(cmd))
            return (Command) commands.get(cmd);
        else
            return (Command) commands.get("add");
    }
}



public class Member {
    private String id;
    private String pw;
    private String addr;
    private String tel;
 
    public void setId(String id) {
        this.id = id;
    }
 
    public void setPw(String pw) {
        this.pw = pw;
    }
 
    public void setAddr(String addr) {
        this.addr = addr;
    }
 
    public void setTel(String tel) {
        this.tel = tel;
    }
 
    public String getId() {
        return id;
    }
 
    public String getPw() {
        return pw;
    }
 
    public String getAddr() {
        return addr;
    }
 
    public String getTel() {
        return tel;
    }
}


import java.util.*;
import java.sql.*;
import javax.sql.*;
import javax.naming.*;
 
public class MemberRepository {
    PreparedStatement putStmt;
    PreparedStatement updateStmt;
    PreparedStatement deleteStmt;
    PreparedStatement getStmt;
    PreparedStatement allStmt;
    Connection conn;
 
    private static MemberRepository instance;
 
    public static MemberRepository getInstance() {
        try {
            if (instance == null)
                instance = new MemberRepository();
        } catch (Exception e) {
            System.out.println(e);
        }
        return instance;
    }
 
    private MemberRepository() {
        try {
            String put = "insert into member values(?,?,?,?)";
            String update = "update member set pw=?, addr=?, tel=? where id=?";
            String delete = "delete from member where id=?";
            String get = "select * from member where id=?";
            String all = "select * from member";
 
            conn = DbConn.getConnection();
 
            putStmt = conn.prepareStatement(put);
            updateStmt = conn.prepareStatement(update);
            deleteStmt = conn.prepareStatement(delete);
            getStmt = conn.prepareStatement(get);
            allStmt = conn.prepareStatement(all);
        } catch (Exception e) {
            System.out.println(e);
        }
    }
 
    // insert메서드
    public void put(Member mem) {
        try {
            putStmt.setString(1, mem.getId());
            putStmt.setString(2, mem.getPw());
            putStmt.setString(3, mem.getAddr());
            putStmt.setString(4, mem.getTel());
            putStmt.executeUpdate();
        } catch (Exception e) {
            System.out.println(e);
        }
    }



import java.awt.*;
import java.awt.event.*;
 
public class MenuForm extends Frame implements ActionListener {
    Label la1;
    Button b1, b2, b3;
    Manager manager = new Manager();
 
    public MenuForm() {
        setLayout(new GridLayout(4, 1));
 
        la1 = new Label("회원관리");
        b1 = new Button("가입");
        b1.addActionListener(this);
        b2 = new Button("수정");
        b2.addActionListener(this);
        b3 = new Button("삭제");
        b3.addActionListener(this);
 
        add(la1);
        add(b1);
        add(b2);
        add(b3);
        addWindowListener(new WindowAdapter() {
            public void windowClosing(WindowEvent e) {
                System.exit(1);
            }
        });
    }
 
    public void actionPerformed(ActionEvent ae) {
        if (ae.getActionCommand().equals("가입"))
            manager.service("add", this);
        else if (ae.getActionCommand().equals("수정"))
            manager.service("update-menu", this);
        else
            manager.service("delete-menu", this);
 
        setVisible(false);
    }
 
    public static void main(String args[]) {
        MenuForm obj = new MenuForm();
        obj.setLocation(200, 200);
        obj.pack();
        obj.setVisible(true);
    }
}


import java.awt.*;
 
public class NullCommand implements Command {
    Frame next;
 
    public NullCommand(Frame next) {
        this.next = next;
    }
 
    public Frame execute(Frame f) {
        return next;
    }
}


728x90