Language/Java

PlayerView (미완)

원2 2021. 4. 20. 16:21
728x90
반응형

PlayerView

package com.view;

import java.awt.BorderLayout;

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;

import com.model.Player;
import com.model.PlayerDAO;
import com.model.PlayerDAOImpl;

import javax.swing.JSplitPane;
import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.SwingConstants;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.awt.event.ActionEvent;

public class PlayerView extends JFrame {

	private JPanel contentPane;
	private JSplitPane splitPane;
	private JPanel panel;
	private JTextField tfNum;
	private JLabel lblNewLabel;
	private JLabel lblNewLabel_1;
	private JTextField tfName;
	private JLabel lblNewLabel_1_1;
	private JLabel lblNewLabel_1_2;
	private JLabel lblNewLabel_1_3;
	private JLabel lblNewLabel_1_3_1;
	private JTextField tfBirth;
	private JTextField tfWeight;
	private JTextField tfHeight;
	private JComboBox cb;
	private JButton btnAdd;
	private JButton btnView;
	private JButton btnUpdate;
	private JButton btnDelete;
	private JSplitPane splitPane_1;
	private JScrollPane scrollPane;
	private JTextArea ta;
	private JPanel panel_1;
	private JComboBox comSearch;
	private JTextField tfSearch;
	private JButton btnSearch;

