JSP Servlet 기초(14) PreparedStatement 객체
- JSP
- 2018. 1. 31. 17:49
이번엔 PreparedStatement 객체에
대해 알아보겠습니다
(11)(12)(13) 포스팅에서는
SQL문 실행을 위해 Statement 객체를 이용했습니다
Statement 객체의 경우 중복코드가 많아지는
단점이 있습니다 이러한 단점을
보완한 객체가 PreparedStatement 객체입니다
먼저 간단하게 PreparedStatement 객체
사용법을 보겠습니다
1 2 3 4 5 6 7 8 9 10 | Class.forName(driver); connection = DriverManager.getConnection(url, uid, upw); int n; String query = "insert into member(id, pw, name, phone) values (?,?,?)"; preparedStatement = connection.prepareStatement(query); preparedStatement.setString(1, "abc"); preparedStatement.setString(2, "123"); preparedStatement.setString(3, "홍길동"); n = preparedStatement.executeUpdate(); | cs |
이제 직접 PreparedStatement 객체
를 사용해보도록 하겠습니다
이클립스에서 WebContent에 preInsert.jsp와
preView.jsp를 만들어줍니다.
먼저 preInsert.jsp 보겠습니다
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 | <%@page import="java.sql.PreparedStatement"%> <%@page import="java.sql.DriverManager"%> <%@page import="java.sql.ResultSet"%> <%@page import="java.sql.Connection"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%! Connection connection; PreparedStatement preparedStatement; ResultSet resultSet; String driver = "oracle.jdbc.driver.OracleDriver"; String url = "jdbc:oracle:thin:@localhost:1521:xe"; String uid = "scott"; String upw = "tiger"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% try{ Class.forName(driver); connection = DriverManager.getConnection(url, uid, upw); int n; String query = "insert into member(id, pw, name) values (?, ?, ?)"; preparedStatement = connection.prepareStatement(query); preparedStatement.setString(1, "asd"); preparedStatement.setString(2, "123"); preparedStatement.setString(3, "홍길동1"); n = preparedStatement.executeUpdate(); preparedStatement.setString(1, "zxc"); preparedStatement.setString(2, "456"); preparedStatement.setString(3, "홍길동2"); n = preparedStatement.executeUpdate(); if(n == 1) { out.println("insert success"); } else { out.println("insert fail"); } } catch(Exception e) { e.printStackTrace(); } finally { try{ if(resultSet != null) resultSet.close(); if(preparedStatement != null) preparedStatement.close(); if(connection != null) connection.close(); } catch(Exception e){} } %> <br /> <a href="preView.jsp">회원정보</a> </body> </html> | cs |
10line 에서 PreparedStatement 객체를
선언해줍니다
다음 32line에서 쿼리문작성법이
Statement 객체를 사용할때와 다릅니다
훨씬 간결해지고 쿼리문을 넣기가 편합니다
values (?, ?, ?) ?는 첫번째부터 1번이 됩니다
35line에서 1,2,3 을 차례대로 데이터를
넣어줍니다
다음 preView.jsp 보겠습니다
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 | <%@page import="java.sql.DriverManager"%> <%@page import="java.sql.ResultSet"%> <%@page import="java.sql.Statement"%> <%@page import="java.sql.Connection"%> <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%! Connection connection; Statement statement; ResultSet resultSet; String driver = "oracle.jdbc.driver.OracleDriver"; String url = "jdbc:oracle:thin:@localhost:1521:xe"; String uid = "scott"; String upw = "tiger"; String query = "select * from member"; %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <title>Insert title here</title> </head> <body> <% try{ Class.forName(driver); connection = DriverManager.getConnection(url, uid, upw); statement = connection.createStatement(); resultSet = statement.executeQuery(query); while(resultSet.next()){ String id = resultSet.getString("id"); String pw = resultSet.getString("pw"); String name = resultSet.getString("name"); out.println("아이디 : " + id + ", 비밀번호 : " + pw + ", 이름 : " + name + "<br />"); } } catch(Exception e) { e.printStackTrace(); } finally { try{ if(resultSet != null) resultSet.close(); if(statement != null) statement.close(); if(connection != null) connection.close(); } catch(Exception e){} } %> </body> </html> | cs |
데이터를 불러올때는 기존처럼 똑같이 하시면
불러올수 있습니다.
이제 preInsert.jsp 를 실행시키켜
결과값을 보면
위처럼 제대로 insert된 데이터들을
확인할수 있습니다
'JSP' 카테고리의 다른 글
JSP Servlet 기초(16) 회원인증 (10) | 2018.02.01 |
---|---|
JSP Servlet 기초(15) 커넥션풀 (DBCP) (0) | 2018.01.31 |
JSP Servlet 기초(13) DAO, DTO (1) | 2018.01.31 |
JSP Servlet 기초(12) 회원가입 및 회원정보 수정하기 (0) | 2018.01.31 |
JSP Servlet 기초(11) JDBC (0) | 2018.01.31 |