JAVA

210903_이클립스 콘솔창 입력으로 오라클 출력(scanner,insert,select,delete,update)

요옫 2021. 9. 3. 12:42

public class HelloDmlEx {

 

//새로운 db만들어서 dml테스트

 

//connect 클래스 생성

DbConnTest db=new DbConnTest();

 

//insert

public void insert() {

 

//insert를 오라클말고 이클립스 콘솔창에서 입력

Scanner sc=new Scanner(System.in);

String name,addr;

String sql="";

 

System.out.println("이름 입력");

name=sc.nextLine();

System.out.println("주소 입력");

addr=sc.nextLine();

 

//name과 addr이 string, 실제 이름이기에 소따음표로 감싸주기

sql="insert into hello values(seq_hello.nextval, '"+name+"','"+addr+"',sysdate)";

 

System.out.println(sql);  //에러 뜨게 될 경우 확인하는 용도

 

//db연결

Connection conn=null;

 

//statement 연결.. insert이기에 resultset 필요없음

Statement stmt=null;

 

conn=db.getCloudOracle();

try {

stmt=conn.createStatement();  //sql문 전송에 필요한 stmt를 createStatement 통해서 얻어주겠다는 의미

 

//sql문 실행..insert는 executeUpdate메서드 사용

stmt.executeUpdate(sql);  //sql문 업데이트

 

System.out.println("***추가됨***");  //추가됐는지 확인용

 

} catch (SQLException e) {

 

} finally {

db.dbClose(stmt, conn);

}

}

 

 

//select

public void select() {

 

System.out.println("시퀀스\t이름\t주소\t날짜");

System.out.println("=========================================");

String sql="select * from hello order by num";

 

//db연결

Connection conn=null;

Statement stmt=null;

ResultSet rs=null;

 

conn=db.getCloudOracle();

 

try {

stmt=conn.createStatement();

rs=stmt.executeQuery(sql);

 

while(rs.next())

{

//rs의 next() 통해서 다음행 선택, get메서드를 이용해서 테이블의 컬럼값 얻는다

System.out.println(rs.getInt("num")+"\t"+rs.getString("name")+"\t"+rs.getString("addr")+"\t"+rs.getDate("sdate"));

}

 

} catch (SQLException e) {

 

} finally {

db.dbClose(rs, stmt, conn);

}

}

 

 

//delete

public void delete() {

 

//조건은 삭제할 num 입력시 삭제

//이름,주소,날짜는 중복이 있을수도 있기에 

Scanner sc=new Scanner(System.in);

String num="";

String sql="";

 

System.out.println("삭제할 번호 입력");

num=sc.nextLine();

 

sql="delete from hello where num="+num;

System.out.println(sql);  //출력창 확인용

 

Connection conn=null;

Statement stmt=null;

 

//1.conn

conn=db.getCloudOracle();

//2.stmt

try {

stmt=conn.createStatement();

 

//sql문 실행

int a=stmt.executeUpdate(sql);  //성공한 레코드 개수

 

//안 해도 되지만 혹시 null값 있을까 확인

if(a==0) //없는 번호 삭제시 0 반환

System.out.println("없는 데이터 번호");

else //삭제되면 1이 반환

System.out.println("삭제 완료");

 

} catch (SQLException e) {

 

} finally {

db.dbClose(stmt, conn);

}

}

 

 

//수정

public void update() {

 

//수정할 번호를 입력후 이름,주소 수정

Scanner sc=new Scanner(System.in);

 

String name,addr,num;

String sql="";

 

System.out.println("수정할 번호 입력");

num=sc.nextLine();

 

System.out.println("수정할 이름 입력");

name=sc.nextLine();

 

System.out.println("수정할 주소 입력");

addr=sc.nextLine();

 

sql="update hello set name='"+name+"', addr='"+addr+"' where num="+num;

 

System.out.println(sql);

 

Connection conn=null;

Statement stmt=null;

 

conn=db.getCloudOracle();

try {

stmt=conn.createStatement();

 

int a=stmt.executeUpdate(sql); //수정됐는지 확인용

if(a==0)

System.out.println("수정할 데이터 존재하지 않음");

else 

System.out.println("수정 완료");

 

} catch (SQLException e) {

} finally {

db.dbClose(stmt, conn);

}

}

 

 

public static void main(String[] args) {

HelloDmlEx hello=new HelloDmlEx();

 

Scanner sc=new Scanner(System.in);

int n=0;

 

while(true)

{

System.out.println("***Oracle DB 연습***");

System.out.println("1.insert   2.select   3.delete   4.update   9.exit");

 

n=Integer.parseInt(sc.nextLine());  //버퍼 방지

 

if(n==1)

hello.insert();

else if(n==2)

hello.select();

else if(n==3)

hello.delete();

else if(n==4)

hello.update();

else if(n==9)

{

System.out.println("프로그램 종료");

break;

}

}

}

}