	PlayerDAOImpl dao = new PlayerDAOImpl();

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					PlayerView frame = new PlayerView();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public PlayerView() {

		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 700, 520);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		contentPane.add(getSplitPane(), BorderLayout.CENTER);
	}

	private JSplitPane getSplitPane() {
		if (splitPane == null) {
			splitPane = new JSplitPane();
			splitPane.setDividerSize(1);
			splitPane.setLeftComponent(getPanel());
			splitPane.setRightComponent(getSplitPane_1());
			splitPane.setDividerLocation(330);
		}
		return splitPane;
	}

	private JPanel getPanel() {
		if (panel == null) {
			panel = new JPanel();
			panel.setLayout(null);
			panel.add(getTfNum());
			panel.add(getLblNewLabel());
			panel.add(getLblNewLabel_1());
			panel.add(getTfName());
			panel.add(getLblNewLabel_1_1());
			panel.add(getLblNewLabel_1_2());
			panel.add(getLblNewLabel_1_3());
			panel.add(getLblNewLabel_1_3_1());
			panel.add(getTfBirth());
			panel.add(getTfWeight());
			panel.add(getTfHeight());
			panel.add(getCb());
			panel.add(getBtnAdd());
			panel.add(getBtnView());
			panel.add(getBtnUpdate());
			panel.add(getBtnDelete());
		}
		return panel;
	}

	private JTextField getTfNum() {
		if (tfNum == null) {
			tfNum = new JTextField();
			tfNum.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						int num = Integer.parseInt(tfNum.getText());
						Player p = dao.view(num);
						tfName.setText(p.getName());
						tfBirth.setText(p.getBirth());
						tfHeight.setText(p.getHeight() + "");
						tfWeight.setText(p.getWeight() + "");
						cb.setSelectedItem(p.getKind());
					} catch (NumberFormatException n) {
						JOptionPane.showMessageDialog(null, "숫자를 입력하시오");
					} catch (NullPointerException n) {
						JOptionPane.showMessageDialog(null, "해당 선수 없음");
					}
				}
			});
			tfNum.setBounds(78, 47, 180, 21);
			tfNum.setColumns(10);
		}
		return tfNum;
	}

	private JLabel getLblNewLabel() {
		if (lblNewLabel == null) {
			lblNewLabel = new JLabel("\uBC88\uD638");
			lblNewLabel.setHorizontalAlignment(SwingConstants.RIGHT);
			lblNewLabel.setHorizontalTextPosition(SwingConstants.CENTER);
			lblNewLabel.setBounds(12, 50, 57, 15);
		}
		return lblNewLabel;
	}

	private JLabel getLblNewLabel_1() {
		if (lblNewLabel_1 == null) {
			lblNewLabel_1 = new JLabel("\uC774\uB984");
			lblNewLabel_1.setHorizontalAlignment(SwingConstants.RIGHT);
			lblNewLabel_1.setHorizontalTextPosition(SwingConstants.CENTER);
			lblNewLabel_1.setBounds(12, 96, 57, 15);
		}
		return lblNewLabel_1;
	}

	private JTextField getTfName() {
		if (tfName == null) {
			tfName = new JTextField();
			tfName.setColumns(10);
			tfName.setBounds(78, 93, 180, 21);
		}
		return tfName;
	}

	private JLabel getLblNewLabel_1_1() {
		if (lblNewLabel_1_1 == null) {
			lblNewLabel_1_1 = new JLabel("\uC0DD\uC77C");
			lblNewLabel_1_1.setHorizontalAlignment(SwingConstants.RIGHT);
			lblNewLabel_1_1.setHorizontalTextPosition(SwingConstants.CENTER);
			lblNewLabel_1_1.setBounds(12, 147, 57, 15);
		}
		return lblNewLabel_1_1;
	}

	private JLabel getLblNewLabel_1_2() {
		if (lblNewLabel_1_2 == null) {
			lblNewLabel_1_2 = new JLabel("\uBAB8\uBB34\uAC8C");
			lblNewLabel_1_2.setHorizontalAlignment(SwingConstants.RIGHT);
			lblNewLabel_1_2.setHorizontalTextPosition(SwingConstants.CENTER);
			lblNewLabel_1_2.setBounds(12, 210, 57, 15);
		}
		return lblNewLabel_1_2;
	}

	private JLabel getLblNewLabel_1_3() {
		if (lblNewLabel_1_3 == null) {
			lblNewLabel_1_3 = new JLabel("\uD0A4");
			lblNewLabel_1_3.setHorizontalAlignment(SwingConstants.RIGHT);
			lblNewLabel_1_3.setHorizontalTextPosition(SwingConstants.CENTER);
			lblNewLabel_1_3.setBounds(12, 277, 57, 15);
		}
		return lblNewLabel_1_3;
	}

	private JLabel getLblNewLabel_1_3_1() {
		if (lblNewLabel_1_3_1 == null) {
			lblNewLabel_1_3_1 = new JLabel("\uC885\uBAA9");
			lblNewLabel_1_3_1.setHorizontalAlignment(SwingConstants.RIGHT);
			lblNewLabel_1_3_1.setHorizontalTextPosition(SwingConstants.CENTER);
			lblNewLabel_1_3_1.setBounds(12, 331, 57, 15);
		}
		return lblNewLabel_1_3_1;
	}

	private JTextField getTfBirth() {
		if (tfBirth == null) {
			tfBirth = new JTextField();
			tfBirth.setColumns(10);
			tfBirth.setBounds(78, 144, 180, 21);
		}
		return tfBirth;
	}

	private JTextField getTfWeight() {
		if (tfWeight == null) {
			tfWeight = new JTextField();
			tfWeight.setColumns(10);
			tfWeight.setBounds(78, 207, 180, 21);
		}
		return tfWeight;
	}

	private JTextField getTfHeight() {
		if (tfHeight == null) {
			tfHeight = new JTextField();
			tfHeight.setColumns(10);
			tfHeight.setBounds(78, 274, 180, 21);
		}
		return tfHeight;
	}

	private JComboBox getCb() {
		if (cb == null) {
			cb = new JComboBox();
			cb.setModel(new DefaultComboBoxModel(new String[] { "야구", "축구", "기타" }));
			cb.setBounds(78, 327, 116, 23);
		}
		return cb;
	}

	private JButton getBtnAdd() {
		if (btnAdd == null) {
			btnAdd = new JButton("추가");
			btnAdd.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Player p = new Player();
					p.setBirth(tfBirth.getText());
					p.setHeight(Double.parseDouble(tfHeight.getText()));
					p.setWeight(Double.parseDouble(tfWeight.getText()));
					p.setKind((String) cb.getSelectedItem());
					p.setName(tfName.getText());

					dao.insert(p);
					btnView.doClick();
				}
			});
			btnAdd.setBounds(46, 379, 97, 23);
		}
		return btnAdd;
	}

	private JButton getBtnView() {
		if (btnView == null) {
			btnView = new JButton("보기");
			btnView.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					ta.setText("");

					ArrayList<Player> arr = dao.selectAll();
					for (Player p : arr) {
						ta.append("번호 : " + p.getNum() + "\n");
						ta.append("이름 : " + p.getName() + "\n");
						ta.append("생일 : " + p.getBirth() + "\n");
						ta.append("키 : " + p.getHeight() + "\n");
						ta.append("몸무게 : " + p.getWeight() + "\n");
						ta.append("종목 : " + p.getKind() + "\n\n");
					}
				}
			});
			btnView.setBounds(155, 379, 97, 23);
		}
		return btnView;
	}

	private JButton getBtnUpdate() {
		if (btnUpdate == null) {
			btnUpdate = new JButton("수정");
			btnUpdate.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					Player p = new Player();
					p.setBirth(tfBirth.getText());
					p.setHeight(Double.parseDouble(tfHeight.getText()));
					p.setWeight(Double.parseDouble(tfWeight.getText()));
					p.setKind((String) cb.getSelectedItem());
					p.setName(tfName.getText());
					p.setNum(Integer.parseInt(tfNum.getText()));
					
					dao.update(p);
					
					btnView.doClick();
				}
			});
			btnUpdate.setBounds(46, 412, 97, 23);
		}
		return btnUpdate;
	}

	private JButton getBtnDelete() {
		if (btnDelete == null) {
			btnDelete = new JButton("삭제");
			btnDelete.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					try {
						int num = Integer.parseInt(tfNum.getText());
						if(JOptionPane.showConfirmDialog(null, "정말 삭제할까요","삭제",JOptionPane.YES_NO_OPTION) == JOptionPane.NO_OPTION) return;
						dao.delete(num);
						btnView.doClick();
					} catch (NumberFormatException n) {
						JOptionPane.showMessageDialog(null, "숫자를 입력하시오");
					}
				}
			});
			btnDelete.setBounds(155, 412, 97, 23);
		}
		return btnDelete;
	}

	private JSplitPane getSplitPane_1() {
		if (splitPane_1 == null) {
			splitPane_1 = new JSplitPane();
			splitPane_1.setOrientation(JSplitPane.VERTICAL_SPLIT);
			splitPane_1.setLeftComponent(getScrollPane());
			splitPane_1.setRightComponent(getPanel_1());
			splitPane_1.setDividerLocation(300);
		}
		return splitPane_1;
	}

	private JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getTa());
		}
		return scrollPane;
	}

	private JTextArea getTa() {
		if (ta == null) {
			ta = new JTextArea();
		}
		return ta;
	}

	private JPanel getPanel_1() {
		if (panel_1 == null) {
			panel_1 = new JPanel();
			panel_1.setLayout(null);
			panel_1.add(getComSearch());
			panel_1.add(getTfSearch());
			panel_1.add(getBtnSearch());
		}
		return panel_1;
	}

	private JComboBox getComSearch() {
		if (comSearch == null) {
			comSearch = new JComboBox();
			comSearch.setModel(new DefaultComboBoxModel(new String[] { "이름", "종목" }));
			comSearch.setBounds(12, 10, 81, 23);
		}
		return comSearch;
	}

	private JTextField getTfSearch() {
		if (tfSearch == null) {
			tfSearch = new JTextField();
			tfSearch.setBounds(105, 11, 116, 21);
			tfSearch.setColumns(10);
		}
		return tfSearch;
	}

	private JButton getBtnSearch() {
		if (btnSearch == null) {
			btnSearch = new JButton("검색");
			btnSearch.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					String field = "";
					if(comSearch.getSelectedIndex()==0) {
						field = "name";
					}else if (comSearch.getSelectedIndex()==1) {
						field = "kind";
					}
					String word = tfSearch.getText();
					if(word.isEmpty()) {
						JOptionPane.showMessageDialog(null, "검색어를 입력하세요");
					}
					ArrayList<Player>sarr = dao.search(field, word);
					ta.setText("");	 // 검색한 자리를 지워줌
					for(Player p : sarr) {
						ta.append("이름:" + p.getName()+"\n");
						ta.append("키:" + p.getHeight()+"\n");
						ta.append("몸무게:" + p.getWeight()+"\n");
						ta.append("종목:" + p.getKind()+"\n");
					}
					
				}
			});
			btnSearch.setBounds(231, 10, 97, 23);
		}
		return btnSearch;
	}
}

