JDBC - PreparedStatement

웅평·2024년 1월 7일
0

PreparedStatement

  • SQL문 작성시 작은따옴표를 자동으로 처리해 준다.
  • SQL문이 한 번 분석되면 캐시에 저장되기 때문에 재사용이 용이
  • SQL문이 미리 컴파일되기 때문에 Statement 보다 성능상 우위
  • 동적으로 SQL문을 처리할 수 있습니다. 즉, 동일한 SQL문에 다른 매개변수를 설정할 수 있다.
  • SQL injection(보안상 허점을 악용한 SQL문을 실행해 DB를 비정상적으로 조작)을 방어할 수 있다.

기본 사용

  1. SQL문을 작성, 특정 값을 지정해야 하는 위치에 물음표(?)를 표시해 줍
    2 .Connection 객체의 prepareStatement(String sql) 메소드를 이용해 PreparedStatement 객체를 생성, Statement와 달리, 객체 생성시 SQL문을 인자로 전달
  2. PreparedStatement 객체의 setXXX(int parameterIndex, XXX x) 메소드를 이용해 값을 설정, 물음표의 인덱스값(1부터 시작)과 데이터 타입에 맞는 값을 인자로 전달
  3. PreparedStatement 객체의 executeQuery() 또는 executeUpdate() 메소드를 이용해 SQL문을 실행하고 결과값을 받는다. 이때 SQL문을 인자로 전달하지 않는 점을 주의

SELETE

public ArrayList<BoardVO> findAll(){
		ArrayList<BoardVO> list = new ArrayList<BoardVO>();
		String sql = "select * from board";
		try {
			Connection conn = ConnectionProvider.getConnection();
			Statement stmt = conn.createStatement();
			ResultSet rs = stmt.executeQuery(sql);
			while(rs.next()) {
				list.add(new BoardVO(rs.getInt(1), 
						rs.getString(2), 
						rs.getString(3), 
						rs.getString(4), 
						rs.getString(5), 
						rs.getDate(6), 
						rs.getInt(7), 
						rs.getString(8),
						rs.getString(9)));
			}			
			ConnectionProvider.close(conn, stmt, rs);			
		}catch (Exception e) {
			System.out.println("예외발생:"+e.getMessage());
		}
		return list;
	}

INSERT

public int insert(BoardVO b) {
		String sql = "insert into board values(?,?,?,?,?,sysdate,1,?,?)";
		int re = -1;
		try {
			Connection conn = 
			ConnectionProvider.getConnection();
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, b.getNo());
			pstmt.setString(2, b.getTitle());
			pstmt.setString(3, b.getWriter());
			pstmt.setString(4, b.getPwd());
			pstmt.setString(5, b.getContent());
			pstmt.setString(6, b.getFname());
			pstmt.setString(7, b.getIp());
			re = pstmt.executeUpdate();
			ConnectionProvider.close(conn, pstmt);
			
		}catch (Exception e) {
			System.out.println("예외발생:"+e.getMessage());
		}
		return re;
	}

UPDATE

public int update(BoardVO b) {
		int re = -1;
		String sql = 
		"update board set title=?,content=?,fname=? where no=? and pwd=?";
		try {		
			Connection conn =ConnectionProvider.getConnection();			
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setString(1, b.getTitle());
			pstmt.setString(2, b.getContent());
			pstmt.setString(3, b.getFname());
			pstmt.setInt(4, b.getNo());
			pstmt.setString(5, b.getPwd());
			re = pstmt.executeUpdate();
			ConnectionProvider.close(conn, pstmt);		
		}catch (Exception e) {
			System.out.println("예외발생:"+e.getMessage());
		}		
		return re;
	}

DELETE

public int delete(int no, String pwd) {
		int re = -1;
		String sql = "delete board where no=? and pwd=?";
		try {
			Connection conn = ConnectionProvider.getConnection();
			PreparedStatement pstmt = conn.prepareStatement(sql);
			pstmt.setInt(1, no);
			pstmt.setString(2, pwd);;
			re = pstmt.executeUpdate();
			ConnectionProvider.close(conn, pstmt);
		}catch (Exception e) {
			System.out.println("예외발생:"+e.getMessage());
		}
		return re;
	}

참고
https://amy-it.tistory.com/62

0개의 댓글