본문 바로가기

콩's EDUCATION/콩's JAVA_RUN

update sql (PreparedStatement)

PreparedStatement 을 이용한 update 문


 

 

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;

public class PreUpdateSQLTest {
 public static void main(String[] args) {
  
  Connection con = null;
  try{
  Class.forName("oracle.jdbc.driver.OracleDriver");
  // 사용할 드라이버 로드한다.
  con = DriverManager.getConnection
    ("jdbc:oracle:thin:@127.0.0.1:1521:xe","scott","tiger");
  System.out.println("정상적으로 연결 되었습니다. ");
  Scanner input = new Scanner(System.in);
  System.out.println("수정할 샐러리 입력하세요 : ");
  Double salary_inc = input.nextDouble();
  System.out.println("수정할 부서 번호 입력하세요 : ");
  int deptno = input.nextInt();
  System.out.println("수정할 이름 입력하세요 : ");
  String name = input.next();
  
  // 1. sql 수정 (키보드 입력 3개 데이터 : ?)
  String updateSql = "update emp"
      +" set sal = sal *?"
     +", deptno = ?"
     +" where ename like ?";     

  PreparedStatement st = con.prepareStatement(updateSql);
  // 2. setter 사용 : 입력 파라미터값 전송
  
  st.setDouble(1,salary_inc);
  st.setDouble(2,deptno);
  st.setString(3,name);

  
  con.close();
  System.out.println("정상적으로 연결해제 되었습니다.");
  }catch(ClassNotFoundException e){
   System.out.println ("jdbc 드라이버 미등록이거나 이름 오류입니다.");
  }catch(SQLException e){
   e.printStackTrace();
   System.out.println("연결정보 오류");
  }finally{
   try{
   if(!con.isClosed()) con.close();
   }catch(SQLException e){
    System.out.println("finally 오류");
   }
  }
 }
}

 

결과 개인 확인

 

 

PreUpdateSQLTest.java