일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 |
Tags
- ★
- menuid
- TBL
- 유니티
- 서버용량
- 프로세스용량
- 관리자페이지랑연결
- db백업
- 환경구축
- Ajax
- 메뉴출판
- 메뉴에추가
- 출처 케이디
- 티스토리챌린지
- DB
- ibatis
- ERWin
- 오류사전
- 참고url
- 데이터삽입
- 오블완
- 관리자페이지
- sqlmap
- 파이썬
- ajaxController
- counseltype
- isNotEmpty
- querybox
- 참고 url
- 쿼리박스
Archives
- Today
- Total
핑핑핑크젤리
JDBC 본문
반응형
* 오라클 시퀀스 : 자동으로 1씩
Main에 다 때려박음
package com.ST;
import java.net.ConnectException;
import java.sql.Connection;
import java.sql.Driver;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Connection con = null;
PreparedStatement pst = null;
PreparedStatement pst2 = null;
ResultSet rs=null;
String name = null;
String phone = null;
String email = null;
int age = 0;
int stnum = 0;
String name2 = null;
String url = "jdbc:oracle:thin:@localhost:1521:xe";
String user = "madang";
String pass = "madang";
System.out.println("==========학생관리 프로그램 ==========\r\n");
while (true) {
System.out.println("1.학생추가 2.전체명단조회 3.특정학생조회 4.학생정보수정 5.학생삭제 6.프로그램종료");
System.out.print("메뉴선택>>");
int memu = sc.nextInt();
switch (memu) {
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
case 1:
System.out.println("1번 선택");
System.out.println("등록할 학생의 정보를 입력하시오.");
System.out.print("이름 : ");
name = sc.next();
System.out.print("나이 : ");
age = sc.nextInt();
System.out.print("전화번호 : ");
phone = sc.next();
System.out.print("이메일 : ");
email = sc.next();
try {
// 1. JDBC로딩
Class.forName("oracle.jdbc.driver.OracleDriver");
// 2. DB연결
con = DriverManager.getConnection(url, user, pass);
// 3. SQL작성
String sql = "insert into student values(stSeq.nextval,?, ?, ?, ?)";// ? : 바인드 변수로 들어감
// 4. 바인드 변수 채우기 / 여기 인덱스는 1부터 시작함
pst = con.prepareStatement(sql);
pst.setString(1, name);
pst.setInt(2, age);
pst.setString(3, phone);
pst.setString(4, email);
// 4. SQL 실행처리
// excuteUpdate => insert, update, delete
int cnt = pst.executeUpdate();
if (cnt > 0) {
System.out.println("학생추가 성공");
} else {
System.out.println("학생추가 실패");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 아래부터 닫아준다.
try {
pst.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
break;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
case 2:
System.out.println("2번 선택");
try {
// 1. JDBC로딩
Class.forName("oracle.jdbc.driver.OracleDriver");
// 2. DB연결
con = DriverManager.getConnection(url, user, pass);
// 3. SQL작성
String sql = "select * from student";// ? : 바인드 변수로 들어감
pst = con.prepareStatement(sql);
// 4. SQL 실행처리
// executeQuery => select
rs = pst.executeQuery();
while (rs.next()) {
stnum = rs.getInt("stnum");
name = rs.getString("name");
age = rs.getInt("age");
phone = rs.getString("phone");
email = rs.getString("email");
System.out.println("------학생------");
System.out.println("학생넘버 : " + stnum);
System.out.println("이름 : " + name);
System.out.println("나이 : " + age);
System.out.println("전화번호 : " + phone);
System.out.println("이메일 : " + email);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 아래부터 닫아준다.
try {
pst.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
break;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
case 3:
System.out.println("3번 선택");
System.out.println("검색할 이름 : ");
name2 = sc.next();
try {
// 1. JDBC로딩
Class.forName("oracle.jdbc.driver.OracleDriver");
// 2. DB연결
con = DriverManager.getConnection(url, user, pass);
// 3. SQL작성
String sql = "select * from student where name=?";// ? : 바인드 변수로 들어감
pst = con.prepareStatement(sql);
pst.setString(1, name2);
// 4. SQL 실행처리
// executeQuery => select
rs = pst.executeQuery();
while (rs.next()) {
stnum = rs.getInt("stnum");
name = rs.getString("name");
age = rs.getInt("age");
phone = rs.getString("phone");
email = rs.getString("email");
System.out.println("------학생------");
System.out.println("학생넘버 : " + stnum);
System.out.println("이름 : " + name);
System.out.println("나이 : " + age);
System.out.println("전화번호 : " + phone);
System.out.println("이메일 : " + email);
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 아래부터 닫아준다.
try {
pst.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
break;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
case 4:
System.out.println("4번 선택");
System.out.println("변경할 학생 이름: ");
name2 = sc.next();
System.out.println("[1]이름 [2]나이 [3]전화번호 [4]이메일 [5]전체 :");
int sel = sc.nextInt();
if (sel == 1) {
System.out.println("변경할 이름 : ");
name = sc.next();
} else if (sel == 2) {
System.out.println("변경할 나이 : ");
age = sc.nextInt();
} else if (sel == 3) {
System.out.println("변경할 전화번호 : ");
phone = sc.next();
} else if (sel == 4) {
System.out.println("변경할 이메일 : ");
email = sc.next();
} else {
System.out.println("변경할 이름 : ");
name = sc.next();
System.out.println("변경할 나이 : ");
age = sc.nextInt();
System.out.println("변경할 전화번호 : ");
phone = sc.next();
System.out.println("변경할 이메일 : ");
email = sc.next();
}
try {
// 1. JDBC로딩
Class.forName("oracle.jdbc.driver.OracleDriver");
// 2. DB연결
con = DriverManager.getConnection(url, user, pass);
// 3. SQL작성
if (sel == 1) {
String sql = "update student set name =? where name =?";// ? : 바인드 변수로 들어감
pst = con.prepareStatement(sql);
pst.setString(1, name);
pst.setString(2, name2);
} else if (sel == 2) {
String sql = "update student set age =? where name =?";// ? : 바인드 변수로 들어감
pst = con.prepareStatement(sql);
pst.setInt(1, age);
pst.setString(2, name2);
} else if (sel == 3) {
String sql = "update student set phone =? where name =?";// ? : 바인드 변수로 들어감
pst = con.prepareStatement(sql);
pst.setString(1, phone);
pst.setString(2, name2);
} else if (sel == 4) {
String sql = "update student set email =? where name =?";// ? : 바인드 변수로 들어감
pst = con.prepareStatement(sql);
pst.setString(1, email);
pst.setString(2, name2);
} else {
String sql = "update student set name =? , age =?, phone =?, email=?where name =?";// ? : 바인드
// 변수로 들어감
pst = con.prepareStatement(sql);
pst.setString(1, name);
pst.setInt(2, age);
pst.setString(3, phone);
pst.setString(4, email);
pst.setString(5, name2);
}
// 4. SQL 실행처리
// excuteUpdate => insert, update, delete
int cnt = pst.executeUpdate();
if (cnt > 0) {
System.out.println("학생수정 성공");
} else {
System.out.println("학생수정 실패");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 아래부터 닫아준다.
try {
pst.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
break;
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
case 5:
System.out.println("5번 선택");
System.out.print("삭제할 학생의 이름을 입력하시오>> ");
name2 = sc.next();
try {
// 1. JDBC로딩
Class.forName("oracle.jdbc.driver.OracleDriver");
// 2. DB연결
con = DriverManager.getConnection(url, user, pass);
// 3. SQL작성
String sql = "delete from student where name=?";// ? : 바인드 변수로 들어감
// String sql2 ="SELECT stSeq.CURRVAL FROM DUAL";//현재 시퀀스 값
// String sql2 ="SELECT stSeq.CURRVAL FROM DUAL";//다음 시퀀스 값
// 4. 바인드 변수 채우기 / 여기 인덱스는 1부터 시작함
pst = con.prepareStatement(sql);
pst.setString(1, name2);
// rs = pst.executeQuery();
// stnum = rs.getInt("stnum");
// 4. SQL 실행처리
// excuteUpdate => insert, update, delete
int cnt = pst.executeUpdate();
if (cnt > 0) {
System.out.println("학생삭제 성공");
} else {
System.out.println("학생삭제 실패");
}
} catch (Exception e) {
e.printStackTrace();
} finally {
// 아래부터 닫아준다.
try {
pst.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
break;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (memu == 6) {
break;
}
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
}
}
}
반응형
'💖빅데이터 과정💖' 카테고리의 다른 글
파이썬 (2022.07.15) (0) | 2022.07.21 |
---|---|
디자인 패턴 (어댑터) (0) | 2022.07.11 |
이클립스 사용자 정의 자동완성 만들기 (0) | 2022.07.07 |
자바 (2022.07.07) (0) | 2022.07.07 |
자바 (2022.07.06) (0) | 2022.07.06 |