1. Statement
package com.haenin.section01.statement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import static com.haenin.common.JDBCTemplate.close;
import static com.haenin.common.JDBCTemplate.getConnection;
public class Application1 {
public static void main(String[] args) {
Connection con = getConnection();
System.out.println("con = " + con); // 필기. DB와의 연동 확인
Statement stmt = null; // 필기. SQL 및 쿼리 실행 결과를 싣고 dbms 오가는 트럭같은 개념
ResultSet rset = null; // 필기. 쿼리(SELECT)의 결과(JAVA의 타입)
try {
stmt = con.createStatement();
rset = stmt.executeQuery("SELECT * FROM EMPLOYEE");
/* 필기. 다중행 조회 결과를 반복하며 추출 */
// 필기. 한 행이 출력
while (rset.next()) {
/* 필기. 반복문에서는 단일행을 하나하나 컬럼별로 꺼내쓴다. */
System.out.println(rset.getString("EMP_NAME"));
}
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
// 필기. 모듈화
/* 필기. 코드의 줄 수를 줄이고 가독성을 높이기 위해 모듈화 */
close(rset);
close(stmt);
close(con);
}
// try {
// rset.close();
// } catch (SQLException e) {
// throw new RuntimeException(e);
// }
// try {
// stmt.close();
// } catch (SQLException e) {
// throw new RuntimeException(e);
// }
//
// try {
// con.close();
// } catch (SQLException e) {
// throw new RuntimeException(e);
// }
// }
}
}
package com.haenin.section01.statement;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Scanner;
import static com.haenin.common.JDBCTemplate.close;
import static com.haenin.common.JDBCTemplate.getConnection;
public class Application2 {
public static void main(String[] args) {
Connection con = getConnection();
Statement stmt = null;
ResultSet rset = null;
/* 설명. Scanner를 활용해 사용자의 입력에 따른 사원 조회 */
Scanner sc = new Scanner(System.in);
System.out.print("조회하고자 하는 사원의 번호를 입력하세요: ");
String empId =sc.nextLine();
try {
stmt = con.createStatement();
// 필기. String이랑 쿼리안에 "" + empId + ""
// 필기. 단점) 문자열로 끊고 이어야 함
rset = stmt.executeQuery("SELECT * FROM EMPLOYEE WHERE EMP_ID = '" + empId + "'");
if(rset.next()){
System.out.println("조회하신 "
+ empId + "반의 사원은 " + rset.getString("EMP_NAME")+"입니다.");
}else{
System.out.println("해당 " + empId + "번의 사원은 존재하지 않습니다.");
}
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
// 필기. 모듈화
/* 필기. 코드의 줄 수를 줄이고 가독성을 높이기 위해 모듈화 */
close(rset);
close(stmt);
close(con);
}
}
}
2. PreparedStatement
package com.haenin.section02.prepared;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import static com.haenin.common.JDBCTemplate.close;
import static com.haenin.common.JDBCTemplate.getConnection;
public class Application1 {
public static void main(String[] args) {
Connection con = getConnection();
PreparedStatement pstmt = null;
ResultSet rset = null;
try {
// 필기. stmt = con.createStatement();
// 필기. 얘는 쿼리를 가지고 객체를 생성
pstmt = con.prepareStatement("SELECT EMP_ID, EMP_NAME FROM EMPLOYEE");
rset = pstmt.executeQuery();
while (rset.next()) {
System.out.println(rset.getString("EMP_ID") + "사번의 "
+ rset.getString("EMP_NAME") +"사원");
}
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
close(rset);
close(pstmt); //필기. 이미 만들어놓은 statement의 자식이기에 다형성으로 메소드 실행
/* 설명. PreparedStatement는 Statement의 자식 클래스라 다형성에 의해 기존 close()활용 가능 */
close(con);
}
}
}
package com.haenin.section02.prepared;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Scanner;
import static com.haenin.common.JDBCTemplate.close;
import static com.haenin.common.JDBCTemplate.getConnection;
public class Application2 {
public static void main(String[] args) {
Connection con = getConnection();
PreparedStatement pstmt = null;
ResultSet rset = null;
Scanner sc = new Scanner(System.in);
System.out.print("조회할 사번을 입력하세요: ");
String empId = sc.nextLine();
System.out.print("퇴사 여부까지 입력하세요: ");
String entYn = sc.nextLine();
try {
/* 설명. ?(placeholder)를 사용하면 1. 가독성 증가(하나의 문자열), 2. 전처리 가능 3. Statement 대비 속도 향상*/
pstmt = con.prepareStatement(
"select * from EMPLOYEE where EMP_ID = ? and ENT_YN = ?");
/* 설명. ?인 placeholder의 위치별로 들어갈 값들은 setter를 이용해 매꿔준다. */
pstmt.setString(1, empId);
pstmt.setString(2, entYn);
rset = pstmt.executeQuery();
if(rset.next()){
System.out.println("입력하신 " + empId +"번의 사원은 "
+ rset.getString("EMP_NAME") + "이고, 월급은 "
+ rset.getInt("SALARY") + "입니다. ");
}
} catch (SQLException e) {
throw new RuntimeException(e);
} finally {
close(rset);
close(pstmt);
close(con);
}
}
}