다른거 다 하고
추가 :
-> window - java - compiler - jdk 맞게 바꾸기
< JavaBean.txt >
DB데이터 처리
ORACLE 정상 작동 여부
제어판 - 관리도구 - 서비스
C:\oraclexe\app\oracle\product\11.2.0\server\network\ADMIN
build path - libraries - add Jars - ojdbc
package dto;
import java.sql.Date;
/*
CREATE TABLE member(
NAME VARCHAR2(20) NOT NULL,
AGE NUMBER,
HEIGHT NUMBER(10,2),
LOGTIME DATE
);
*/
public class MemberDTO {
private String name;
private int age;
private double height;
private Date date;
public MemberDTO() {}
public MemberDTO(String name, int age, double height) {
super();
this.name = name;
this.age = age;
this.height = height;
this.date = date;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public double getHeight() {
return height;
}
public void setHeight(double height) {
this.height = height;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
}
package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import dto.MemberDTO;
public class MemberDAO {
// 연결 정보
private String url = "jdbc:oracle:thin:@localhost:1521:xe";
private String id = "dbtest";
private String pwd = "a1234";
public MemberDAO() {
try {
Class.forName("oracle.jdbc.OracleDriver");
System.out.println("로딩 성공");
} catch (Exception e) {
System.out.println("로딩 실패 ㅠㅠ");
e.printStackTrace();
}
}// MemberDAO() end
public Connection getConnection() {
Connection con = null;
try {
// DB 연결 객체 생성
con = DriverManager.getConnection(url,id,pwd);
System.out.println("연결 성공!!");
} catch (Exception e) {
System.out.println("연결 실패~");
e.printStackTrace();
}
return con;
}// getConnection() end
// 추가
public int insert(MemberDTO dto) {
Connection con = null;
PreparedStatement pstmt = null;
int su = 0;
try {
String sql = "INSERT INTO MEMBER VALUES(?,?,?,sysdate)";
con = getConnection();
pstmt = con.prepareStatement(sql);
pstmt.setString(1,dto.getName());
pstmt.setInt(2,dto.getAge());
pstmt.setDouble(3,dto.getHeight());
su = pstmt.executeUpdate(); // 추가 성공한 레코드 수 반환
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(pstmt != null) {
pstmt.close();
}
if(con != null) {
con.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println(su+"개 레코드 추가");
return su;
}// insert() end
public void select() {
Connection con = null;
PreparedStatement pstmt = null;
ResultSet res = null;
try {
String sql = "SELECT * FROM MEMBER";
con = getConnection();
pstmt = con.prepareStatement(sql);
res = pstmt.executeQuery();
if(res == null) {
System.out.println("조회된 데이터가 없습니다.");
} else {
while(res.next()) {
String name = res.getString("name");
int age = res.getInt("age");
double height = res.getDouble("height");
String logtime = res.getString("logtime");
System.out.println(name + "\t" + age + "\t" + height + "\t" + logtime);
}
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(res != null) res.close();
if(pstmt != null) pstmt.close();
if(con != null) con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
} // select() end
public void delete(String name) {
Connection con = null;
PreparedStatement pstmt = null;
int res = 0;
try {
String sql = "DELETE FROM MEMBER WHERE NAME=?";
con = getConnection();
pstmt = con.prepareStatement(sql);
pstmt.setString(1, name);
res = pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// 사용 자원 종료
if (pstmt != null)
pstmt.close();
if (con != null)
con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println(res + "개의 행이 삭제 되었습니다~");
}// delete() end
public int update(MemberDTO dto) {
Connection con = null;
PreparedStatement pstmt = null;
int res = 0;
try {
String sql = "UPDATE MEMBER SET AGE=?, HEIGHT=? WHERE NAME=?";
con = getConnection();
pstmt = con.prepareStatement(sql);
pstmt.setInt(1, dto.getAge());
pstmt.setDouble(2, dto.getHeight());
pstmt.setString(3, dto.getName());
res = pstmt.executeUpdate();
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(pstmt != null) pstmt.close();
if(con != null) con.close();
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println(res + " 개 행 삭제");
return res;
} // update() end
}
import java.util.Scanner;
import dao.MemberDAO;
import dto.MemberDTO;
public class MemberController {
private Scanner sc = new Scanner(System.in);
private MemberDAO dao;
public MemberController() {
dao = new MemberDAO();
}
// 1.추가 2.삭제 3.수정 4.확인
public void menu() {
while(true) {
System.out.print("1.추가 2.삭제 3.수정 4.확인 >> ");
int select = sc.nextInt();
switch(select) {
case 1:
System.out.println("추가");
System.out.println("이름 입력 >");
String name = sc.next();
System.out.println("나이 입력 >");
int age = sc.nextInt();
System.out.println("키 입력 >");
double height = sc.nextDouble();
dao.insert(new MemberDTO(name, age, height));
break;
case 2:
System.out.println("삭제할놈");
String dname = sc.next();
dao.delete(dname);
break;
case 3:
System.out.println("수정");
System.out.println("이름 입력 >");
String uname = sc.next();
System.out.println("나이 입력 >");
int uage = sc.nextInt();
System.out.println("키 입력 >");
double uheight = sc.nextDouble();
dao.update(new MemberDTO(uname, uage, uheight));
break;
case 4:
dao.select();
break;
case 0:
System.out.println("시스템 종료");
System.exit(0);
default :
System.out.println("다시");
return;
}
}
}// menu() end
}
import java.util.Scanner;
import dao.MemberDAO;
import dto.MemberDTO;
public class MemberControllerT {
private Scanner scanner = new Scanner(System.in);
private MemberDAO dao;
public MemberControllerT() {
dao = new MemberDAO();
}
// 1.추가 2.삭제 3.수정 4.확인
public void menu() {
boolean run = true;
while(run) {
System.out.print("1.추가 2.삭제 3.수정 4.확인\n선택 >> ");
int select = scanner.nextInt();
switch(select) {
case 1: // 추가
System.out.println("--- 회 원 추 가 ---");
MemberDTO inputMember = inputDTO();
if(dao.insert(inputMember) > 0) {
System.out.println(inputMember.getName() + " 회원 등록 완료");
}
break;
case 2: // 삭제
System.out.println("--- 회 원 삭 제 ---");
String dname = deleteName();
if(dao.delete(dname) > 0) {
System.out.println(dname + " 회원 삭제");
} else {
System.out.println("없는 회원입니다~");
}
break;
case 3: // 수정
System.out.println("--- 회 원 수 정 ---");
MemberDTO updateMember = inputDTO();
if(dao.update(updateMember) > 0) {
System.out.println(updateMember.getName() + " 회원 수정 완료");
}
break;
case 4: // 목록
System.out.println("--- 회 원 목 록 ---");
dao.select();
break;
case 0: // 종료
run = false;
break;
default:
System.out.println("선택 오류~");
}
System.out.println();
}
System.out.println("- Progeam end -");
}
// menu() end
// 회원 정보 입력
public MemberDTO inputDTO() {
System.out.print("이름 입력 > ");
String name = scanner.next();
System.out.print("나이 입력 > ");
int age = scanner.nextInt();
System.out.print("키 입력 > ");
double height = scanner.nextDouble();
MemberDTO dto = new MemberDTO(name, age, height);
return dto;
}
// inputDTO() end
// 삭제 이름
public String deleteName() {
System.out.print("이름 입력 > ");
String name = scanner.next();
return name;
}
}
import dao.MemberDAO;
import dto.MemberDTO;
public class MemberMain {
public static void main(String[] args) {
// MemberDAO dao = new MemberDAO();
// dao.getConnection();
//
// dao.insert(new MemberDTO("testA", 21, 123.5));
// dao.insert(new MemberDTO("testB", 21, 123.5));
// dao.insert(new MemberDTO("testC", 21, 123.5));
// dao.select();
// dao.delete("testA");
// dao.select();
// dao.update(new MemberDTO("testC",33, 234.6));
// 1.추가 2.삭제 3.수정 4.확인
// - 필요한 데이터는 Scanner 로 입력받아서 처리합니다
MemberController control = new MemberController();
control.menu();
}
}
package dto;
/*
CREATE TABLE school(
NAME VARCHAR2(15) NOT NULL, -- 이름
VALUE VARCHAR2(15), -- 학생 : 학번, 교수 : 과목명, 관리자 : 부서명
CODE NUMBER -- 관리 코드 : 1.학생 2. 교수 3. 관리자
);
*/
public class SchoolDTO {
private String name;
private String value;
private int code;
public SchoolDTO() {}
public SchoolDTO(String name, String value, int code) {
this.name = name;
this.value = value;
this.code = code;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public int getCode() {
return code;
}
public void setCode(int code) {
this.code = code;
}
}
package dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import dto.SchoolDTO;
public class SchoolDAO {
private String url = "jdbc:oracle:thin:@localhost:1521:xe";
private String id = "dbtest";
private String pwd = "a1234";
public SchoolDAO() {
try {
Class.forName("oracle.jdbc.OracleDriver");
System.out.println("로딩 성공");
} catch (Exception e) {
System.out.println("로딩 실패 ㅠㅠ");
e.printStackTrace();
}
}// SchoolDAO() end
public Connection getConnection() {
Connection con = null;
try {
// DB 연결 객체 생성
con = DriverManager.getConnection(url,id,pwd);
System.out.println("연결 성공!!");
} catch (Exception e) {
System.out.println("연결 실패~");
e.printStackTrace();
}
return con;
}// getConnection() end
// 추가
public boolean insert(SchoolDTO dto) {
Connection con = null;
PreparedStatement pstmt = null;
boolean check = false;
try {
String sql = "INSERT INTO school VALUES(?,?,?)";
con = getConnection();
pstmt = con.prepareStatement(sql);
pstmt.setString(1,dto.getName());
pstmt.setString(2,dto.getValue());
pstmt.setInt(3,dto.getCode());
int su = pstmt.executeUpdate(); // 추가 성공한 레코드 수 반환
if(su > 0) check = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(pstmt != null) {
pstmt.close();
}
if(con != null) {
con.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
System.out.println(dto.getName()+"정보 추가");
return check;
}// insert() end
// 목록
public ArrayList<SchoolDTO> getList() {
ArrayList<SchoolDTO> list = new ArrayList<>();
Connection con = null;
PreparedStatement pstmt = null;
ResultSet res = null;
try {
String sql = "SELECT * FROM SCHOOL";
con = this.getConnection();
pstmt = con.prepareStatement(sql);
res = pstmt.executeQuery();
while(res.next()) {
String name = res.getString("name");
String value = res.getString("value");
int code = res.getInt("code");
list.add(new SchoolDTO(name, value, code));
}
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if(pstmt != null) {
pstmt.close();
}
if(con != null) {
con.close();
}
if(res != null) {
res.close();
}
} catch (Exception e) {
e.printStackTrace();
}
}
if(list.isEmpty()){
list = null;
}
return list;
}// getList() end
}
import java.util.List;
import java.util.Scanner;
import dao.SchoolDAO;
import dto.SchoolDTO;
public class SchoolController {
private Scanner sc = new Scanner(System.in);
private SchoolDAO dao = null;
public SchoolController() {
dao = new SchoolDAO();
menu();
}
public void menu() {
while(true) {
System.out.println("----- 메 뉴 -----");
System.out.print("1. 입력 2. 검색 3. 삭제 4. 목록 5. 종료 \n 선택 >>");
int select = sc.nextInt();
switch(select) {
case 1: // 입력
insert();
break;
// case 2: // 검색
// search();
// break;
// case 3: // 삭제
// delete();
// break;
case 4: // 목록
list();
break;
case 5: // 종료
System.exit(0);
break;
default:
System.out.println("선택 오류~");
}
System.out.println();
}
}// menu() end
// 코드 설정
public int codeInput() {
System.out.println("--- 코 드 선텍 ---");
System.out.println("1. 학생");
System.out.println("2. 교수");
System.out.println("3. 관리자");
System.out.println("4. 이전");
System.out.print("선택 > ");
int code = sc.nextInt();
return code;
}// codeInput() end
public String valueInput(int code) {
if(code == 1) {
System.out.print("학번 입력 > ");
} else if(code == 2) {
System.out.print("과목 입력 > ");
} else {
System.out.print("부서 입력 > ");
}
String value = sc.next();
return value;
}
// valueInput() end
// 추가
public void insert() {
System.out.println("----- 추 가 -----");
int code = codeInput();
if(code < 1 || code > 3) {
System.out.println("이전 메뉴로 이동합니다~");
return;
}
System.out.print("이름 입력 >> ");
String name = sc.next();
String value = valueInput(code);
SchoolDTO dto = new SchoolDTO(name, value, code);
boolean check = dao.insert(dto);
if(check) {
System.out.println(name + "님이 등록 되었습니다");
} else {
System.out.println("등록 실패~");
}
}// insert() end
public void list() {
List<SchoolDTO> list = dao.getList();
System.out.println("----- 목 록 -----");
for(SchoolDTO man : list) {
System.out.println(man.getName() +"\t");
if(man.getCode()==1) {
System.out.println("학번 : " + man.getValue());
}else if(man.getCode()==2) {
System.out.println("과목 : " + man.getValue());
}else{
System.out.println("부서 : " + man.getValue());
}
}
} // list() end
}
public class SchoolMain {
public static void main(String[] args) {
SchoolController sc = new SchoolController();
}
}