//오라클의 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){} } } }
|
|
댓글 없음:
댓글 쓰기