2014년 1월 21일 화요일

[예제]Swing과 JDBC(오라클)를 이용한 예제[재직자무료교육/프로그래머교육/구로디지털IT교육,오라클/자바/닷넷/C#/iOS/안드로이드/아이폰교육]

[예제]Swing과 JDBC(오라클)를 이용한 예제[재직자무료교육/프로그래머교육/구로디지털IT교육,오라클/자바/닷넷/C#/iOS/안드로이드/아이폰교육]


[예제]Swing과 JDBC(오라클)를 이용한 예제

//오라클의 EMP Table의 데이터를 가지고 화면에 뿌리는 예제
//이름을 입력하고 Enter Key를 누르면 JDBC를 이용하여  데이터를 가지고 옵니다.

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import java.sql.*;

class DBTest {
        JTextField name;    JPasswordField tel;  JTextField addr;
        public DBTest() {
                JFrame f = new JFrame();
                Container cp = f.getContentPane();
                cp.setLayout(new FlowLayout());
                name = new JTextField("",10);
                name.setCaretColor(Color.blue);
                tel  = new JPasswordField("",10);
               
                                tel.setEditable(false);
                addr = new JTextField("",10);
                addr.setEditable(false);
                cp.add(new JLabel("성명 : "));         cp.add(name);
                cp.add(new JLabel("전화번호 : ")); cp.add(tel);
                cp.add(new JLabel("주소 : "));         cp.add(addr);

                name.addActionListener( new ActionListener()
                        {
                                public void actionPerformed(ActionEvent ae) {
                                        dataGet();
                                }
                        }
                );
                f.setSize(600, 100);                f.setVisible(true);
        }
       
                public static void main(String[] args) {
                new DBTest();       
        }

        public void dataGet() {
                Connection con=null;
                Statement stmt=null;
                ResultSet rs=null;
                try {
              Class.forName("oracle.jdbc.driver.OracleDriver");                        
                  con = DriverManager.getConnection("jdbc:oracle:thin:@***.***.***.***:1521:WINK", "test", "test");
                  stmt = con.createStatement();
                  rs = stmt.executeQuery("select tel, addr from emp where name = " + "'" + name.getText().trim() + "'");
                  if (rs!=null) {

                                            rs.next();
                          tel.setText(rs.getString("tel"));
                          addr.setText(rs.getString("addr"));
                  }
                }
                catch(Exception e) {System.out.println(e);}
                finally {
                        try        {
                                if (con != null) {con.close();         }
                        }
                        catch (Exception e){}
                }
        }
}



 


  • 자바
  • 오라클/빅데이터
  • 아이폰/안드로이드
  • 닷넷/WPF
  • 표준웹/HTML5
  • 채용/취업무료교육
  • 초보자코스

  • 댓글 없음:

    댓글 쓰기