Upload
isabel-stevens
View
252
Download
2
Embed Size (px)
Citation preview
JDBC
컴퓨터정보과 안유정 교수연구동 410호 [email protected]
차례차례
JDBC 란 ?
JDBC 구조
JDBC 접속
JDBC 프로그래밍 예제
JBDCJBDC
JDBC (Java Database Connectivity)
자바 환경에서 데이터베이스 처리를 위한 메커니즘
자바 클라이언트와 관계형 데이터베이스 서버와의 연동을 위한 메커니즘
자바 프로그램을 사용하여 데이터베이스에 접속하고 SQL 문을 실행하고 실행 결과로 데이터를 얻는 일련의 과정 제공
자바 프로그램 내에 SQL 명령문을 사용하여 데이터베이스와 연동
JDBC 구조
JDBC JDBC 구조구조자바 프로그램 자바 프로그램
JDBC API
JDBC Manager
JDBC Driver API
JDBC
JDBC-ODBCBridge Driver
JDBC NetworkDriver
Direct JDBCDriver
DBMS DBMSDBMS
ODBC Manager& ODBC Driver
JDBC JDBC 구조구조 JDBC API데이터베이스 연동을 가능하게 하는 , Java.sql 패키지에 들어있는 프로그램들
JDBC Manager( 관리자 )
응용 프로그램이 요구하는 데이터베이스에 접근하도록 적절한 드라이버 선택
데이터베이스와 연결
JDBC Driver 다양한 DBMS 제조사들이 자기 회사의 DB를 Sun사의 자바 프로그램과 연동할 수 있도록 지원하는 기술로서 , 제조사마다 다른 JDBC 드라이버를 제공MSSQL2K , ORACLE, Cybase, DB2, MySQL 등 다양한 드라이버
JDBC JDBC 접속 접속 1. 데이터베이스 등록
데이터베이스를 만든다 . (예 : student.accdb)
제어판 – 관리도구 – 데이터 원본 (ODBC) – 시스템 DSN 에서 데이터베이스 파일 (student.accdb) 추가
2. JDBC Driver 를 다운로드
사용하고자 하는 DBMS 사의 드라이버를 다운로드한다 .
JDBC-ODBC Bridge 드라이버를 사용할 경우 SDK에 기본적으로
포함되어 있으므로 별도의 다운로드가 필요없다 .
JDBC JDBC 접속 접속 3. JDBC 드라이버 로딩
자바 소스 프로그램 내에 , 사용할 JDBC 드라이버를 로드한다 .
Class.forName(“드라이버 클래스 명” );
예 ) Class.forName(“sun.jdbc.odbc.JdbcOdbcDriver”);
Class.forName(“oracle.jdbc.odbc.OracleDriver”);
4. JDBC 연결 Java.sql.DriverManager 클래스에 의해 Connection 객체를 생성하여
DB와 연결
Connection con = DriverManager.getConnection( “JDBC URL” );
JDBC URL의 형태 jdbc : <하위 프로토콜 명 > : <데이터베이스 원본 >
Connection con = DriverManager.getConnection(“jdbc:odbc:student”);
Connection con = DriverManager.getConnection(
“jdbc:oracle:[email protected]:8080:student” );
JDBC JDBC 프로그래밍프로그래밍
DB 연동을 위해 아래와 같은 데이터베이스를 먼저 만들어 보자 .
1. MS Access 를 연다 .
2. 새 테이블에 id, password, 이름 , 연락처 1, 연락처 2, 주소 필드 생성 (password는 숫자 , 나머지는 텍스트 필드로 )
3. 테이블 이름을 student_info 로 저장4. 자신의 폴더에 student로 데이터베이스 저장
또는다음 장에서처럼 자바 프로그램에서 테이블 생성도 가능
JDBC JDBC 프로그래밍프로그래밍 테이블 생성 예제 - CreateTable.java
import java.sql.*;
public class CreateTable {
public static void main(String[] args) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // JDBC 드라이버 로딩 System.err.println("JDBC-ODBC 드라이버를 정상적으로 로드함 ");
} catch(java.lang.ClassNotFoundException e) {
System.err.println(" 드라이버 로드에 실패했습니다 .");
}
CreateTable.java (계속 )
try {
Connection con = DriverManager.getConnection("jdbc:odbc:student"); //DB연결 Statement dbSt = con.createStatement(); // Statement 객체 생성 System.out.println("JDBC 드라이버가 정상적으로 연결되었습니다 .");
String strSql = "CREATE TABLE student_info (ID varchar, password long,”+
“이름 varchar, 연락처1 varchar, 연락처2 varchar, 주소 varchar)"; //SQL질의어생성 dbSt.executeUpdate(strSql); // SQL 질의어 실행 System.out.println("테이블을 생성했습니다 .");
dbSt.close(); // Statement 객체 종료 con.close(); // DB 연결 해제 } catch (SQLException e) {
System.out.println("SQLException : "+e.getMessage());
}
}
}
JDBC JDBC 프로그래밍프로그래밍
테이블 생성 예제 결과
JDBC JDBC 프로그래밍프로그래밍
테이블 생성후 데이터베이스 상태
JDBC JDBC 프로그래밍프로그래밍
데이터 삽입 예제앞에서 생성한 아래 테이블에 학생들의 데이터를 삽입하는 예제
JDBC JDBC 프로그래밍프로그래밍 InsertData.java
import java.sql.*;
public class InsertData {
public static void main(String[] args) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
System.err.println("JDBC-ODBC 드라이버를 정상적으로 로드함 ");
} catch(java.lang.ClassNotFoundException e) {
System.err.println(" 드라이버 로드에 실패했습니다 .");
}
InsertData.java (계속 )
try {
Connection con = DriverManager.getConnection("jdbc:odbc:student");
Statement dbSt = con.createStatement();
System.out.println("JDBC 드라이버가 정상적으로 연결되었습니다 .");
String strSql = "INSERT INTO student_info (ID, password, 이름 , 연락처1, “+
“연락처2, 주소 ) VALUES (‘abc12’, 1230, '안유정 ', 010, '123-1234‘, ‘서울 홍은동’ )";
dbSt.executeUpdate(strSql);
System.out.println("데이터 삽입 완료 ");
dbSt.close();
con.close();
} catch (SQLException e) {
System.out.println("SQLException : "+e.getMessage());
}
}
}
JDBC JDBC 프로그래밍프로그래밍
데이터 삽입 예제 결과
JDBC JDBC 프로그래밍프로그래밍
데이터 삽입후 데이터베이스 상태
JDBC JDBC 프로그래밍프로그래밍
데이터 수정 예제데이터베이스 테이블의 정보가 아래와 같을때 ID=dream33 인 학생의 연락처를 수정하는 예제
JDBC JDBC 프로그래밍프로그래밍 UpdateData.java
import java.sql.*;
public class UpdateData {
public static void main(String[] args) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// System.err.println("JDBC-ODBC 드라이버를 정상적으로 로드함 ");
} catch(java.lang.ClassNotFoundException e) {
System.err.println(" 드라이버 로드에 실패했습니다 .");
}
UpdateData.java (계속 )
try {
Connection con = DriverManager.getConnection("jdbc:odbc:student");
Statement dbSt = con.createStatement();
System.out.println("JDBC 드라이버가 정상적으로 연결되었습니다 .");
String strSql = "UPDATE student_info SET 연락처1= ‘011’,
연락처2= ‘777-7777‘ WHERE ID = ‘dream33’ ";
dbSt.executeUpdate(strSql);
System.out.println("데이터 수정 완료 ");
dbSt.close();
con.close();
} catch (SQLException e) {
System.out.println("SQLException : "+e.getMessage()); } // catch 문 } // main} // 프로그램 종료
JDBC JDBC 프로그래밍프로그래밍
데이터 수정 예제 결과
JDBC JDBC 프로그래밍프로그래밍
데이터 수정후 데이터베이스
JDBC JDBC 프로그래밍프로그래밍
데이터 삭제 예제앞 예제의 테이블에서 ID=‘abc12’ 인 학생의 데이터를 삭제하는
예제
JDBC JDBC 프로그래밍프로그래밍 DeleteData.java
import java.sql.*;
public class DeleteData {
public static void main(String[] args) {
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// System.err.println("JDBC-ODBC 드라이버를 정상적으로 로드함 ");
} catch(java.lang.ClassNotFoundException e) {
System.err.println(" 드라이버 로드에 실패했습니다 .");
}
DeleteData.java (계속 )
try {
Connection con = DriverManager.getConnection("jdbc:odbc:student");
Statement dbSt = con.createStatement();
System.out.println("JDBC 드라이버가 정상적으로 연결되었습니다 .");
String strSql = "DELETE FROM student_info WHERE ID = ‘abc12’ ";
dbSt.executeUpdate(strSql);
System.out.println("데이터 삭제 완료 ");
dbSt.close();
con.close();
} catch (SQLException e) {
System.out.println("SQLException : "+e.getMessage()); } // catch 문 } // main} // 프로그램 종료
JDBC JDBC 프로그래밍프로그래밍
데이터 삭제 예제 결과
JDBC JDBC 프로그래밍프로그래밍
데이터 삭제후 데이터베이스
JDBC JDBC 프로그래밍프로그래밍 GUI 를 통한 데이터 삽입 예제
이전에 만든 로그인 ->회원가입 창에 앞에서 만든
student 데이터베이스를 연동하여 데이터를 삽입하는 예제
JDBC JDBC 프로그래밍프로그래밍 InsertDataFromGUI.java
import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Login extends JFrame implements ActionListener {
Container ct = getContentPane();
JTextField id; JPasswordField passwd;
Login(String title) { // 생성자
setTitle(title);
JLabel l1 = new JLabel("LoginID :");
id = new JTextField (8);
InsertDataFromGUI.java 계속
l1.setBounds(20, 60, 70, 30);
id.setBounds(100, 60, 120, 30);
ct.add(l1); ct.add(id);
JLabel l2 = new JLabel("PASSWORD :");
passwd = new JPasswordField (8);
l2.setBounds(20, 100, 70, 30);
passwd.setBounds(100, 100, 120, 30);
ct.add(l2); ct.add(passwd);
JButton b1 = new JButton(" 로그인 ");
JButton b2 = new JButton(" 취소 ");
b2.addActionListener(this);
JButton b3 = new JButton(" 회원가입 ");
b3.addActionListener(this);
JLabel l3 = new JLabel();
InsertDataFromGUI.java 계속
b1.setBounds(30, 170, 80, 30);
b2.setBounds(120, 170, 80, 30);
b3.setBounds(210, 170, 80, 30);
ct.add(b1); ct.add(b2); ct.add(b3); ct.add(l3);
} // 생성자 끝
public void actionPerformed(ActionEvent ae) {
String s = ae.getActionCommand();
if ( s.equals(" 취소 ")) { // 취소시 id, password 입력값 초기화
id.setText(""); passwd.setText(""); }
else if ( s.equals(" 회원가입 ")) { // 회원가입 창 띄우기
NewMember my = new NewMember(" 회원가입 ");
my.setSize(350, 250);
my.setLocation(400, 300);
my.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
my.show(); }
InsertDataFromGUI.java 계속
else {} // 로그인 클릭시 DB 연동 해보길…
} // actionPerformed
} // Login 클래스 끝
class NewMember extends JFrame implements ActionListener {
JTextField id, name, tel_number, address;
JPasswordField passwd ;
JComboBox tel;
JButton b1, b2 ;
NewMember (String title) {
Container ct = getContentPane();
setTitle(title);
ct.setLayout(new BorderLayout(0, 20));
JPanel top = new JPanel();
top.setLayout(new GridLayout(5, 1));
InsertDataFromGUI.java 계속
JPanel p1 = new JPanel();
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel l1 = new JLabel("ID :");
id = new JTextField (8);
JButton idcheck = new JButton("ID 중복체크 ");
idcheck.addActionListener(this);
p1.add(l1); p1.add(id); p1.add(idcheck);
Panel p2 = new Panel();
p2.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel l2 = new JLabel("PASSWORD :");
passwd = new JPasswordField (8);
p2.add(l2); p2.add(passwd);
InsertDataFromGUI.java 계속
JPanel p3 = new JPanel();
p3.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel l3 = new JLabel(" 이름 :");
name = new JTextField (8);
p3.add(l3); p3.add(name);
JPanel p4 = new JPanel();
p4.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel l4 = new JLabel(" 연락처 :");
tel = new JComboBox();
tel.addItem("010"); tel.addItem("011");
tel_number = new JTextField (10);
p4.add(l4); p4.add(tel); p4.add(tel_number);
InsertDataFromGUI.java 계속
JPanel p5 = new JPanel();
p5.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel l5 = new JLabel(" 주소 :");
address = new JTextField (20);
p5.add(l5); p5.add(address);
top.add(p1); top.add(p2); top.add(p3); top.add(p4); top.add(p5);
ct.add(top, BorderLayout.CENTER);
JPanel bottom = new JPanel();
b1 = new JButton(" 확인 ");
b1.addActionListener(this);
b2 = new JButton(" 취소 ");
bottom.add(b1); bottom.add(b2);
add(bottom, BorderLayout.SOUTH);
} // 회원가입 NewMember 의 생성자 끝
InsertDataFromGUI.java 계속
public void actionPerformed(ActionEvent ae) {
String t_id, t_passwd, t_name, t_tel, t_tel_number, t_address;
String s = ae.getActionCommand();
if (s.equals("ID 중복체크 ")) {
MessageDialog md = new MessageDialog(this, "ID 중복체크 ", true);
md.show();
}
else if(s.equals(" 확인 ")) { // 화면에 입력된 회원가입 정보를 DB 에 삽입
t_id = id.getText();
t_passwd = passwd.getText();
int t_pswd = Integer.parseInt(t_passwd);
t_name = name.getText();
t_tel = (String) tel.getSelectedItem();
t_tel_number = tel_number.getText();
t_address = address.getText();
InsertDataFromGUI.java 계속
try {
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver"); // JDBC Driver 로딩함
} catch(java.lang.ClassNotFoundException e) {
System.err.println(" 드라이버 로드에 실패했습니다 .");
}
try { // DB 연동하기
Connection con = DriverManager.getConnection("jdbc:odbc:student");
Statement dbSt = con.createStatement();
String strSql = "INSERT INTO student_info (ID, password, 이름 , "+
" 연락처 1, 연락처 2, 주소 ) VALUES ( '"+t_id+"', "+t_pswd+
", '"+t_name+"','"+t_tel+"','"+t_tel_number+"', '"+t_address+"' )";
dbSt.executeUpdate(strSql);
System.out.println(" 데이터 삽입 완료 ");
InsertDataFromGUI.java 계속
dbSt.close();
con.close();
} catch (SQLException e) {
System.out.println("SQLException : "+e.getMessage());
} // catch
} // else if
else ;
} // actionPerformed
} // NewMember 클래스 끝
class MessageDialog extends JDialog implements ActionListener { //ID 중복체크
Button ok;
MessageDialog(Frame parent, String title, boolean mode)
{ // 생성자 메소드를 이용하여 다이어로그 박스 구성
super(parent, title, mode); // JDialog(parent, title, mode); 와 동일
InsertDataFromGUI.java 계속
JPanel pc = new JPanel();
JLabel label = new JLabel(" 사용할 수 있는 ID 입니다 ");
pc.add(label);
add(pc, BorderLayout.CENTER);// Panel 을 생성하여 레이블을 추가
JPanel ps = new JPanel();
ok = new Button("OK");
ok.addActionListener(this);
ps.add(ok);
add(ps, BorderLayout.SOUTH); // Panel 을 생성하여 버튼을 추가
pack(); // 컴포넌트를 배치하고 다이어로그 박스의 초기 크기를 설정
} // MessageDialog 의 생성자
public void actionPerformed(ActionEvent ae) {
dispose(); // ok 버튼 클릭하면 창닫기
}
} // MessageDialog 클래스 끝
InsertDataFromGUI.java 계속
class InsertDataFromGUI { // 앞의 Login 클래스를 사용하여 윈도우 객체 생성
public static void main(String args[]) {
Login win = new Login(" 로그인 ");
win.setSize(310, 250);
win.setLocation(200, 200);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.show();
}
} // 프로그램 끝
JDBC JDBC 프로그래밍프로그래밍 GUI 를 통한 데이터 저장 , 취소 , 수정 ,삭제 예제
앞의 로그인 ->회원가입 창에서
데이터를 저장 , 취소 , 수정 , 삭제 , 취소하는 예제
JDBC JDBC 프로그래밍프로그래밍 GUI 를 통한 데이터 저장 , 취소 , 수정 , 삭제 예제 UpdateDataFromGUI.java 프로그램의 구조class UpdateDataFromGUI extends JFrame implements ActionListener {
UpdateDataFromGUI (String title) {
// 화면을 구성하는 객체 생성 , 이벤트 연결 , 화면 출력 }
actionPerformed(ActionEvent ae) {
if ( ID 중복체크 버튼 ) { ID 중복체크 메시지 창 띄우기 }
else if ( 취소 버튼 ) { 화면 CLEAR }
else { // 저장 , 수정 , 삭제인 경우 모두 DB 연동이 필요함 DB 연결 if ( 저장 버튼 ) { 화면에서 입력받은 데이터를 DB 에 저장 }
else if ( 수정버튼 ) { 화면에서 입력받은 ID 의 데이터를 DB 에서 수정 }
else if ( 삭제버튼 ) { 화면에서 입력받은 ID 의 데이터를 DB 에서 삭제 }
else ;
DB 연결 끊기 } // else
}
main(String[] args) { 윈도우 객체 생성 및 창 띄우기 } }
(1)
(2)
(3)
JDBC JDBC 프로그래밍프로그래밍 UpdateDataFromGUI.java
import java.sql.*;
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
class Login extends JFrame implements ActionListener {
Container ct = getContentPane();
JTextField id; JPasswordField passwd;
Login(String title) { // 생성자
setTitle(title);
JLabel l1 = new JLabel("LoginID :");
id = new JTextField (8);
l1.setBounds(20, 60, 70, 30);
UpdateDataFromGUI.java 계속
id.setBounds(100, 60, 120, 30);
ct.add(l1); ct.add(id);
JLabel l2 = new JLabel("PASSWORD :");
passwd = new JPasswordField (8);
l2.setBounds(20, 100, 70, 30);
passwd.setBounds(100, 100, 120, 30);
ct.add(l2); ct.add(passwd);
JButton b1 = new JButton(" 로그인 ");
JButton b2 = new JButton(" 취소 ");
b2.addActionListener(this);
JButton b3 = new JButton(" 회원가입 ");
b3.addActionListener(this);
JLabel l3 = new JLabel();
b1.setBounds(30, 170, 80, 30); b2.setBounds(120, 170, 80, 30);
b3.setBounds(210, 170, 80, 30);
UpdateDataFromGUI.java 계속
ct.add(b1); ct.add(b2); ct.add(b3); ct.add(l3);
} // 생성자 끝
public void actionPerformed(ActionEvent ae) {
String s = ae.getActionCommand();
if ( s.equals(" 취소 ")) {
id.setText("");
passwd.setText(""); }
else if ( s.equals(" 회원가입 ")) {
NewMember my = new NewMember(" 회원가입 ");
my.setSize(350, 250);
my.setLocation(400, 300);
my.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
my.show(); }
else {} // 로그인 DB 연동
}
} // Login 클래스 끝
UpdateDataFromGUI.java 계속
class NewMember extends JFrame implements ActionListener {
JTextField id, name, tel_number, address;
JPasswordField passwd ;
JComboBox tel;
JButton b1, b2, b3, b4, b5 ;
NewMember (String title) {
Container ct = getContentPane();
setTitle(title);
ct.setLayout(new BorderLayout(0, 20));
JPanel top = new JPanel();
top.setLayout(new GridLayout(5, 1));
JPanel p1 = new JPanel();
p1.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel l1 = new JLabel("ID :");
id = new JTextField (8);
UpdateDataFromGUI.java 계속
JButton idcheck = new JButton("ID 중복체크 ");
idcheck.addActionListener(this);
p1.add(l1); p1.add(id); p1.add(idcheck);
Panel p2 = new Panel();
p2.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel l2 = new JLabel("PASSWORD :");
passwd = new JPasswordField (8);
p2.add(l2); p2.add(passwd);
JPanel p3 = new JPanel();
p3.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel l3 = new JLabel(" 이름 :");
name = new JTextField (8);
p3.add(l3); p3.add(name);
JPanel p4 = new JPanel();
p4.setLayout(new FlowLayout(FlowLayout.LEFT));
UpdateDataFromGUI.java 계속
JLabel l4 = new JLabel(" 연락처 :");
tel = new JComboBox();
tel.addItem("010"); tel.addItem("011");
tel_number = new JTextField (10);
p4.add(l4); p4.add(tel); p4.add(tel_number);
JPanel p5 = new JPanel();
p5.setLayout(new FlowLayout(FlowLayout.LEFT));
JLabel l5 = new JLabel(" 주소 :");
address = new JTextField (20);
p5.add(l5); p5.add(address);
top.add(p1); top.add(p2); top.add(p3); top.add(p4);
top.add(p5);
ct.add(top, BorderLayout.CENTER);
JPanel bottom = new JPanel();
b1 = new JButton(" 저장 ");
b2 = new JButton(" 취소 ");
UpdateDataFromGUI.java 계속
b3 = new JButton(" 조회 ");
b4 = new JButton(" 수정 ");
b5 = new JButton(" 삭제 ");
b1.addActionListener(this);
b2.addActionListener(this);
b4.addActionListener(this);
b5.addActionListener(this);
bottom.add(b1); bottom.add(b2);
bottom.add(b3); bottom.add(b4); bottom.add(b5);
add(bottom, BorderLayout.SOUTH);
}
UpdateDataFromGUI.java 계속
public void actionPerformed(ActionEvent ae) {
String t_id, t_passwd, t_name, t_tel, t_tel_number, t_address;
String strSql="";
String s = ae.getActionCommand();
if (s.equals("ID 중복체크 ")) {
MessageDialog md = new MessageDialog(this, "ID 중복체크 ", true);
md.show();
}
else if(s.equals(" 취소 ")) {
id.setText(""); passwd.setText(""); name.setText("");
tel_number.setText(""); address.setText("");
}
else { // 저장 , 수정 , 삭제일 경우 DB 연동을 먼저 해야하므로 묶어줌
try { Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
} catch(java.lang.ClassNotFoundException e) {
System.err.println(" 드라이버 로드에 실패했습니다 ."); }
UpdateDataFromGUI.java 계속
try { Connection con = DriverManager.getConnection("jdbc:odbc:student");
Statement dbSt = con.createStatement();
t_id = id.getText();
t_passwd = passwd.getText();
int t_pswd = Integer.parseInt(t_passwd);
t_name = name.getText();
t_tel = (String) tel.getSelectedItem();
t_tel_number = tel_number.getText();
t_address = address.getText();
if(s.equals(" 저장 ")) { // DB 에 회원가입 데이터 삽입
strSql = "INSERT INTO student_info (ID, password, 이름 , 연락처 1, “+
“ 연락처 2, 주소 ) VALUES ( '"+ t_id+"', "+t_pswd+", '"+t_name+"', '"+t_tel+
"', '"+t_tel_number+"', '"+t_address+"')";
}
UpdateDataFromGUI.java 계속
else if (s.equals(" 수정 ")) { // DB 에서 해당 ID, password 갖는 회원 정보 수정
strSql = "UPDATE student_info SET 이름 = '"+t_name+"', 연락처1='"+t_tel+
"', 연락처 2='"+t_tel_number+"', 주소 ='"+t_address+"' WHERE ID='"+t_id+"'";
}
else if (s.equals(" 삭제 ")) { // DB 에서 해당 ID, password 갖는 회원 정보 삭제
strSql = "DELETE FROM student_info WHERE ID='"+t_id+"'";
}
else ;
dbSt.executeUpdate(strSql);
System.out.println(" 데이터베이스 수정 완료 ");
dbSt.close(); con.close();
} catch (SQLException e) {
System.out.println("SQLException : "+e.getMessage()); }
} // else
} // actionPerformed
} // NewMember 클래스 끝
UpdateDataFromGUI.java 계속
class MessageDialog extends JDialog implements ActionListener {
// 다이어로그 창을 정의한 클래스
Button ok;
MessageDialog(Frame parent, String title, boolean mode)
{ super(parent, title, mode); // JDialog(parent, title, mode); 와 동일
JPanel pc = new JPanel();
JLabel label = new JLabel(" 사용할 수 있는 ID 입니다 ");
pc.add(label);
add(pc, BorderLayout.CENTER);// Panel 을 생성하여 레이블을 추가
JPanel ps = new JPanel();
ok = new Button("OK");
ok.addActionListener(this);
ps.add(ok);
add(ps, BorderLayout.SOUTH); // Panel 을 생성하여 버튼을 추가
pack(); // 컴포넌트를 배치하고 다이어로그 박스의 초기 크기를 설정
}
UpdateDataFromGUI.java 계속
public void actionPerformed(ActionEvent ae) {
dispose(); // ok 버튼 클릭하면 창닫기
}
} // MessageDialog 클래스 끝
class UpdateDataFromGUI { // 앞의 Login 클래스를 사용하여 윈도우 객체 생성
public static void main(String args[]) {
Login win = new Login(" 로그인 ");
win.setSize(310, 250);
win.setLocation(200, 200);
win.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
win.show();
}
}
JDBC JDBC 프로그래밍프로그래밍 GUI 를 통한 데이터 저장 , 취소 , 조회 , 수정 ,삭제 예제
앞의 로그인 ->회원가입 창에서
데이터를 저장, 취소, 조회, 수정, 삭제, 취소하는 예제