관리 메뉴

moozi

7/21 haksa 본문

TIS_2017/응용sw_2기

7/21 haksa

moozi 2017. 7. 21. 10:29

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.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;

public class Haksa extends JFrame {
 Container contentPane;
 String[] language={"영어","중국어","일본어","기타"};
 
 JTextArea ta;
 
 Haksa(){
  this.setTitle("학사관리");
  this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
  this.contentPane=getContentPane();
  this.contentPane.setLayout(new FlowLayout());
  
  this.contentPane.add(new JLabel("이름"));
  this.contentPane.add(new JTextField(10));
  
  this.contentPane.add(new JLabel("학번"));
  this.contentPane.add(new JTextField(12));
  
  this.contentPane.add(new JLabel("학과"));
  this.contentPane.add(new JTextField(25));
  
  this.contentPane.add(new JLabel("주소"));
  this.contentPane.add(new JTextField(25));
  
//  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));
  
  this.contentPane.add(new JButton("입력"));
  this.contentPane.add(new JButton("수정"));
  this.contentPane.add(new JButton("삭제"));
  JButton selectBtn=new JButton("목록");
  this.contentPane.add(selectBtn);
  selectBtn.addActionListener(new ActionListener(){
   @Override
   public void actionPerformed(ActionEvent arg0) {
    Connection conn = null; // 연결객체
        ResultSet rs = null;    // select한 결과 조회.cursor
        Statement stmt = null;  // 쿼리실행객체
        String url = null;   // 연결문자열
        String id = "ora_user";    // 오라클 ID
        String pw = "hong";  // 오라클 PW
      
           try{
            // 연결문자열.
               url ="jdbc:oracle:thin:@localhost:1521:orcl";
               // 드라이버 로드
               Class.forName("oracle.jdbc.driver.OracleDriver");
              
               // 연결
               conn = DriverManager.getConnection(url,id,pw);
              
               System.out.println("연결되었습니다.");
              
               // statement객체 생성
               stmt = conn.createStatement();
               // 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"
                     );
               }
     
               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기' 카테고리의 다른 글

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