Notice
Recent Posts
Recent Comments
Link
«   2024/06   »
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
more
Archives
Today
Total
관리 메뉴

WON.dev

chapter04 : delete 본문

JAVA/chapter27_preparestatement

chapter04 : delete

GAWON 2023. 5. 31. 09:39
package org.joonzis.ex;

import java.sql.Connection;
import java.sql.PreparedStatement;

import org.joonzis.db.DBConnection;

public class Ex03_delete {
   public static void main(String[] args) {
      
      // 김씨 데이터 삭제
      
      Connection conn = null;
      PreparedStatement ps = null;
      
      try {
         conn = DBConnection.getConnection();
         
         String sql = "delete from person where name = '김씨'";
         
         ps = conn.prepareStatement(sql);
         
         int result = ps.executeUpdate(sql);
         if(result == 1) {
            System.out.println("삭제 성공!");
         }else {
            System.out.println("삭제 실패!");
         }
         
         conn.commit();
         
      }  catch (Exception e) {
         e.printStackTrace();
      }finally {
         try {
            if(conn != null) {conn.close();}
            if(ps != null) {ps.close();}
         } catch (Exception e2) {
            e2.printStackTrace();
         }
      
      
      }

   }
}
package org.joonzis.ex;

import java.sql.Connection;
import java.sql.PreparedStatement;

import org.joonzis.db.DBConnection;

public class Ex03_delete {
   public static void main(String[] args) {
      // 김씨 데이터 삭제
      Connection conn = null;
      PreparedStatement ps = null;
      
      try {
         conn = DBConnection.getConnectine();
         
         String sql = "DELETE PERSON AGE = ?, REGION = ? WHERE NAME = ?";
         ps = conn.prepareStatement(sql);
         
         ps.setInt(1, 100);
         ps.setString(2, "마포");
         ps.setString(3, "김씨");
         
         int result = ps.executeUpdate();
         
         if(result > 0) {
            System.out.println("데이터 삭제 성공!");
         }else {
            System.out.println("데이터 삭제 실패!");
         }
         
         conn.commit();
         
      }catch (Exception e) {
         e.printStackTrace();
         
         try {
            if(conn != null) {
               conn.rollback();
            }
         } catch (Exception e2) {
            e2.printStackTrace();
         }
         
      }finally {
         try {
            if(ps != null) {ps.close();}
            if(conn != null) {conn.close();}
         } catch (Exception e2) {
            e2.printStackTrace();
         }
      }
      
   }
}

'JAVA > chapter27_preparestatement' 카테고리의 다른 글

chapter05 : select  (0) 2023.05.31
chapter03 : update  (0) 2023.05.31
chapter02 : insert  (0) 2023.05.31
chapter01 : DBConnection  (0) 2023.05.31