java

JSP - ๊ฒŒ์‹œํŒ ๋งŒ๋“ค๊ธฐ(6) PrepareStatement๋ฅผ ์ด์šฉํ•œ CRUD ํ…Œ์ŠคํŠธ

dev_summer 2020. 11. 9. 15:34

 

1.CRUD

(1) C -> Create -> insert -> HTTP(POST)

(2) R -> Read -> select -> HTTP(GET)

(3) U -> Update - > update -> HTTP(PUT)

(4) D -> Delete -> delete -> HTTP(DELETE)

 

2.User CRUD ํ…Œ์ŠคํŠธ

(1) model/User.java

 

package com.cos.board.model;

import java.sql.Timestamp;

// model, vo(value object), bean(์ฝฉ)
public class User {
	private int id;
	private String username;
	private String password;
	private String email;
	private Timestamp createTime;
	
	// All Constructor
	public User(int id, String username, String password, String email, Timestamp createTime) {
		this.id = id;
		this.username = username;
		this.password = password;
		this.email = email;
		this.createTime = createTime;
	}

	// Getter, Setter
	public int getId() {
		return id;
	}

	public void setId(int id) {
		this.id = id;
	}

	public String getUsername() {
		return username;
	}

	public void setUsername(String username) {
		this.username = username;
	}

	public String getPassword() {
		return password;
	}

	public void setPassword(String password) {
		this.password = password;
	}

	public String getEmail() {
		return email;
	}

	public void setEmail(String email) {
		this.email = email;
	}

	public Timestamp getCreateTime() {
		return createTime;
	}

	public void setCreateTime(Timestamp createTime) {
		this.createTime = createTime;
	}
		
}

(2)test/UserDaoTest.java

 

 

package com.cos.board.test;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import com.cos.board.db.DButil;
import com.cos.board.model.User;

