Java Web Development Workbook Chapter. 04
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.40</version>
</dependency>
@Override
public void service(ServletRequest request, ServletResponse response)
throws ServletException, IOException {
Connection conn = null;
Statement stmt = null;
ResultSet rs = null;
try {
DriverManager.registerDriver(new com.mysql.jdbc.Driver());
conn = DriverManager.getConnection("jdbc:mysql://localhost/studydb?useUnicode=true&characterEncoding=UTF-8",
"study", "study");
stmt = conn.createStatement();
rs = stmt.executeQuery(
"select mno, mname, email, cre_date" +
" from members" +
" order by mno asc"
);
...
DriverManager는 이 인터페이스에서 connect 메서드를 통해 DB 연결 수행
java.sql.Connection: DB 접속 정보를 다룸
createStatement, prepareStatement, prepareCall: SQL문을 실행하는 객체 리턴 -> java.sql.Statement 인터페이스의 구현체
commit / rollback: 트랜잭션 처리
java.sql.Statement: SQL문을 서버에 쿼리 날릴 수 있음.
executeQuery: SQL 문을 실행. 보통 select 구문
<!-- filters -->
<filter>
<filter-name>CharacterEncodingFilter</filter-name>
<filter-class>Lesson04.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<!-- filter mapping -->
<filter-mapping>
<filter-name>CharacterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>