본문 바로가기
728x90
728x90

Backend/JDBC7

PreparedStatement 생성 PreparedStatement 생성 PreparedStatement는 쿼리문을 미리 준비하여 컴파일하고, 매번 실행할 때마다 파라미터 값을 전달하여 실행한다. public class Application { public static void main(String[] args) { Connection connection = getConnection(); PreparedStatement statement = null; ResultSet resultSet = null; String query = "SELECT MENU_CODE,NAME,PRICE FROM TBL_MENU"; try { statement = connection.prepareStatement(query); resultSet = statement.. 2023. 6. 13.
JDBC DTO의 개념 JDBC DTO의 개념 데이터베이스의 내용을 조회하고 조회된 내용을 반환받기 위해서는 여러개의 컬럼 값을 전달 받아야한다. 각각의 컬럼을 반환받는 메소드를 만들게 되면 컬럼 수 만큼의 메소드를 추가해야한다. 하지만 함수의 리턴 값은 하나이고 데이터들을 전달 받기 위한 방법이 필요하다. 결과 데이터를 하나로 묶어 반환할 수 있는 방법은 크게 2가지가 있다. 첫 번째로는 HashMap을 사용하는 방법이고 두 번째로는 DTO(Data Transfer Object)를 사용하는 방법이다. 두 방법 모두 key-value 방식으로 동작하며 데이터의 이동을 목적으로 사용할 수 있다. HashMap을 사용하면 별도의 DTO class를 만들 필요없이 데이터를 가져올 수 있다. 하지만 DTO를 사용하는 것이 더 바람직하.. 2023. 6. 12.
JDBC statement와 resultSet 생성 JDBC statement와 resultSet 생성 statement는 데이터베이스에서 실행해야할 쿼리문을 저장하고 실행할 수 있도록 하는 객체이다. statement의 종류에는 statement, preparedStatement, callableStatement가 있다. 일반적인 sql문 실행에는 statement, preparedStatement가 사용되고 callableStatement는 프로시저 호출 시 사용된다. public class Application { public static void main(String[] args) { Connection connection = getConnection(); Statement statement = null; ResultSet resultSet = n.. 2023. 6. 11.
JDBC Template 구현 JDBC Template 구현 prop.load(new FileReader("src/com/forsaken/connection/jdbc-config.properties")); String driver = prop.getProperty("driver"); String url = prop.getProperty("url"); String user = prop.getProperty("user"); String password = prop.getProperty("password"); System.out.printf("%s %s %s %s\n", driver, url, user, password); try { Class.forName(driver); } catch (ClassNotFoundException e) {.. 2023. 6. 10.
728x90
728x90