1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
|
package swingTest;
//선수 정보 컬럼명 저장.
public class Player {
private int num;
private String name;
private String birth;
private double height;
private double weight;
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 getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public double getWeight() {
return weight;
}
public void setWeight(double weight) {
this.weight = weight;
}
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
}
|
cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
|
package swingTest;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
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.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
public class PlayerSwing2 extends JFrame {
JTextField []tf=new JTextField[6];
PlayerDBA dao=new PlayerDBA();
public PlayerSwing2() {
setTitle("Player Test22");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setLayout(new GridLayout(2,2));
add(new PanelPane()); //1행 1열
JTextArea ta=new JTextArea(); //1행 2열
JScrollPane jsp=new JScrollPane(ta);
add(jsp);
ta.addMouseListener(new MouseAdapter() { //번호 선택시 상세보기
public void mouseReleased(MouseEvent e) {
int num = Integer.parseInt(ta.getSelectedText().trim());
Player p=dao.playerDetail(num); //rs가 아닌 객체에 담아서 값을 돌림
tf[0].setText(num+"");
tf[1].setText(p.getName());
tf[2].setText(p.getBirth());
tf[3].setText(p.getHeight()+"");
tf[4].setText(p.getWeight()+"");
tf[5].setText(p.getKind());
}
});
JPanel p1=new JPanel(); //2행 1열
JButton insertBtn=new JButton("추가");
JButton viewBtn=new JButton("보기");
JButton updateBtn=new JButton("수정");
JButton deleteBtn=new JButton("삭제");
p1.add(insertBtn); p1.add(viewBtn); p1.add(updateBtn); p1.add(deleteBtn);
add(p1);
//추가
insertBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Player p=new Player();
p.setName(tf[1].getText());
p.setBirth(tf[2].getText());
p.setHeight(Double.parseDouble(tf[3].getText()));
p.setWeight(Double.parseDouble(tf[4].getText()));
p.setKind(tf[5].getText());
dao.playerInsert(p);
viewBtn.doClick();
}
});
//보기
viewBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta.setText("");
ArrayList<Player> arr=dao.playerView();
for(Player p:arr) { //ArrayList돌면서 ta에 정보 붙여넣기
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");
ta.append("\n");
}
}
});
//수정 (ta에서 번호를 선택한 후 tf창에 뜨면 수정)
updateBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
Player p= new Player();
p.setNum(Integer.parseInt(tf[0].getText()));
p.setName(tf[1].getText());
p.setBirth(tf[2].getText());
p.setHeight(Double.parseDouble(tf[3].getText()));
p.setWeight(Double.parseDouble(tf[4].getText()));
p.setKind(tf[5].getText());
dao.playerUpdate(p);
viewBtn.doClick();
clearText();
}
});
//삭제
deleteBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
int num=Integer.parseInt(tf[0].getText()); //tf[0]에 들어있는 글자(숫자)
dao.playerDelete(num);
viewBtn.doClick(); //보기버튼 클릭
clearText(); //내용지우기
}
});
JPanel p2=new JPanel(); //2행 2열
JComboBox<String>jcb=new JComboBox<String>();
jcb.addItem("이름");
jcb.addItem("종목");
JTextField searchtf=new JTextField(10);
JButton searchBtn=new JButton("검색");
p2.add(jcb); p2.add(searchtf); p2.add(searchBtn);
add(p2);
//검색버튼 (이름, 종목으로 검색)
searchBtn.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
ta.setText("");
int idx=jcb.getSelectedIndex(); //콤보박스에서 선택한 값 (이름, 종목 인덱스 0,1)
String key="";
if(idx==0) {
key="name";
}else if(idx==1) {
key="kind";
}
ArrayList<Player>arr=dao.playerSearch(key, searchtf.getText());
for(Player p:arr) { //ArrayList돌면서 ta에 정보 붙여넣기
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");
}
}
});
setSize(600,400);
setVisible(true);
}
class PanelPane extends JPanel {
private String[] text= {"번호","이름","생일","키","몸무게","종목"};
public PanelPane() {
setLayout(null); //좌표를 내가 정하겠다.
for(int i=0;i<text.length;i++) {
JLabel la=new JLabel(text[i]);
la.setHorizontalAlignment(JLabel.RIGHT);
la.setSize(50,20);
la.setLocation(30,50+i*20); //y좌표를 늘여야지 글자가 겹치지 않음
add(la);
tf[i]=new JTextField(50);
tf[i].setHorizontalAlignment(JTextField.CENTER);
tf[i].setSize(150,20);
tf[i].setLocation(120, 50+i*20);
add(tf[i]);
}
tf[0].setEditable(false);
}
}
private void clearText() {
for(int i=0;i<tf.length;i++) {
tf[i].setText("");
}
}
public static void main(String[] args) {
new PlayerSwing2();
}
}
|
cs |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
package swingTest;
//db와 연결
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;
public class PlayerDBA {
String url, user, pwd;
//db연결: 생성자가 해줌. url,user,pwd는 앞으로 계속 쓸거라서 전역변수로 빼줌.
public PlayerDBA() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
url="jdbc:oracle:thin:@localhost:1521:xe"; //1521:오라클이 깔린 포트 번호
user="scott";
pwd="1234";
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
}
//추가
public void playerInsert(Player p) {
Connection con=null;
PreparedStatement ps=null;
try {
con=DriverManager.getConnection(url,user,pwd);
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) {
e.printStackTrace();
} finally {
closeConnection(con,ps);
}
}
//상세보기
public Player playerDetail(int num) {
Connection con=null;
Statement st=null;
ResultSet rs=null;
Player p=null;
try {
con=DriverManager.getConnection(url,user,pwd);
String sql="SELECT*FROM player WHERE num="+num;
st=con.createStatement();
rs=st.executeQuery(sql);
if(rs.next()) { //rs가 있다면 p에 다 담으면 됨
p=new Player(); //Player 객체 만들어주고 게터 세터 활용
p.setNum(rs.getInt("num"));
p.setName(rs.getString("name"));
p.setBirth(rs.getString("birth"));
p.setHeight(rs.getDouble("height"));
p.setWeight(rs.getDouble("weight"));
p.setKind(rs.getString("kind"));
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeConnection(con, st, rs);
}
return p;
}
//보기
public ArrayList<Player> playerView() { //arr값을 PlayerSwing2에 돌려줘야함. 리턴값은 ArrayList<Player>
Connection con=null; //연결하는 객체들은 나중에 반드시 닫아주기 (닫기 메소드 따로 사용)
Statement st=null;
ResultSet rs=null;
ArrayList<Player> arr=new ArrayList<Player>(); //rs.next()가 도는 동안 저장해야할 곳=ArrayList
try {
con=DriverManager.getConnection(url,user,pwd);
String sql="select * from player order by num desc";
st=con.createStatement();
rs=st.executeQuery(sql);
while(rs.next()) {
Player p=new Player(); //Player 객체 만들어주고 게터 세터 활용
p.setNum(rs.getInt("num"));
p.setName(rs.getString("name"));
p.setBirth(rs.getString("birth"));
p.setHeight(rs.getDouble("height"));
p.setWeight(rs.getDouble("weight"));
p.setKind(rs.getString("kind"));
arr.add(p);
}
} catch (SQLException e) {
e.printStackTrace();
} finally { //try, catch문 수행하든 반드시 실행. 닫기 메소드 호출
closeConnection(con,st,rs);
}
return arr;
}
//수정
public void playerUpdate(Player p) {
Connection con=null;
PreparedStatement ps=null;
try {
String sql="UPDATE player SET name=?, birth=?, "
+ "height=?, weight=?,kind=? where num=?";
con=DriverManager.getConnection(url,user,pwd);
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.executeUpdate();
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeConnection(con, ps);
}
}
//삭제
public void playerDelete(int num) {
Connection con=null;
Statement st=null;
try {
con=DriverManager.getConnection(url,user,pwd);
String sql="DELETE FROM palyer WHERE num="+num;
st=con.createStatement();
st.executeUpdate(sql);
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeConnection(con, st, null);
}
}
//검색
public ArrayList<Player> playerSearch(String key, String word) {
Connection con=null;
Statement st=null;
ResultSet rs=null;
ArrayList<Player>arr=new ArrayList<Player>(); //전체보기와 똑같은데 sql문만 다름
try {
con=DriverManager.getConnection(url,user,pwd);
st=con.createStatement();
String sql="SELECT * FROM player WHERE "
+key+" LIKE '%"+word+"%'";
rs=st.executeQuery(sql);
while(rs.next()) {
Player p=new Player();
p.setNum(rs.getInt("num"));
p.setName(rs.getString("name"));
p.setBirth(rs.getString("birth"));
p.setHeight(rs.getDouble("height"));
p.setWeight(rs.getDouble("weight"));
p.setKind(rs.getString("kind"));
arr.add(p);
}
} catch (SQLException e) {
e.printStackTrace();
} finally {
closeConnection(con, st, rs);
}
return arr;
}
//닫기 (종료)메소드
public void closeConnection(Connection con, Statement st, ResultSet rs) {
try {
if(rs!=null) rs.close();
if(st!=null) st.close();
if(con!=null) con.close();
}catch(SQLException e){
e.printStackTrace();
}
}
public void closeConnection(Connection con, PreparedStatement ps) {
try {
if(con!=null) con.close();
if(ps!=null) ps.close();
}catch(SQLException e){
e.printStackTrace();
}
}
}
|
cs |
'Learning > JAVA' 카테고리의 다른 글
JUnit 테스트 (Test, BeforeClass, Before, AfterClass, After) (0) | 2020.07.08 |
---|---|
자바 스윙 개인프로젝트-영단어 암기 프로그램 (0) | 2020.07.06 |
선수 정보 입력하는 프레임 만들고 데이터베이스에 연결 (검색버튼 활성화) (PlayerSwing) (0) | 2020.06.30 |
선수 정보 입력하는 프레임 만들고, '추가' 버튼 누를시 데이터베이스에 연결(PlayerSwing) (0) | 2020.06.29 |
메모장처럼 내용을 입력하고 열기, 저장하는 프레임 만들기 (Memo) (0) | 2020.06.29 |