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();
}
}
}
}