PlayerDAO

package com.model;

import java.util.ArrayList;

public interface PlayerDAO {
	// 추가
	public void insert(Player p);

	// 전체보기
	public ArrayList<Player> selectAll();

	// 수정
	public void update(Player p);

	// 삭제
	public void delete(int num);

	// 검색
	public ArrayList<Player> search(String field, String word);

	// 상세보기
	public Player view(int num);
}

Player

package com.model;

public class Player {
	private int num;
	private String name;
	private String birth;
	private double weight;
	private double height;
	private String kind;

	public int getNum() {
		return num;
	}

	public void setNum(int num) {
		this.num = num;
	}

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getBirth() {
		return birth;
	}

	public void setBirth(String birth) {
		this.birth = birth;
	}

	public double getWeight() {
		return weight;
	}

	public void setWeight(double weight) {
		this.weight = weight;
	}

	public double getHeight() {
		return height;
	}

	public void setHeight(double height) {
		this.height = height;
	}

	public String getKind() {
		return kind;
	}

	public void setKind(String kind) {
		this.kind = kind;
	}

}

PlayerDAOImpl

package com.model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.naming.spi.DirStateFactory.Result;

public class PlayerDAOImpl implements PlayerDAO {

	Connection con = null;
	PreparedStatement ps = null;
	Statement st = null;
	ResultSet rs = null;

	String url;
	String user;
	String passwd;

