PreparedStatement
유저입력받기
Scanner sc = new Scanner(System.in); System.out.println("ID : "); String inputId = sc.nextLine(); System.out.println("PWD : "); String inputPwd = sc.nextLine(); System.out.println("NICK : "); String inputNick = sc.nextLine();
SQL 준비
String sql= "INSERT INTO MEMBER(ID, PWD, NICK) VALUES( ? , ?, ? )";
SQL 실행을 위한 statement 준비
PreparedStatement pstmt = conn.prepareStatement(sql); pstmt.setString(1, inputId); pstmt.setString(2, inputPwd); pstmt.setString(3, inputId);
statement 에 SQL 담아주고 실행 및 결과 리턴받기
int result = pstmt.executeUpdate();
결과 출력
System.out.println("쿼리 실행 결과 : " + result);
기존의 DML을 자바에서 사용했던 코드와 같지만 몇 가지 다른 점이 있다.
'PreparedStatement'
'SQL 준비' 단계에서 INSERT할 VALUES의 값이 ?로 되어있다.
PreparedStatement는 앞에서 ?로 지정한 갚을 뒤에서 작성해 줄 수 있다.
방법은 setString(index1, index2)메소드를 사용한다. index1는 ?의 위치(순서), index2는 값을 넣어주면 된다.