728x90
1. PreparedStatement 방식
package com.test01;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class MTest06 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String url = "jdbc:oracle:thin:@localhost:1521:xe";
String user = "kh";
String password = "kh";
Connection con = null;
try {
con = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("삭제할 부서 번호 : ");
int deptno = sc.nextInt();
String sql = " DELETE FROM DEPT "
+ " WHERE DEPTNO = ? ";
PreparedStatement pstm = null;
try {
pstm = con.prepareStatement(sql);
pstm.setInt(1, deptno);
int res = pstm.executeUpdate();
if(res > 0) {
System.out.println("삭제 성공");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
//5.
try {
pstm.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
2. Statement 방식
package com.test01;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
public class MTest06 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
try {
Class.forName("oracle.jdbc.driver.OracleDriver");
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
String url = "jdbc:oracle:thin:@localhost:1521:xe";
String user = "kh";
String password = "kh";
Connection con = null;
try {
con = DriverManager.getConnection(url, user, password);
} catch (SQLException e) {
e.printStackTrace();
}
System.out.println("삭제할 부서 번호 : ");
int deptno = sc.nextInt();
String sql = " DELETE FROM DEPT "
+ " WHERE DEPTNO = " + deptno;
Statement stmt = null;
try {
stmt = con.createStatement();
int res = stmt.executeUpdate(sql);
if(res > 0) {
System.out.println("삭제 성공");
}
} catch (SQLException e) {
e.printStackTrace();
}finally {
try {
stmt.close();
con.close();
} catch (SQLException e) {
e.printStackTrace();
}
}
}
}
728x90
'Java 관련 > JDBC' 카테고리의 다른 글
[JDBC] MVC 패턴(Model View Controller) (0) | 2022.03.06 |
---|---|
[JDBC] update(데이터 수정) (0) | 2022.03.04 |
[JDBC] selectOne(선택 출력하기) (0) | 2022.03.02 |
[JDBC] Statement & PreparedStatement (0) | 2022.03.01 |
[JDBC] JDBC(Java DataBase Connectivity)연결순서 (0) | 2022.02.28 |