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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
|
package employee;
import java.awt.Container;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
public class Employee extends JFrame {
private Connection con;
private Statement stmt;
private String url = "jdbc:oracle:thin:@192.168.0.7:1521:onj";
private String ID = "scott";
private String PW = "tiger";
private ResultSet rs = null;
private JButton[] btn = new JButton[5];
private JLabel[] label = new JLabel[8];
private JTextField[] text = new JTextField[8];
private JTable table;
private JScrollPane jp = new JScrollPane();
private JPanel p[] = new JPanel[4];
private JPanel p2 = new JPanel();
private String[][] data = new String[100][8];
private String[] col = { "empno ", "ename ", "job ", "mgr ", "hiredate", "sal ",
"comm ", "deptno " };
private String[] string = { "", "", "", "", "", "", "", "" };
public Employee() {
try {
Class.forName("oracle.jdbc.driver.OracleDriver"); // JDBC 사용
DriverManager.setLoginTimeout(60);
con = DriverManager.getConnection(url, ID, PW); // DB와 연결
stmt = con.createStatement();
} catch (Exception ex) {
System.err.println(ex);
}
JPanel p1 = new JPanel();
p1.setLayout(new GridLayout(5, 1));
JPanel p3 = new JPanel();
btn[0] = new JButton("선택");
btn[1] = new JButton("출력");
btn[2] = new JButton("입력");
btn[3] = new JButton("수정");
btn[4] = new JButton("삭제");
for (int i = 0; i < 5; i++) {
btn[i].addActionListener(action);
p3.add(btn[i]);
}
for (int i = 0; i < 8; i++) {
label[i] = new JLabel(col[i]);
}
for (int i = 0; i < 8; i++) {
text[i] = new JTextField(15);
}
for (int i = 0; i < 4; i++) {
p[i] = new JPanel();
}
int j = -1;
for (int i = 0; i < 8; i++) {
if (i % 2 == 0) {
j++;
}
p[j].add(label[i]);
p[j].add(text[i]);
}
p1.add(p3);
p1.add(p[0]);
p1.add(p[1]);
p1.add(p[2]);
p1.add(p[3]);
table = new JTable(data, col);
jp = new JScrollPane(table);
jp.addMouseListener(new MouseAdapter() {
});
Container cp = this.getContentPane();
cp.setLayout(new GridLayout(2, 1));
cp.add(p1);
cp.add(jp);
setDefaultCloseOperation(DISPOSE_ON_CLOSE);
setBounds(100, 100, 500, 500);
setVisible(true);
showTable();
}
public Statement getStatement() {
return stmt;
}
public void showTable() {
table.removeAll();
try {
for (int i = 0; i < 100; i++) {
for (int j = 0; j < 8; j++) {
data[i][j] = "";
}
}
rs = stmt.executeQuery("select * from emp");
int i = 0;
while (rs.next()) {
data[i][0] = rs.getString(1);
data[i][1] = rs.getString(2);
data[i][2] = rs.getString(3);
data[i][3] = rs.getString(4);
data[i][4] = rs.getString(5);
data[i][5] = rs.getString(6);
data[i][6] = rs.getString(7);
data[i++][7] = rs.getString(8);
}
table.repaint();
} catch (Exception e) {
System.err.println("보여 주기 에러");
}
}
ActionBtn action = new ActionBtn();
class ActionBtn extends Exception implements ActionListener {
public void actionPerformed(ActionEvent e) {
if (e.getActionCommand().equals("출력")) {
showTable();
} else if (e.getActionCommand().equals("선택")) {
int row = table.getSelectedRow();
/////////////////////
// 텍스트 필드에 받아온 데이터 보여주기
for (int i = 0; i < 8; i++) {
text[i].setText(data[row][i]);
}
} else if (e.getActionCommand().equals("입력")) {
try {
for (int i = 0; i < 8; i++) {
if (text[i].getText().equals("")) {
if (i == 0 || i == 3 || i == 5 || i == 6 || i == 7)
string[i] = "\'\'";
else
string[i] = "";
} else
string[i] = text[i].getText();
}
stmt.executeQuery("insert into emp values(" + string[0] + ",'" + string[1] + "' ,'" + string[2]
+ "'," + string[3] + ",'" + string[4] + "'," + string[5] + "," + string[6] + "," + string[7]
+ ")");
stmt.executeQuery("commit");
} catch (Exception ex) {
System.err.println("입력 에러");
}
////////////////////
// 화면에 출력하기
showTable();
for (int i = 0; i < 8; i++) {
text[i].setText("");
}
} else if (e.getActionCommand().equals("수정")) {
try {
stmt.executeQuery(
"update emp set" + " empno = " + text[0].getText() + " ,ename='" + text[1].getText()
+ "',job='" + text[2].getText() + "',mgr=" + text[3].getText() + ",hiredate='"
+ text[4].getText() + "',sal=" + text[5].getText() + ",comm=" + text[6].getText()
+ ",deptno =" + text[7].getText() + "where empno = " + text[0].getText());
stmt.executeQuery("commit");
} catch (Exception ex) {
System.err.println("수정 에러");
}
////////////////////
// 화면에 출력하기
showTable();
for (int i = 0; i < 8; i++) {
text[i].setText("");
}
} else if (e.getActionCommand().equals("삭제")) {
try {
System.out.println(text[0].getText());
stmt.executeQuery("delete from emp where" + " empno = " + text[0].getText());
stmt.executeQuery("commit");
} catch (Exception ex) {
System.err.println("삭제 에러");
}
////////////////////
// 화면에 출력하기
showTable();
for (int i = 0; i < 8; i++) {
text[i].setText("");
}
}
}
}
public static void main(String[] args) {
Employee db = new Employee();
}
}
|
cs |
'Java' 카테고리의 다른 글
[자바 Swing] Calculator.java (0) | 2020.04.30 |
---|---|
19. 회원관리 Swing 앱 :: AdminController.java (0) | 2020.04.30 |
[자바 Swing] 로또 LottoMain.java LottoUI.java Lotto.java (0) | 2020.04.30 |
[자바 객체지향] 인터페이스 활용한 가위바위보 게임 Rps.java (0) | 2020.04.30 |
[자바 객체지향] 인터페이스 예제 Bank.java (0) | 2020.04.30 |