관리 메뉴

moozi

7/21 haksa new 본문

TIS_2017/응용sw_2기

7/21 haksa new

moozi 2017. 7. 21. 11:30

import java.awt.Container;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
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.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Haksa extends JFrame {
 Container contentPane;
 String[] language={"영어","중국어","일본어","기타"};
 
 JTextField tfName;
 JTextField tfNo;
 JTextField tfDept_ID;
 JTextField tfAddress;
 
 JTextArea ta;
 
 Connection conn = null; // 연결객체
    ResultSet rs = null;    // select한 결과 조회.cursor
    Statement stmt = null;  // 쿼리실행객체
    String url = null;   // 연결문자열
    String id = "ora_user";    // 오라클 ID
    String pw = "hong";  // 오라클 PW
 
 
 Haksa(){
  this.setTitle("학사관리");
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
  // DB연결 -------------------------------------
  // 연결문자열.
        url ="jdbc:oracle:thin:@localhost:1521:orcl";
        // 드라이버 로드
        try{
         Class.forName("oracle.jdbc.driver.OracleDriver");
        
         // 연결
         conn = DriverManager.getConnection(url,id,pw);
        
         System.out.println("연결되었습니다.");
        
         // statement객체 생성
         stmt = conn.createStatement();
        }catch(Exception e){
         e.printStackTrace(); //에러메시지 출력.
        }
  
  // DB연결.끝.----------------------------------
  
  
  this.contentPane=getContentPane();
  this.contentPane.setLayout(new FlowLayout());
  
  this.contentPane.add(new JLabel("이름"));
  tfName=new JTextField(10);
  this.contentPane.add(tfName);
  
  this.contentPane.add(new JLabel("학번"));
  tfNo=new JTextField(12);
  this.contentPane.add(tfNo);
  
  this.contentPane.add(new JLabel("학과"));
  tfDept_ID=new JTextField(25);
  this.contentPane.add(tfDept_ID);
  
  this.contentPane.add(new JLabel("주소"));
  tfAddress=new JTextField(25);
  this.contentPane.add(tfAddress);
  
//  this.contentPane.add(new JLabel("외국어"));
//  JComboBox cb1=new JComboBox(language);
//  cb1.addActionListener(new ActionListener(){
//   @Override
//   public void actionPerformed(ActionEvent arg0) {
//    JComboBox cb=(JComboBox)arg0.getSource();
//    System.out.println(cb.getSelectedIndex());
//    System.out.println(language[cb.getSelectedIndex()]);
//   }});
//  this.contentPane.add(cb1);
  ta=new JTextArea(10,28);
  this.contentPane.add(new JScrollPane(ta));
  
  JButton insertBtn=new JButton("입력");
  this.contentPane.add(insertBtn);
  insertBtn.addActionListener(new ActionListener(){

   @Override
   public void actionPerformed(ActionEvent arg0) {
           try{
            
               // Insert문 실행
               String sql="insert into student "
                 + " values( '"+tfNo.getText()+"' ,"
                 + "         '"+tfName.getText()+"' ,"
                    + "         '"+tfDept_ID.getText()+"'  ,"
                    + "         '"+tfAddress.getText()+"')";
              
               // Update문 실행
//               String sql="update student set "
//                 + " address='제주도' "
//                 + " where no='A0001'";
              
               // Delete문 실행
//               String sql="delete from student "
//                 + " where no='A0001'";
               stmt.executeUpdate(sql);
              
     
               //stmt.close();
               // conn.close();
     
               //메시지출력
               JOptionPane.showMessageDialog(null, "입력되었습니다", "알림", JOptionPane.INFORMATION_MESSAGE);

           } catch (Exception e){
               e.printStackTrace();
           }

    
   }});
  
  JButton updateBtn=new JButton("수정");
  this.contentPane.add(updateBtn);
  updateBtn.addActionListener(new ActionListener(){

   @Override
   public void actionPerformed(ActionEvent arg0) {
    try{             
              // Update문 실행
              String sql="update student set "
                + " name='"+tfName.getText()+"' , "
                + " dept_ID='"+tfDept_ID.getText()+"' ,"
                + " address='"+tfAddress.getText()+"' "
                + " where no='"+tfNo.getText()+"'";
             
              // Delete문 실행
//              String sql="delete from student "
//                + " where no='A0001'";
              System.out.println(sql);
             
              stmt.executeUpdate(sql);
             
             
    
              //stmt.close();
              // conn.close();
    
              //메시지출력
              JOptionPane.showMessageDialog(null, "수정되었습니다", "알림", JOptionPane.INFORMATION_MESSAGE);

          } catch (Exception e){
              e.printStackTrace();
          }
    
   }});
  
  JButton deleteBtn=new JButton("삭제");
  this.contentPane.add(deleteBtn);
  deleteBtn.addActionListener(new ActionListener(){
   @Override
   public void actionPerformed(ActionEvent arg0) {
    try{             
              // Confirm Dialog사용
     int result=JOptionPane.showConfirmDialog(null, "삭제하시겠습니까?","삭제",JOptionPane.YES_NO_OPTION);
     if(result==JOptionPane.YES_OPTION){
      // Delete문 실행
               String sql="delete from student "
                 + " where no='"+tfNo.getText()+"'";
               System.out.println(sql);
              
               stmt.executeUpdate(sql);
              
             //메시지출력
               JOptionPane.showMessageDialog(null, "삭제되었습니다", "알림", JOptionPane.INFORMATION_MESSAGE);
     }
             
    
              //stmt.close();
              // conn.close();
    
             

          } catch (Exception e){
              e.printStackTrace();
          }
    
    
   }});
  
  
  JButton selectBtn=new JButton("목록");
  this.contentPane.add(selectBtn);
  selectBtn.addActionListener(new ActionListener(){
   @Override
   public void actionPerformed(ActionEvent arg0) {
    
           try{                          // select문 실행
               rs = stmt.executeQuery("select * from student");
              
               // ta 리셋
               ta.setText("");
              
               // 컬럼명 추가
               ta.append("no"
                 + "\t"
                 + "name"
                 + "\t"
                 + "dept_id"
                 + "\t"
                 + "address"                 
                 + "\n");
               ta.append("==================================================\n");
              
               // Fetch
               while(rs.next()){
                   String no = rs.getString("no");
                   String name = rs.getString("name");
                   String dept_id = rs.getString("dept_id");
                   String address=rs.getString("address");
                 
                   ta.append(no+"\t"
                     + name +"\t"
                     + dept_id +"\t"
                     + address +"\n"
                     );
               }
     
               rs.close();
              // stmt.close();
              // conn.close();
     

           } catch (Exception a){
               a.printStackTrace();
           }
    
   }});
  
  this.setSize(350,400);
  this.setVisible(true);
 }

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

 }

}

'TIS_2017 > 응용sw_2기' 카테고리의 다른 글

jdbc연습문제02  (0) 2017.07.21
jdbc03 new  (0) 2017.07.21
7/21 haksa  (0) 2017.07.21
jdbc 연습문제01  (0) 2017.07.20
오라클 jdbc기초  (0) 2017.07.20
Comments