Skip to content

Commit 546d4bc

Browse files
committed
도서추가 예외처리
1 parent 6265afb commit 546d4bc

File tree

2 files changed

+134
-4
lines changed

2 files changed

+134
-4
lines changed

images/nobook.png

2.75 KB
Loading

src/view/AddBook.java

Lines changed: 134 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616
import java.awt.Color;
1717
import javax.swing.SwingConstants;
1818
import javax.swing.border.LineBorder;
19+
import javax.swing.event.DocumentEvent;
20+
import javax.swing.event.DocumentListener;
1921
import javax.swing.filechooser.FileNameExtensionFilter;
2022

2123
import java.awt.SystemColor;
@@ -31,6 +33,7 @@
3133
import java.sql.PreparedStatement;
3234
import java.sql.ResultSet;
3335
import java.sql.SQLException;
36+
import java.sql.SQLIntegrityConstraintViolationException;
3437

3538
import javax.swing.JTextArea;
3639
import javax.imageio.ImageIO;
@@ -48,6 +51,8 @@ public class AddBook extends JFrame {
4851
dbConnector dbConn = new dbConnector();
4952
public String user_phone=""; //유저 PK 정보를 저장할 변수
5053
public Boolean manager = false; //유저가 관리자인지 확인할 변수
54+
int ret = 1; //파일 선택 여부를 알려주는 변수
55+
JButton bookAddButton;
5156
private JPanel contentPane;
5257
private JTextField bookNameTextField;
5358
private JTextField bookHeaderTextField_1;
@@ -105,13 +110,14 @@ public AddBook(String user_phone, Boolean manager) {
105110
bookImageFindButton.addMouseListener(new MouseAdapter() { // 클릭이벤트
106111
@Override
107112
public void mouseClicked(MouseEvent e) {
108-
int ret = book_img.showOpenDialog(null); //파일 찾는 창을 띄우줌
113+
ret = book_img.showOpenDialog(null); //파일 찾는 창을 띄우줌
109114
if(ret == 0) { //파일을 선택했다면
110115

111116
String filePath = book_img.getSelectedFile().getPath(); //파일 경로를 filePath에 저장
112117

113118
JOptionPane.showMessageDialog(null, filePath,"당신이 선택한 파일은",JOptionPane.NO_OPTION); //선택한 파일경로를 메시지 창으로 띄움
114119
book_img_path.setText(filePath);
120+
115121
try { // DB 접근
116122
//책 이미지
117123
File tmpFile = new File(book_img_path.getText());
@@ -157,6 +163,27 @@ public void mouseClicked(MouseEvent e) {
157163
bookNameTextField.setBounds(92, 20, 407, 26);
158164
panel_1.add(bookNameTextField);
159165
bookNameTextField.setColumns(10);
166+
bookNameTextField.getDocument().addDocumentListener(new DocumentListener() {
167+
public void changedUpdate(DocumentEvent e) {
168+
changed();
169+
}
170+
public void removeUpdate(DocumentEvent e) {
171+
changed();
172+
}
173+
public void insertUpdate(DocumentEvent e) {
174+
changed();
175+
}
176+
177+
public void changed() {
178+
if (bookISBNTextField.getText().equals("")){
179+
bookAddButton.setEnabled(false);
180+
}
181+
else {
182+
bookAddButton.setEnabled(true);
183+
}
184+
185+
}
186+
});
160187

161188
// 책 저자 라벨
162189
JLabel bookHeaderLabel_1 = new JLabel("\uC800\uC790 :");
@@ -171,6 +198,27 @@ public void mouseClicked(MouseEvent e) {
171198
bookHeaderTextField_1.setColumns(10);
172199
bookHeaderTextField_1.setBounds(92, 60, 157, 26);
173200
panel_1.add(bookHeaderTextField_1);
201+
bookHeaderTextField_1.getDocument().addDocumentListener(new DocumentListener() {
202+
public void changedUpdate(DocumentEvent e) {
203+
changed();
204+
}
205+
public void removeUpdate(DocumentEvent e) {
206+
changed();
207+
}
208+
public void insertUpdate(DocumentEvent e) {
209+
changed();
210+
}
211+
212+
public void changed() {
213+
if (bookISBNTextField.getText().equals("")){
214+
bookAddButton.setEnabled(false);
215+
}
216+
else {
217+
bookAddButton.setEnabled(true);
218+
}
219+
220+
}
221+
});
174222

175223
// 책 출판사 라벨
176224
JLabel bookHeaderLabel_2 = new JLabel("\uCD9C\uD310\uC0AC : ");
@@ -184,6 +232,27 @@ public void mouseClicked(MouseEvent e) {
184232
bookHeaderTextField_2.setColumns(10);
185233
bookHeaderTextField_2.setBounds(342, 60, 157, 26);
186234
panel_1.add(bookHeaderTextField_2);
235+
bookHeaderTextField_2.getDocument().addDocumentListener(new DocumentListener() {
236+
public void changedUpdate(DocumentEvent e) {
237+
changed();
238+
}
239+
public void removeUpdate(DocumentEvent e) {
240+
changed();
241+
}
242+
public void insertUpdate(DocumentEvent e) {
243+
changed();
244+
}
245+
246+
public void changed() {
247+
if (bookISBNTextField.getText().equals("")){
248+
bookAddButton.setEnabled(false);
249+
}
250+
else {
251+
bookAddButton.setEnabled(true);
252+
}
253+
254+
}
255+
});
187256

188257
// 책 관련링크 라벨
189258
JLabel bookLinkLabel = new JLabel("\uAD00\uB828\uB9C1\uD06C : ");
@@ -203,33 +272,79 @@ public void mouseClicked(MouseEvent e) {
203272
bookPriceLabel.setFont(new Font("한컴산뜻돋움", Font.PLAIN, 16));
204273
bookPriceLabel.setBounds(12, 107, 46, 26);
205274
panel_1.add(bookPriceLabel);
275+
206276

207277
// 책 가격 텍스트필드
208278
bookPriceTextField = new JTextField();
209279
bookPriceTextField.setFont(new Font("한컴산뜻돋움", Font.PLAIN, 16));
210280
bookPriceTextField.setColumns(10);
211281
bookPriceTextField.setBounds(92, 107, 407, 26);
212282
panel_1.add(bookPriceTextField);
283+
bookPriceTextField.getDocument().addDocumentListener(new DocumentListener() {
284+
public void changedUpdate(DocumentEvent e) {
285+
changed();
286+
}
287+
public void removeUpdate(DocumentEvent e) {
288+
changed();
289+
}
290+
public void insertUpdate(DocumentEvent e) {
291+
changed();
292+
}
293+
294+
public void changed() {
295+
if (bookISBNTextField.getText().equals("")){
296+
bookAddButton.setEnabled(false);
297+
}
298+
else {
299+
bookAddButton.setEnabled(true);
300+
}
301+
302+
}
303+
});
213304

214305
// 책 ISBN 라벨
215306
JLabel bookISBNLabel = new JLabel("ISBN : ");
216307
bookISBNLabel.setFont(new Font("한컴산뜻돋움", Font.PLAIN, 16));
217308
bookISBNLabel.setBounds(12, 154, 58, 26);
218309
panel_1.add(bookISBNLabel);
310+
219311

220312
// 책 ISBN 텍스트필드
221313
bookISBNTextField = new JTextField();
222314
bookISBNTextField.setFont(new Font("한컴산뜻돋움", Font.PLAIN, 16));
223315
bookISBNTextField.setColumns(10);
224316
bookISBNTextField.setBounds(92, 154, 407, 26);
225317
panel_1.add(bookISBNTextField);
318+
bookISBNTextField.getDocument().addDocumentListener(new DocumentListener() {
319+
public void changedUpdate(DocumentEvent e) {
320+
changed();
321+
}
322+
public void removeUpdate(DocumentEvent e) {
323+
changed();
324+
}
325+
public void insertUpdate(DocumentEvent e) {
326+
changed();
327+
}
328+
329+
public void changed() {
330+
if (bookISBNTextField.getText().equals("")){
331+
bookAddButton.setEnabled(false);
332+
}
333+
else {
334+
bookAddButton.setEnabled(true);
335+
}
336+
337+
}
338+
});
339+
226340

227341
// 책 줄거리 패널
228342
JPanel panel_2 = new JPanel();
229343
panel_2.setBackground(new Color(255, 255, 255));
230344
panel_2.setBounds(12, 310, 805, 177);
231345
contentPane.add(panel_2);
232346
panel_2.setLayout(null);
347+
233348

234349
JScrollPane scrollPane = new JScrollPane();
235350
scrollPane.setViewportBorder(new LineBorder(new Color(128, 128, 128), 1, true));
@@ -242,13 +357,15 @@ public void mouseClicked(MouseEvent e) {
242357
bookDescriptionTextField.setColumns(10);
243358

244359
// 책 추가 버튼
245-
JButton bookAddButton = new JButton("\uB3C4\uC11C \uCD94\uAC00");
360+
bookAddButton = new JButton("\uB3C4\uC11C \uCD94\uAC00");
361+
bookAddButton.setEnabled(false);
246362
bookAddButton.setFont(new Font("한컴산뜻돋움", Font.PLAIN, 15));
247363
bookAddButton.setBounds(348, 511, 132, 48);
248364
//책 추가 버튼을 누르면 호출되는 메소드 연결
249365
bookAddButton.addActionListener(new ActionListener() {
250366
@Override
251367
public void actionPerformed(ActionEvent e) {
368+
boolean isSuccess = false;
252369

253370
String sql = "insert into BOOK(\r\n"
254371
+ "BOOK_ISBN,\r\n"
@@ -274,19 +391,31 @@ public void actionPerformed(ActionEvent e) {
274391
ps.setString(6, bookDescriptionTextField.getText()); //책 줄거리
275392
ps.setString(7, bookLinkTextField.getText()); //책 관련링크
276393

394+
if(ret == 1) book_img_path.setText("images/nobook.png");
277395
//책 이미지
278396
FileInputStream fin = new FileInputStream(book_img_path.getText());
279397
ps.setBinaryStream(8, fin, fin.available());
280398

281399

400+
282401
int count = ps.executeUpdate();
283402
if(count==0) {
284403
JOptionPane.showMessageDialog(null,"ISBN : "+bookISBNTextField.getText()+"이(는) 등록에 실패하였습니다.", "신규도서등록 실패", JOptionPane.ERROR_MESSAGE);
285404
}
286405
else {
287406
JOptionPane.showMessageDialog(null,"ISBN : "+bookISBNTextField.getText()+"이(는) 등록에 성공하였습니다.", "신규도서등록 성공", JOptionPane.NO_OPTION);
407+
isSuccess = true;
288408
}
289-
} catch (SQLException e1) {
409+
}
410+
catch (NumberFormatException e1) {
411+
e1.printStackTrace(); //에러 추적
412+
JOptionPane.showMessageDialog(null, "가격에 숫자만 입력가능합니다.", "입력 오류", JOptionPane.ERROR_MESSAGE); //가격에 문자 입력시 메시지 호출
413+
}
414+
catch(SQLIntegrityConstraintViolationException e1) {
415+
e1.printStackTrace();
416+
JOptionPane.showMessageDialog(null, "입력한 ISBN이 이미 존재합니다.", "입력 오류", JOptionPane.ERROR_MESSAGE); //가격에 문자 입력시 메시지 호출
417+
}
418+
catch (SQLException e1) {
290419
e1.printStackTrace(); //에러 추적
291420
System.out.println("도서추가 화면에서 SQL 실행 에러");
292421
}catch(FileNotFoundException e1) {
@@ -295,7 +424,8 @@ public void actionPerformed(ActionEvent e) {
295424
} catch (IOException e1) {
296425
e1.printStackTrace();
297426
}
298-
dispose(); //추가 후 창 닫기
427+
if(isSuccess)
428+
dispose(); //추가 후 창 닫기
299429
}
300430
});
301431
contentPane.add(bookAddButton); //책 추가 버튼 부착

0 commit comments

Comments
 (0)