JAVA_37_Statement vs PreparedStatement_JDBC(오라클)

hyeong taek jo·2023년 7월 15일

JAVA

목록 보기
37/39

📌Statement vs PreparedStatement의 차이점

  • statement가 아닌 PreparedStatement 쓰는 이유는 보안 때문이다. 전문적인 용어로는 스니핑을 위해서 사용한다.
  • 사용방법이 조금 다르다.
  • PreparedStatement에 직접 sql문을 작성한다.

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;

public class OraSelect1 {

	public static void main(String[] args) throws SQLException {
		Scanner sc = new Scanner(System.in);
		System.out.println("부서코드를 입력하세요");
		int deptno = sc.nextInt();
		
		String driver = "oracle.jdbc.driver.OracleDriver";
		String url    = "jdbc:oracle:thin:@localhost:1521:xe";
		String sql = "Select dname, loc FROM Dept Where deptno=" + deptno;
		                                                  //deptno가 PK라서 한 행만 나온다.
		Connection conn = null;
		Statement stmt = null;
		ResultSet rs = null;
		  // 받을 값이 있다고하면  ResultSet으로 반드시 받아야 한다.
		
		System.out.println("sql-> " + sql);
		
		try {
			Class.forName(driver);
			conn = DriverManager.getConnection(url,"scott","tiger");
			stmt = conn.createStatement();  // stmt
			rs   = stmt.executeQuery(sql);  // Select 문장은 executeQuery를 써야함
			
			if (rs.next()) {
				String dname = rs.getString("dname"); // rs.getString(1) <-- dname
				String loc   = rs.getString(2);       // rs.getString(2) <-- loc
				                         //위에 "loc"이라고 적어도 된다.
				System.out.println("부서번호 :" + deptno);
				System.out.println("부서명 :" + dname);
				System.out.println("위치 :" + loc);
			} else {
				System.out.println("자료가 없습니다.");
			}
		} catch (Exception e) {
			System.out.println(e.getMessage());
		} finally {
			if( rs != null) rs.close();
			if( stmt != null) stmt.close();
			if( conn != null) conn.close();
		}
		sc.close();
	}
}

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

// Insert가 아닌 PreparedStatement 쓰는 이유는 보안 때문이다
// 전문적인 용어로는 스니핑을 위해서 사용한다.

public class Oraprepare {

public static void main(String[] args) throws SQLException {
	
	Scanner sc = new Scanner(System.in);
	System.out.println("입력할 Oracle 부서코드 ?");  String deptno  = sc.nextLine();
	System.out.println("입력할 Oracle 부서명 ?");  String dname  = sc.nextLine();
	System.out.println("입력할 Oracle 근무지 ?");  String loc  = sc.nextLine();
	
	Connection conn = null;
	PreparedStatement pstmt = null;
	
	String driver = "oracle.jdbc.driver.OracleDriver";
	String url    = "jdbc:oracle:thin:@localhost:1521:xe";
	String sql    = "Insert Into dept values(?, ?, ?)";
	                                        //3개의 컬럼이 들어간다는걸 암시
	
	try {
		Class.forName(driver);
		conn = DriverManager.getConnection(url,"scott","tiger");
		pstmt = conn.prepareStatement(sql);
		pstmt.setString(1, deptno);
		pstmt.setString(2, dname);
		pstmt.setString(3, loc);
		
		int result = pstmt.executeUpdate(); // 입력/수정/삭제시 -> executeUpdate
		                    // statement와 다르게 ( )안에 sql을 안넣는다 그 이유는
		                    //위에 prepareStatement에 spl을 넣어서
		if (result > 0) System.out.println("OraPrepare 입력성공 ^^");
		else 		    System.out.println("OraPrepare 입력성공 ^^");
	} catch (Exception e) {
		System.out.println(e.getMessage());
	} finally {
		if (pstmt != null) pstmt.close();
		if (conn != null) conn.close();
	}
	sc.close();
}

}

profile
마포구 주민

0개의 댓글