관리 메뉴

nkdk의 세상

자바 Language 25일째 상품 추가, 삭제, 종료 만들기 본문

My Programing/JAVA

자바 Language 25일째 상품 추가, 삭제, 종료 만들기

nkdk 2008. 3. 8. 01:19
package db;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.table.*;
import java.sql.*;


public class SangpumData extends JFrame implements ActionListener {
JButton btnInsert,btnDelete,btnExit,btnExit1,btnExit2,btnExit3,btnExit4;
String [][] data=new String[0][5];
String []cols={"코드","상품명","수량","단가","금액"};
DefaultTableModel mod=new DefaultTableModel(data,cols);
JTable tab=new JTable(mod);
JLabel lblCou=new JLabel();


Connection conn;
Statement stmt;
ResultSet rs;

public SangpumData() {

// this.setResizable(false); //테이블 크기 변경 못하게
this.setTitle("상품정보");

JPanel pn=new JPanel(new FlowLayout(FlowLayout.RIGHT));
btnInsert=new JButton("추가");
btnDelete=new JButton("삭제");
btnExit=new JButton("종료");

pn.add(btnInsert);
pn.add(btnDelete);
pn.add(btnExit);

btnInsert.addActionListener(this);
btnDelete.addActionListener(this);
btnExit.addActionListener(this);

JScrollPane scroll=new JScrollPane(tab,ScrollPaneConstants.
VERTICAL_SCROLLBAR_AS_NEEDED,
ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
this.getContentPane().add("North",pn);
this.getContentPane().add("Center",scroll);
this.getContentPane().add("South",lblCou);

this.setBounds(200, 200, 400, 300);
this.setVisible(true);

accdb();
}

private void accdb() {

try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch (Exception e) {
JOptionPane.showMessageDialog(this, "로딩실패:"+e);
}
try {
conn=DriverManager.getConnection("jdbc:odbc:oradbs","scott","tiger");
stmt=conn.createStatement();


} catch (Exception e) {
JOptionPane.showMessageDialog(this, "연결실패:"+e);
}
disp();

}





private void disp() { //테이블에 자료 출력
try {
mod.setNumRows(0); //표를 초기화
rs=stmt.executeQuery("SELECT * FROM SANGDATA ORDER BY CODE");
while(rs.next()){
String code=rs.getString("CODE");
String sang=rs.getString("SANG");
String su=rs.getString("SU");
String dan=rs.getString("DAN");
int kum=Integer.parseInt(su)* Integer.parseInt(dan);
String kumaek=Integer.toString(kum);
String imsi[]={code,sang,su,dan,kumaek};
mod.addRow(imsi);
}
rs=stmt.executeQuery("SELECT COUNT(*)FROM SANGDATA");
rs.next();
lblCou.setText("전체건수:"+rs.getString(1));

} catch (Exception e) {
System.out.println(e);
}

}





public void actionPerformed(ActionEvent ae) {
if(ae.getActionCommand().equals("추가")){ //별도의 창을 불러서 하겟음..
//InWin();

InWin iw=new InWin (this,"상품자료 추가");
iw.display();

disp();

}
else if (ae.getActionCommand().equals("삭제")){
try {
String re=JOptionPane.showInputDialog(this,"삭제할 번호 입력");
if(re.equals(""))return;

rs=stmt.executeQuery("SELECT COUNT(*) FROM SANGDATA WHERE CODE="+
re);

rs.next();
if(rs.getInt(1) > 0){ //삭제가능
stmt.executeUpdate("DELETE FROM SANGDATA WHERE CODE=" + re);
disp(); //삭제된후 disp호출 다시 지료 뿌림..
}
else
JOptionPane.showMessageDialog(this, "등록되지 않은 코드");
return;
} catch (Exception e) {
JOptionPane.showMessageDialog(this,"삭제 취소");
return;
}

}

else if (ae.getActionCommand().equals("종료")){
try {
rs.close(); stmt.close();conn.close();
System.exit(0);
} catch (Exception e) {

}System.out.println("aaa");

}

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

}
}


class InWin extends Dialog implements ActionListener {
JTextField txtSang=new JTextField();
JTextField txtSu=new JTextField();
JTextField txtDan=new JTextField();
JButton btnOk=new JButton("등록");
JButton btnCan=new JButton("취소");

String msg;

public InWin(Frame f, String s){
super(f,"상품추가",true);
msg=s;

addWindowListener(new WindowAdapter(){

public void windowClosing(WindowEvent we){
dispose();
}
});
}
public void display(){
JPanel pnc=new JPanel (new GridLayout(3,2));
pnc.add(new JLabel("상품명 :"));
pnc.add(txtSang);
pnc.add(new JLabel("수 량 :"));
pnc.add(txtSu);
pnc.add(new JLabel("단 가:"));
pnc.add(txtDan);

JPanel pn=new JPanel();
pn.add(btnOk);
pn.add(btnCan);
btnCan.addActionListener(this);
btnOk.addActionListener(this);

this.add("North",new JLabel(msg,JLabel.CENTER));
this.add("Center",pnc);
this.add("South",pn);

setBounds(300, 300, 200, 200);
setVisible(true);

}

public void actionPerformed(ActionEvent ae){
if (ae.getActionCommand().equals("등록")) {
if(txtSang.getText().equals("")) {
JOptionPane.showMessageDialog(this, "상품명을 입력하시오.");
txtSang.requestFocus();
return;
}
if(txtSu.getText().equals("")) {
JOptionPane.showMessageDialog(this, "수량을 입력하시오.");
txtSang.requestFocus();
return;
}
if(txtDan.getText().equals("")) {
JOptionPane.showMessageDialog(this, "단가를 입력하시오.");
txtSang.requestFocus();
return;
}
// 수량 단위 제한 : 1 ~ 10000 허용
int su=Integer.parseInt(txtSu.getText());
if (su <1 || su > 10000) {
JOptionPane.showMessageDialog(this, "수량 범위 초과(1~10000)");
txtSu.requestFocus();
return;
}
// 단가 범위 제한
int dan=Integer.parseInt(txtDan.getText());
if (dan <1 || dan > 999999) {
JOptionPane.showMessageDialog(this, "단가 범위 초과(1~999999)");
txtDan.requestFocus();
return;
}
// 등록 가능 상태
Connection conn;
Statement stmt;
ResultSet rs;
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
conn=DriverManager.getConnection("jdbc:odbc:oradbs", "scott", "tiger");
stmt=conn.createStatement();

// 상품명은 unique이다.
rs=stmt.executeQuery("select count(*) from sangdata where sang='"+txtSang.getText()+"'");
rs.next();
if(rs.getInt(1) > 0) {
JOptionPane.showMessageDialog(this, "해당 상품명이 이미 있습니다.");
txtSang.requestFocus();
return;
}
String sql="insert into sangdata values(code_seq.nextval, '"+txtSang.getText()+"' , "+txtSu.getText() +
"," + txtDan.getText() + ")";
stmt=conn.createStatement();
System.out.println(sql);
stmt.executeUpdate(sql);
rs.close(); stmt.close(); conn.close();
dispose();
} catch (Exception e) {
System.out.println(e);
}
}else if(ae.getActionCommand().equals("취소")) {
txtSang.setText(null);
txtSu.setText(null);
txtDan.setText(null);
txtSang.requestFocus();
}
}
}