	// 생성자
	public PlayerDAOImpl() {

		try {

			Class.forName("oracle.jdbc.driver.OracleDriver");
			url = "jdbc:oracle:thin:@localhost:1521:xe";
			user = "scott";
			passwd = "tiger";

		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	@Override
	public void insert(Player p) {
		try {

			con = DriverManager.getConnection(url, user, passwd);
			String sql = "insert into player values (player_seq.nextval,? ,? ,? ,? ,?)";
			ps = con.prepareStatement(sql);

			ps.setString(1, p.getName());
			ps.setString(2, p.getBirth());
			ps.setDouble(3, p.getHeight());
			ps.setDouble(4, p.getWeight());
			ps.setString(5, p.getKind());

			ps.executeUpdate();

		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			closeConnection(con, ps, null, null);
		}
	}

	@Override
	public ArrayList<Player> selectAll() {
		// TODO Auto-generated method stub
		Connection con = null;
		Statement st = null;
		ResultSet rs = null;
		ArrayList<Player> arr = new ArrayList<>();

		try {
			con = DriverManager.getConnection(url, user, passwd);
			String sql = "select * from player";
			st = con.createStatement();
			rs = st.executeQuery(sql);

			while (rs.next()) {
				Player p = new Player();
				p.setBirth(rs.getString("birth"));
				p.setHeight(rs.getDouble("height"));
				p.setKind(rs.getString("kind"));
				p.setName(rs.getString("name"));
				p.setNum(rs.getInt("num"));
				p.setWeight(rs.getDouble("weight"));

				arr.add(p);
			}

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeConnection(con, null, st, rs);
		}

		return arr;
	}

	@Override
	public void update(Player p) {
		Connection con = null;
		PreparedStatement ps = null;

		try {
			con = DriverManager.getConnection(url, user, passwd);
			String sql = "update player set name=?,birth=?,height=?,weight=?,kind=? where num=?";
			ps = con.prepareStatement(sql);

			ps.setString(1, p.getName());
			ps.setString(2, p.getBirth());
			ps.setDouble(3, p.getHeight());
			ps.setDouble(4, p.getWeight());
			ps.setString(5, p.getKind());
			ps.setInt(6, p.getNum());

			ps.execute();

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			closeConnection(con, ps, null, null);
		}
	}

	@Override
	public void delete(int num) {
		Connection con = null;
		Statement st = null;

		try {
			con = DriverManager.getConnection(url, user, passwd);
			String sql = "delete from player where num = " + num;
			st = con.createStatement();
			st.executeUpdate(sql);

		} catch (Exception e) {

		} finally {
			closeConnection(con, ps, null, null);
		}
	}

	@Override
	public ArrayList<Player> search(String field, String word) { // word는 변수라서 입력값
		Connection con = null;
		Statement st = null;
		ResultSet rs = null;
		ArrayList<Player> arr = new ArrayList<>();
		try {
			con = DriverManager.getConnection(url, user, passwd);
			String sql = "select * from player where"+" kind like '%"+word+"%'";
			st = con.createStatement();
			rs = st.executeQuery(sql);
			while (rs.next()) {
				Player p = new Player();
				p.setBirth(rs.getString("birth"));
				p.setHeight(rs.getDouble("height"));
				p.setKind(rs.getString("kind"));
				p.setName(rs.getString("name"));
				p.setNum(rs.getInt("num"));
				p.setWeight(rs.getDouble("weight"));
				arr.add(p);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}

		return arr;
	}

	@Override
	public Player view(int num) {
		Connection con = null;
		Statement st = null;
		ResultSet rs = null;
		Player p = null;
		try {
			con = DriverManager.getConnection(url, user, passwd);
			st = con.createStatement();
			String sql = "select * from player where num = " + num;
			rs = st.executeQuery(sql);
			if (rs.next()) {
				p = new Player();

				p.setBirth(rs.getString("birth"));
				p.setHeight(rs.getDouble("height"));
				p.setKind(rs.getString("kind"));
				p.setName(rs.getString("name"));
				p.setNum(rs.getInt("num"));
				p.setWeight(rs.getDouble("weight"));

			}

		} catch (SQLException e) {
			e.printStackTrace();
		} finally {
			closeConnection(con, null, st, rs);
		}

		return p;
	}

	private void closeConnection(Connection con, PreparedStatement ps, Statement st, ResultSet rs) {
		try {
			if (rs != null)
				rs.close();
			if (ps != null)
				ps.close();
			if (st != null)
				st.close();
			if (con != null)
				con.close();

		} catch (Exception e) {

		}
	}

}
728x90
반응형

'Language > Java' 카테고리의 다른 글

HashMap을 이용한 수도 퀴즈 게임  (0) 2021.04.29
CalculatorTest  (0) 2021.04.21
플레이어 미완++  (0) 2021.04.20
Swing 치트키  (0) 2021.04.20
pannelTest1,2,3  (0) 2021.04.19
Bank  (0) 2021.04.19