본문 바로가기

Java

[Java] GUI 회원관리 프로그램 만들기

 

자바를 이용한 회원관리 프로그램 만들기

 

5/28 ~ 6/3

 


 

데이터 베이스 테이블 생성

 

 


 

소스코드

 

MangementDTO.java

package dto;

import java.sql.Date;

public class ManagementDTO {
private int seq;
private String id;
private String password;
private String name;
private String gender;
private String tel;
private String email;
private Date createDate;


public void setSeq(int seq) {
    this.seq = seq;
}
public int getSeq() {
    return seq;
}
public String getId() {
    return id;
}
public void setId(String id) {
    this.id = id;
}
public String getPassword() {
    return password;
}
public void setPassword(String password) {
    this.password = password;
}
public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public String getGender() {
    return gender;
}
public void setGender(String gender) {
    this.gender = gender;
}
public String getTel() {
    return tel;
}
public void setTel(String tel) {
    this.tel = tel;
}
public String getEmail() {
    return email;
}
public void setEmail(String email) {
    this.email = email;
}
public Date getCreateDate() {
    return createDate;
}
public void setCreateDate(Date createDate) {
    this.createDate = createDate;
}
@Override
public String toString() {
    return "ManagementDTO [seq=" + seq + ", id=" + id + ", password=" + password + ", name=" + name + ", gender="
            + gender + ", tel=" + tel + ", email=" + email + ", createDate=" + createDate + "]";
   }
}

 

MangementDAO.java

package dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;

import dto.ManagementDTO;

public class ManagementDAO {

private Connection conn = null;
private PreparedStatement stmt = null;
private ResultSet rs = null;

private final String LOGIN = "select * from management where id = ? and password = ?";
private final String INSERT = "insert into management(seq,id,password,name,gender,tel,email,createdate) "
        + "values((select ifnull(max(seq),0)+1 from management as a),?,?,?,?,?,?,NOW())";
private final String LIST = "select * from management";


private ManagementDAO() {}
private static ManagementDAO instance = new ManagementDAO();

public static ManagementDAO getInstance() {
    return instance;
}

public int idPassword(String id, String password) {

    conn = DataBase.getConnection();

    try {
        stmt = conn.prepareStatement(LOGIN);

        stmt.setString(1, id);
        stmt.setString(2, password);

        rs = stmt.executeQuery();

        if(rs.next()) {
            return 1;
        }
    } catch(SQLException e) {
        e.printStackTrace();
    }

    return -1;
}

public int insertMember(ManagementDTO mdto) {

    conn = DataBase.getConnection();

    try {
        stmt = conn.prepareStatement(INSERT);

        stmt.setString(1, mdto.getId());
        stmt.setString(2, mdto.getPassword());
        stmt.setString(3, mdto.getName());
        stmt.setString(4, mdto.getGender());
        stmt.setString(5, mdto.getTel());
        stmt.setString(6, mdto.getEmail());
        stmt.executeUpdate();
        return 1;

    } catch(Exception e) {
        e.printStackTrace();
    }
    return -1;
}

public List<ManagementDTO> managementList() {
        conn = DataBase.getConnection();
    List<ManagementDTO> list = new ArrayList<ManagementDTO>();

    try {
        stmt = conn.prepareStatement(LIST);
        rs = stmt.executeQuery();
        while (rs.next()) {
            ManagementDTO mdto = new ManagementDTO();
            mdto.setSeq(rs.getInt("SEQ"));
            mdto.setId(rs.getString("ID"));    
            mdto.setPassword(rs.getString("PASSWORD"));
            mdto.setName(rs.getString("NAME"));
            mdto.setGender(rs.getString("GENDER"));
            mdto.setTel(rs.getString("TEL"));
            mdto.setEmail(rs.getString("EMAIL"));
            mdto.setCreateDate(rs.getDate("CREATEDATE"));
            list.add(mdto);

        }

        return list;

    } catch (Exception e) {
        e.printStackTrace();
    }

    return null;
  }
}

 

DataBase.java

    package dao;

    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.PreparedStatement;
    import java.sql.ResultSet;
    import java.sql.SQLException;

    public class DataBase {

    public static Connection dbConn;
    public static Connection getConnection() {

        Connection conn = null;

        try {
            String id = "root"; 
            String pw = "1234";
            String url = "jdbc:mysql://localhost:3306/management?characterEncoding=UTF-8&&serverTimezone=UTC";

            Class.forName("com.mysql.cj.jdbc.Driver");        
            conn = DriverManager.getConnection(url, id, pw);

            System.out.println("Database 연결 성공");

        } catch (Exception e) {
            System.out.println("Database 연결 실패");
            e.printStackTrace();
        }
        return conn;     
    }

    public static void close(PreparedStatement stmt, Connection conn) {
        if (stmt != null) {
            try {
                if (!stmt.isClosed())
                    stmt.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                stmt = null;
            }
        }

        if (conn != null) {
            try {
                if (!conn.isClosed())
                    conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                conn = null;
            }
        }
    }

    public static void close(ResultSet rs, PreparedStatement stmt, Connection conn) {
        if (rs != null) {
            try {
                if (!rs.isClosed())
                    rs.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                rs = null;
            }
        }

        if (stmt != null) {
            try {
                if (!stmt.isClosed())
                    stmt.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                stmt = null;
            }
        }
        if (conn != null) {
            try {
                if (!conn.isClosed())
                    conn.close();
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                conn = null;
            }
        }
    }

    public static void main(String[] args) {
        getConnection();
      }
    }

 