// User Test
// DAO -> data Access object : ๋ฐ์ดํ„ฐ๋ฒ ์ด์Šค์— ์ ‘์†ํ•ด์„œ 
public class UserDaoTest {
	public int save() {
		// 1. Stream ์—ฐ๊ฒฐ
		Connection conn = DButil.getConnection();
		PreparedStatement pstmt = null;
		try {
			// 2. ์ฟผ๋ฆฌ ๋ฉ”์‹œ์ง€ ์ „์†ก ํด๋ž˜์Šค(๊ทœ์•ฝ์— ๋งž๊ฒŒ), sql์€ ์ฟผ๋ฆฌ
			final String SQL = "INSERT INTO user(username, password, email, createTime) VALUES(?,?,?,now())";
			pstmt = conn.prepareStatement(SQL);
			// 3. SQL๋ฌธ ์™„์„ฑํ•˜๊ธฐ
			pstmt.setString(1, "love");
			pstmt.setString(2, "1234");
			pstmt.setString(3, "love@nate.com");
			// 4. SQL๋ฌธ ์ „์†ก write,read์˜ ์ „์†ก ๋ฐฉ๋ฒ•์ด ๋‹ค๋ฅด๋‹ค. ํ”„๋กœํ† ์ฝœ์ด 2๊ฐœ ํ•„์š”. executeQuery๋Š” ๊ฒฐ๊ณผ๋ฅผ ์•Œ๋ ค๋‹ฌ๋ผ๋Š” ๋ฌธ,
			// executeUpdate๋Š” DB์— ๊ธฐ๋กํ•˜๊ณ  return์ด ํ•„์š”์—†๋Š” ๋ฌธ
			// pstmt.executeQuery();
			int result = pstmt.executeUpdate();// PSMT๊ฐ€ ๋‹ซํž๋•Œ DB์— ์ž๋™์œผ๋กœ ์ปค๋ฐ‹ ํ•ด์คŒ.

			return result;

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				conn.close();
				// conn.commit(); ์›๋ž˜๋Š” ์ž๋™์œผ๋กœ ์ปค๋ฐ‹๋˜๋‚˜ ๋‘๊ฐ€์ง€์˜ ์ผ์ด ํ•œ๊ฐœ์˜ ํŠธ๋žœ์žญ์…˜์ผ๋•Œ๋Š” ๋”ฐ๋กœ ์ปค๋ฐ‹ํ•ด์•ผํ•จ
				pstmt.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return -1;
	}

	public int update() {
		// 1. Stream ์—ฐ๊ฒฐ
		Connection conn = DButil.getConnection();
		PreparedStatement pstmt = null;
		try {
			// 2. ์ฟผ๋ฆฌ ๋ฉ”์‹œ์ง€ ์ „์†ก ํด๋ž˜์Šค(๊ทœ์•ฝ์— ๋งž๊ฒŒ), sql์€ ์ฟผ๋ฆฌ
			final String SQL = "UPDATE user SET password=? where id=?";
			pstmt = conn.prepareStatement(SQL);
			// 3. SQL๋ฌธ ์™„์„ฑํ•˜๊ธฐ
			pstmt.setString(1, "5678");
			pstmt.setInt(2, 2); // where์ ˆ์—๋Š” ์œ ์ผํ•œ ๊ฐ’, pk๋‚˜ index, ๋ฒ”์œ„๊ฐ€ ๊ฐ€์žฅ ์ž‘์„์ˆ˜์žˆ๋Š” ๊ฐ’ ์‚ฌ์šฉ
			// ๋น„ํŠธ๋งต ์ธ๋ฑ์Šค : ์ €์žฅํ• ๋•Œ ๊ตฐ์ง‘ํ™”ํ•˜์—ฌ ์ €์žฅํ•˜๋ฉด where์ ˆ์— ๋น ๋ฅด๊ฒŒ ์ฐพ์„์ˆ˜์ž‡๊ฒŒ ์ฒ˜๋ฆฌํ•  ์ˆ˜ ์žˆ์Œ.
			// 4. SQL๋ฌธ ์ „์†ก write,read์˜ ์ „์†ก ๋ฐฉ๋ฒ•์ด ๋‹ค๋ฅด๋‹ค. ํ”„๋กœํ† ์ฝœ์ด 2๊ฐœ ํ•„์š”. executeQuery๋Š” ๊ฒฐ๊ณผ๋ฅผ ์•Œ๋ ค๋‹ฌ๋ผ๋Š” ๋ฌธ,
			// executeUpdate๋Š” DB์— ๊ธฐ๋กํ•˜๊ณ  return์ด ํ•„์š”์—†๋Š” ๋ฌธ
			// pstmt.executeQuery();
			int result = pstmt.executeUpdate();// PSMT๊ฐ€ ๋‹ซํž๋•Œ DB์— ์ž๋™์œผ๋กœ ์ปค๋ฐ‹ ํ•ด์คŒ.

			return result;

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				conn.close();
				// conn.commit(); ์›๋ž˜๋Š” ์ž๋™์œผ๋กœ ์ปค๋ฐ‹๋˜๋‚˜ ๋‘๊ฐ€์ง€์˜ ์ผ์ด ํ•œ๊ฐœ์˜ ํŠธ๋žœ์žญ์…˜์ผ๋•Œ๋Š” ๋”ฐ๋กœ ์ปค๋ฐ‹ํ•ด์•ผํ•จ
				pstmt.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return -1;
	}

	public int delete() {
		// 1. Stream ์—ฐ๊ฒฐ
		Connection conn = DButil.getConnection();
		PreparedStatement pstmt = null;
		try {
			// 2. ์ฟผ๋ฆฌ ๋ฉ”์‹œ์ง€ ์ „์†ก ํด๋ž˜์Šค(๊ทœ์•ฝ์— ๋งž๊ฒŒ), sql์€ ์ฟผ๋ฆฌ
			final String SQL = "delete from user where id= ?";
			pstmt = conn.prepareStatement(SQL);
			// 3. SQL๋ฌธ ์™„์„ฑํ•˜๊ธฐ
			pstmt.setInt(1, 1);
			int result = pstmt.executeUpdate();// PSMT๊ฐ€ ๋‹ซํž๋•Œ DB์— ์ž๋™์œผ๋กœ ์ปค๋ฐ‹ ํ•ด์คŒ.

			return result;

		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				conn.close();
				// conn.commit(); ์›๋ž˜๋Š” ์ž๋™์œผ๋กœ ์ปค๋ฐ‹๋˜๋‚˜ ๋‘๊ฐ€์ง€์˜ ์ผ์ด ํ•œ๊ฐœ์˜ ํŠธ๋žœ์žญ์…˜์ผ๋•Œ๋Š” ๋”ฐ๋กœ ์ปค๋ฐ‹ํ•ด์•ผํ•จ
				pstmt.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return -1;
	}

	public List<User> findAll() {
		// 0. ์ปฌ๋ ‰์…˜ ๋งŒ๋“ค๊ธฐ ALL, ์—ฌ๋Ÿฌ๊ฑด์„ returnํ• ๋•Œ ๊ผญ ํ•„์š”
		List<User> users = new ArrayList<>();

		// 1. Stream ์—ฐ๊ฒฐ
		Connection conn = DButil.getConnection();
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			// 2. ์ฟผ๋ฆฌ ๋ฉ”์‹œ์ง€ ์ „์†ก ํด๋ž˜์Šค(๊ทœ์•ฝ์— ๋งž๊ฒŒ), sql์€ ์ฟผ๋ฆฌ
			final String SQL = "select * from user";
			pstmt = conn.prepareStatement(SQL);
			rs = pstmt.executeQuery();// resultset์€ ๊ฒฐ๊ณผ์— ์ง„์ž…ํ•˜๊ธฐ์œ„ํ•œ ์ฃผ์†Œ๊ฐ’์„ ๊ฐ€๋ฆฌํ‚ค๊ณ ์žˆ์Œ.
			while(rs.next()) {
				int id = rs.getInt("id"); // ์ปฌ๋Ÿผ ๋„ค์ž„
				String username = rs.getString("username");
				String password = rs.getString("password");
				String email = rs.getString("email");
				Timestamp createTime = rs.getTimestamp("createTime");
				
				User user = new User(id, username, password, email, createTime);
				users.add(user);
			}

			return users;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				conn.close();
				// conn.commit(); ์›๋ž˜๋Š” ์ž๋™์œผ๋กœ ์ปค๋ฐ‹๋˜๋‚˜ ๋‘๊ฐ€์ง€์˜ ์ผ์ด ํ•œ๊ฐœ์˜ ํŠธ๋žœ์žญ์…˜์ผ๋•Œ๋Š” ๋”ฐ๋กœ ์ปค๋ฐ‹ํ•ด์•ผํ•จ
				pstmt.close();
				rs.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
		}
		return null;
	}

	public User findById() {
		// 1. Stream ์—ฐ๊ฒฐ
		Connection conn = DButil.getConnection();
		PreparedStatement pstmt= null;
		ResultSet rs = null;
		try {
			// 2. ์ฟผ๋ฆฌ ์ „์†ก ํด๋ž˜์Šค (๊ทœ์•ฝ์— ๋งž๊ฒŒ)
			final String SQL = "SELECT * FROM user WHERE id = ?";
			pstmt = conn.prepareStatement(SQL);
			// 3. SQL๋ฌธ ์™„์„ฑํ•˜๊ธฐ
			pstmt.setInt(1, 2);
			// 4. SQL๋ฌธ ์ „์†ก
			rs = pstmt.executeQuery();
			
			User user = null;
			if(rs.next()) {
				int id = rs.getInt("id"); // ์ปฌ๋Ÿผ ๋„ค์ž„
				String username = rs.getString("username");
				String password = rs.getString("password");
				String email = rs.getString("email");
				Timestamp createTime = rs.getTimestamp("createTime");
				
				user = new User(id, username, password, email, createTime);
			}
			
			return user;
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				pstmt.close();
				rs.close();
				conn.close();
			} catch (Exception e) {
				e.printStackTrace();
			}
			
		}
		return null;
	}
}

 

 

(3)test/UserControllerTest.java

 

package com.cos.board.test;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.cos.board.action.Action;
import com.cos.board.action.board.BoardFactory;
import com.cos.board.db.DButil;
import com.cos.board.model.User;

@WebServlet("/test")
public class UserControllerTest extends HttpServlet {
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		doPost(req, resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// DB Test
		UserDaoTest t4 = new UserDaoTest();
//		int result = t4.save();
//		System.out.println("reault : " + result);
//
//		int result = t4.update();
//		System.out.println("reault : " + result);
//
//		int result = t4.delete();
//		System.out.println("reault : " + result);
//
//		List<User> users = t4.findAll();
//		for (User user : users) { // forEach๋ฌธ
//			System.out.println("id : " + user.getId());
//			System.out.println("username : " + user.getUsername());
//			System.out.println("password : " + user.getPassword());
//			System.out.println("email : " + user.getEmail());
//			System.out.println("createTime : " + user.getCreateTime());
//			System.out.println();
//		}

		User user = t4.findById();
		System.out.println("id : " + user.getId());
		System.out.println("username : " + user.getUsername());
		System.out.println("password : " + user.getPassword());
		System.out.println("createTime : " + user.getCreateTime());
		System.out.println("");

	}

}

 

 

 

 

3.MVC์˜ ์—ญํ• 

C - ์š”์ฒญ์„ ๋ฐ›์Œ

M - ํ•„์š”ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ controller์—๊ฒŒ ์ „๋‹ฌ

 

 

 

 

 

==========================================================

 

 

 

* ์ฐธ๊ณ ์‚ฌํ•ญ๋“ค

 

ํŒฉํ† ๋ฆฌ : ๋‚˜ ๋Œ€์‹  ๊ฐ์ฒด ๋งŒ๋“ค์–ด ์ฃผ๋Š” ํด๋ž˜์Šค๋“ค

 

https://stackoverflow.com/

 

Stack Overflow - Where Developers Learn, Share, & Build Careers

Stack Overflow | The World’s Largest Online Community for Developers

stackoverflow.com

๋ชจ๋“  ์–ธ์–ด์˜ ์˜ค๋ฅ˜ ์„œ์น˜

๋ฐฐํฌ ์‹œ web.xml์˜ ๋ฒ„์ „์ด 2.4 3.1๋“ฑ์˜ ๋ฒ„์ „์— ๋”ฐ๋ผ ์„œ๋ฒ„์™€ ์—ฐ๋™์ด ์•ˆ๋ ์ˆ˜๋„ ์žˆ๋‹ค.

์„œ๋ฒ„์˜ ํ†ฐ์บฃ๋ฒ„์ „์— ๋”ฐ๋ผ ๋‚˜์˜ ๋ฒ„์ „์„ ๋ฐ”๊ฟ”์ฃผ์–ด์•ผํ•จ.

 

์•„ํŒŒ์น˜์˜ ์˜ค๋ผํด๋ถ€๋ถ„์—์„œ ์ž๋ฐ”์ฝ”๋“œ ๋ณต์‚ฌํ•˜๊ธฐ

 

 

 

 

 

์˜ˆ์™ธ์ฒ˜๋ฆฌ ๋ฐ ์ž„ํฌํŠธ๋ชฉ๋ก

 

 

 

 

 

ํ†ฐ์บฃ์˜ ์—ญํ• 

 

 

์„œ๋กœ ์•ฝ์†๋˜์ง€์•Š์€ ๋ฉ”์‹œ์ง€๋ฅผ ๋ณด๋‚ด๋ฉด ๊ทœ์•ฝ, ์•ฝ์† ์ฆ‰ ํ”„๋กœํ† ์ฝœ์ด ์—†๋‹ค.

 

์ฟผ๋ฆฌ๋ฅผ ๋งจ ์ฒ˜์Œ ๋‚ ๋ฆฌ๋ฉด ๋ฉ”ํƒ€๋ฐ์ดํ„ฐ์˜ ๋ฐ์ดํ„ฐ๋”•์…”๋„ˆ๋ฆฌ๋กœ ์ฒซ๋ฒˆ์จฐ๋กœ ๋ฐฉ๋ฌธํ•จ.

๋ฉ”ํƒ€๋ฐ์ดํ„ฐ๋Š” ๋ชจ~๋“  ๋ฐ์ดํ„ฐ๋ฅผ ๋“ค๊ณ ์žˆ๋Š” ๋ฌธ์„œ, ์–ด๋–ค ๋ฐ์ดํ„ฐ๊ฐ€ ์ง์ ‘์ ์œผ๋กœ ๋ณด์ด๋Š”๊ฒƒ์ด ์•„๋‹Œ emp{empno, empname{ ์ด๋ผ๋Š” ์ •๋ณด๊ฐ€ ์žˆ๋‹ค๋ผ๋Š” ์–ด๋–ค ์ •๋ณด๊ฐ€ ์žˆ๋Š”์ง€๋ฅผ ์‚ฌ์ง„์ฒ˜๋Ÿผ ์ €์žฅํ•ด๋†“์Œ. ์Šค๋ƒ…์ƒท

prepareStmt์€ where์„ ? ๋ณ€์ˆ˜๋กœ ์‚ฌ์šฉํ•˜๊ธฐ๋•Œ๋ฌธ์— ์†๋„๊ฐ€ ๋น ๋ฅด๋‹ค.

 

ํŠธ๋žœ์žญ์…˜ : ์ผ์ด ์ฒ˜๋ฆฌ ๋‹จ์œ„ ex.๊ณ„์ขŒ์ด์ฒด

 

ORM : ๋‚ด๊ฐ€ ๋งŒ๋“  ์˜ค๋ธŒ์ ํŠธ์— ๋”ฐ๋ผ์„œ ๋งŒ๋“ค์–ด์ง