Notice
Recent Posts
Recent Comments
Link
05-15 10:34
관리 메뉴

필피리의 잡학사전

java와 oracle연동 -2(학생관리 예제) 본문

Developer/Java

java와 oracle연동 -2(학생관리 예제)

김수필 2011. 12. 27. 19:23
336x280(권장), 300x250(권장), 250x250, 200x200 크기의 광고 코드만 넣을 수 있습니다.


이번 데이터베이스 수업에 냈던 프로젝트 과제 중 연동소스코드 부분.
학생에게 학번입력받으면 학번을 저장하고 그학번을 쿼리문에 추가하여 정보를 얻어옴

//student_num input listener(JLabel Handler)
//
class Student_num implements ActionListener{

 JTextField student_num;
 
 //생성자(현재 stdudent_num에 입력받은 id값 저장)
 public Student_num(JTextField id)
 {
         this.student_num = id;
 }
 
 
 
 public void actionPerformed(ActionEvent e){

  String num1;
  num1 = this.student_num.getText();
//현재 student_num은 JTextFeild형으로 저장, string값으로 형변환 하여 string값 num1에 저장
  System.out.println("ID : " + num1);
  student_num.setText("");
  //oracle 접속 부분 상황에 따라 다르게
  Connection conn = null;
  ResultSet rs = null;
  Statement stmt = null;
  String url = null;
  String id = "hr";
  String pw = "system";
    
  try{
            url ="jdbc:oracle:thin:@127.0.0.1:1521";
            Class.forName("oracle.jdbc.driver.OracleDriver");
            conn = DriverManager.getConnection(url,id,pw);
            System.out.println("DB connection complete");
            stmt = conn.createStatement();
           
           
            String sql1 = "select Subject_name, Grade, Timing from stu_take_lecture where Student_id = "+num1;
            //String sql1 = "select Subject_name, Grade, Timing from stu_take_lecture where Student_id = 2007103119";
           //입력받은 학번을 쿼리문에 추가 할 때
            
            rs = stmt.executeQuery(sql1);
            JLabel Label[] = new JLabel[100];
            for(int i=0;i<100;i++) 
            {     
                    Label[i] = new JLabel();             
            } 
            int i=0;
            JFrame frm = new JFrame("Grade information print");
            frm.setBounds(120, 120, 600, 600);
            frm.setLayout(new GridLayout(10,1));
      
            while(rs.next()){
                    String Subject_name = rs.getString(1);
                    String Grade = rs.getString(2);
                    String Timing = rs.getString(3);
               
                //name, grade, timing 화면에 출력
                Label[i] = new JLabel(Subject_name+", " + Grade +", "+Timing);
                System.out.println(Subject_name+", " + Grade +", "+Timing);
              
                frm.add(Label[i]); 
                frm.setVisible(true);
               
                i++;
            }
           
            stmt.close();
            conn.close();

  

        } catch (Exception e1){
            e1.printStackTrace();
        }
 }
 public JTextField return_num(){
  return this.student_num;
 }
}

Comments