LoginFrame.java

   package gui;

   import java.awt.BorderLayout;
   import java.awt.Dimension;
   import java.awt.EventQueue;
   import java.awt.FlowLayout;
   import java.awt.Font;
   import java.awt.GridLayout;
   import java.awt.event.ActionEvent;
   import java.awt.event.ActionListener;

   import javax.swing.JButton;
   import javax.swing.JFrame;
   import javax.swing.JLabel;
   import javax.swing.JOptionPane;
   import javax.swing.JPanel;
   import javax.swing.JPasswordField;
   import javax.swing.JTextField;
   import javax.swing.SwingConstants;

   import dao.ManagementDAO;

    public class LoginFrame extends JFrame {

    private JPanel lPanel;
    private JLabel lLogin, lId, lPassword;
    private JTextField tId;
    private JPasswordField tPassword;
    private JButton login, register, exit;

    public LoginFrame() {
    super("로그인");
    super.setResizable(true);
    setSize(350, 400);
    setLocationRelativeTo(null);

    lPanel = new JPanel();
    lPanel.setLayout(new BorderLayout());
    setContentPane(lPanel); 

    lLogin = new JLabel("로그인");
    lLogin.setFont(new Font("Serif", Font.BOLD, 50));
    lLogin.setHorizontalAlignment(SwingConstants.CENTER);
    lLogin.setPreferredSize(new Dimension(120, 120));
    lPanel.add(lLogin, BorderLayout.NORTH);

    JPanel Main = new JPanel(new GridLayout(2, 2, 15, 15));

    lId = new JLabel("아이디");
    lId.setFont(new Font("Serif", Font.BOLD, 15));
    lId.setHorizontalAlignment(SwingConstants.CENTER);
    Main.add(lId);

    tId = new JTextField();
    tId.setColumns(10);
    Main.add(tId);

    lPassword = new JLabel("비밀번호");
    lPassword.setFont(new Font("Serif", Font.BOLD, 15));
    lPassword.setHorizontalAlignment(SwingConstants.CENTER);
    Main.add(lPassword);

    tPassword = new JPasswordField();
    tPassword.setColumns(10);
    Main.add(tPassword);

    lPanel.add(Main, BorderLayout.WEST);

    JPanel btnMain = new JPanel(new FlowLayout(FlowLayout.CENTER, 40, 60));

    login = new JButton("로그인");
    btnMain.add(login);
    register = new JButton("회원등록");
    btnMain.add(register);
    exit = new JButton("닫기");
    btnMain.add(exit);
    lPanel.add(btnMain, BorderLayout.SOUTH);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);

    login.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            String id = tId.getText();
            String password = tPassword.getText();

            ManagementDAO mdao = ManagementDAO.getInstance();

            int result = mdao.idPassword(id, password);
            if (result == 1) {
                JOptionPane.showMessageDialog(null, "로그인 완료");
                ListFrame lf = new ListFrame();
                dispose();

            } else {
                JOptionPane.showMessageDialog(null, "로그인 실패");
            }

        }
    });

    register.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            RegisterFrame rf = new RegisterFrame();

        }
    });

    exit.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {

            dispose();

        }
    });
}

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                LoginFrame lif = new LoginFrame();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

   }
}

 

RegisterFrame.java

package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JTextField;
import javax.swing.SwingConstants;

import dao.ManagementDAO;
import dto.ManagementDTO;

public class RegisterFrame extends JFrame {

private JPanel rPanel;
private JLabel rLabel;
private JLabel rId, rPassword, rName, rTel, rEmail;
private JTextField tId, tPassword, tName, tTel, tEmail;
private JRadioButton rMan, rWoman;
private JButton rButton;

public RegisterFrame() {

    super("회원등록");
    super.setResizable(true);
    setSize(350, 400);
    setLocationRelativeTo(null);

    rPanel = new JPanel();
    rPanel.setLayout(new BorderLayout());
    setContentPane(rPanel);

    rLabel = new JLabel("회원등록");
    rLabel.setFont(new Font("Serif", Font.BOLD, 40));
    rLabel.setHorizontalAlignment(SwingConstants.CENTER);
    rPanel.add(rLabel, BorderLayout.NORTH);

    JPanel main = new JPanel(new GridLayout(7, 2, 10, 10));

    rId = new JLabel("아이디");
    rId.setHorizontalAlignment(SwingConstants.CENTER);
    main.add(rId);

    tId = new JTextField();
    main.add(tId);

    rPassword = new JLabel("비밀번호");
    rPassword.setHorizontalAlignment(SwingConstants.CENTER);
    main.add(rPassword);

    tPassword = new JTextField();
    main.add(tPassword);

    rName = new JLabel("성명");
    rName.setHorizontalAlignment(SwingConstants.CENTER);
    main.add(rName);

    tName = new JTextField();
    main.add(tName);

    rTel = new JLabel("전화번호");
    rTel.setHorizontalAlignment(SwingConstants.CENTER);
    main.add(rTel);

    tTel = new JTextField();
    main.add(tTel);

    rEmail = new JLabel("이메일");
    rEmail.setHorizontalAlignment(SwingConstants.CENTER);
    main.add(rEmail);

    tEmail = new JTextField();
    main.add(tEmail);


    ButtonGroup bg = new ButtonGroup();

    rMan = new JRadioButton("남자");
    rMan.setHorizontalAlignment(SwingConstants.CENTER);
    bg.add(rMan);

    main.add(rMan);
    rWoman = new JRadioButton("여자");
    rWoman.setHorizontalAlignment(SwingConstants.CENTER);
    bg.add(rWoman);
    main.add(rWoman); 

    rPanel.add(main, BorderLayout.CENTER);

    JPanel sMain = new JPanel();
    rPanel.add(sMain, BorderLayout.EAST);

    rButton = new JButton("등록하기");
    rButton.setFont(new Font("Serif", Font.BOLD, 20));
    rPanel.add(rButton, BorderLayout.SOUTH);

    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    setVisible(true);

    rButton.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            ManagementDTO mdto = new ManagementDTO();
            mdto.setId(tId.getText());
            mdto.setPassword(tPassword.getText());
            mdto.setName(tName.getText());
            mdto.setTel(tTel.getText());
            mdto.setEmail(tEmail.getText());
            if(rMan.isSelected())  
                mdto.setGender(rMan.getText());
            else  
                mdto.setGender(rWoman.getText());


            ManagementDAO mdao = ManagementDAO.getInstance();
            int result = mdao.insertMember(mdto);

            if (result == 1) {
                JOptionPane.showMessageDialog(null, "회원등록 완료");
                dispose();
            } else {
                JOptionPane.showMessageDialog(null, "회원동록 실패");
                dispose();
            }
        }
    });

}

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                RegisterFrame rf = new RegisterFrame();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

   }
}

 

ListFrame.java

package gui;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.sql.Date;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.SwingConstants;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableModel;

import dao.ManagementDAO;
import dto.ManagementDTO;

public class ListFrame extends JFrame {

private JPanel panel;
private JTable table;
private JLabel lilabel;
private JButton logout;
private DefaultTableModel tModel;

public ListFrame() {
    super("List");
    super.setResizable(true);
    setSize(1300, 800);
    setLocationRelativeTo(null);
    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    panel = new JPanel(new BorderLayout());
    panel.setBorder(new EmptyBorder(5, 5, 5, 5));
    setContentPane(panel);

    lilabel = new JLabel("회원 리스트");
    lilabel.setFont(new Font("Serif", Font.BOLD, 50));
    lilabel.setHorizontalAlignment(SwingConstants.CENTER);
    lilabel.setPreferredSize(new Dimension(400, 80));
    panel.add(lilabel, BorderLayout.NORTH);

    ManagementDAO mdao = ManagementDAO.getInstance();
    List<ManagementDTO> list = mdao.managementList();

    String[] member = { "번호", "아이디", "비밀번호", "성명", "성별", "전화번호", "이메일", "생성일" };

    tModel = new DefaultTableModel(member, 0);

    for (int i = 0; i < list.size(); i++) {

        int seq = list.get(i).getSeq();
        String id1 = list.get(i).getId();
        String password = list.get(i).getPassword();
        String name = list.get(i).getName();
        String gender = list.get(i).getGender();
        String tel = list.get(i).getTel();
        String email = list.get(i).getEmail();
        Date createDate = list.get(i).getCreateDate();

        Object[] data = { seq, id1, password, name, gender, tel, email, createDate };

        tModel.addRow(data);

    }

    table = new JTable(tModel);
    table.setFont(new Font("돋움", Font.PLAIN, 20));
    table.setRowHeight(30);

    JScrollPane scrollPane = new JScrollPane(table);
    panel.add(scrollPane, BorderLayout.CENTER);

    logout = new JButton("로그아웃");
    panel.add(logout, BorderLayout.SOUTH);

    logout.addActionListener(new ActionListener() {

        @Override
        public void actionPerformed(ActionEvent e) {
            JOptionPane.showMessageDialog(null, "로그아웃 되었습니다.");
            dispose();
            new LoginFrame();
        }
    });

    setVisible(true);

}

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {
        public void run() {
            try {
                ListFrame lf = new ListFrame();

            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    });

  }
}