CRUD (Create, Read, Update, Delete)

Joy๐ŸŒฑยท2023๋…„ 1์›” 23์ผ
0

๐Ÿ˜ JDBC

๋ชฉ๋ก ๋ณด๊ธฐ
2/3
post-thumbnail

๐Ÿ’โ€โ™€๏ธ CRUD๋ž€,
๋Œ€๋ถ€๋ถ„์˜ ์ปดํ“จํ„ฐ ์†Œํ”„ํŠธ์›จ์–ด๊ฐ€ ๊ฐ€์ง€๋Š” ๊ธฐ๋ณธ์ ์ธ ๋ฐ์ดํ„ฐ ์ฒ˜๋ฆฌ ๊ธฐ๋Šฅ์ธ Create(์ƒ์„ฑ), Read(์ฝ๊ธฐ), Update(๊ฐฑ์‹ ), Delete(์‚ญ์ œ)๋ฅผ ๋ฌถ์–ด์„œ ์ผ์ปซ๋Š” ๋ง

  • ์‚ฌ์šฉ์ž ์ธํ„ฐํŽ˜์ด์Šค๊ฐ€ ๊ฐ–์ถ”์–ด์•ผ ํ•  ๊ธฐ๋Šฅ(์ •๋ณด์˜ ์ฐธ์กฐ/๊ฒ€์ƒ‰/๊ฐฑ์‹ )์„ ๊ฐ€๋ฆฌํ‚ค๋Š” ์šฉ์–ด๋กœ์„œ๋„ ์‚ฌ์šฉ

๐Ÿ“Œ ์ดˆ๊ธฐ Setting

โ—ผ Setting

โ—ผ menu-query.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
	<!--  menu ํ…Œ์ด๋ธ”์— menu 1๊ฐœ๋ฅผ ์‚ฝ์ž…ํ•˜๋Š” ์ฟผ๋ฆฌ -->
	<entry key="insertMenu">
		INSERT
			INTO TBL_MENU
		(
			MENU_CODE
		,	MENU_NAME
		, 	MENU_PRICE
		,	CATEGORY_CODE
		,	ORDERABLE_STATUS
		)
		VALUES
		(
			SEQ_MENU_CODE.NEXTVAL <!-- ์‹œํ€€์Šค๋กœ MENU_CODE ์ปฌ๋Ÿผ ๊ฐ’ ๊ณ„์† ์ƒ์„ฑ -->
		,	?
		,	?
		,	?
		,	?		
		)
	</entry>
	
	<!--  ๋ฉ”๋‰ด ์ฝ”๋“œ ์ž…๋ ฅ ๋ฐ›์•„ ๋ฉ”๋‰ด๋ช…, ๋ฉ”๋‰ด๊ฐ€๊ฒฉ ๋ณ€๊ฒฝํ•˜๋Š” ์ฟผ๋ฆฌ -->
	<entry key="updateMenu">
		UPDATE
				TBL_MENU
			SET MENU_NAME = ?
			,	MENU_PRICE = ?
			WHERE MENU_CODE = ? <!-- ์–ด๋–ค ๋ฉ”๋‰ด ์ฝ”๋“œ๋ฅผ ๊ฐ€์ง€๋Š” ๊ฒƒ์— ๋Œ€ํ•ด์„œ๋งŒ NAME๊ณผ PRICE๋ฅผ update -->	
	</entry>
	
	<!-- ๋ฉ”๋‰ด ์ฝ”๋“œ ์ž…๋ ฅ ๋ฐ›์•„ ํ•ด๋‹น ํ–‰ ์‚ญ์ œํ•˜๋Š” ์ฟผ๋ฆฌ -->
	<entry key="deleteMenu">
		DELETE
			FROM TBL_MENU
			WHERE MENU_CODE = ?
	</entry>
</properties>

โ—ผ JDBCTemplate

public class JDBCTemplate { 
	
	/*  Connection์„ return์‹œํ‚ฌ ๋ฉ”์†Œ๋“œ */ 
	public static Connection getConnection() {
		
		Connection conn = null;
		Properties prop = new Properties();
		
		try {
			prop.load(new FileReader("config/jdbc-config.properties")); // config๋ถ€ํ„ฐ ์ž‘์„ฑ
			
			String driver = prop.getProperty("driver");
			String url = prop.getProperty("url");
			
			Class.forName(driver);
			
			conn = DriverManager.getConnection(url, prop);
			
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		} 
		
		return conn;
	}
	/* Connection์„ closeํ•  ๋ฉ”์†Œ๋“œ */
	public static void close(Connection conn) {
		try {
			if(conn != null && !conn.isClosed()) {
				conn.close(); 
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/* Statement๋ฅผ closeํ•  ๋ฉ”์†Œ๋“œ */
	public static void close(Statement stmt) {
		try {
			if(stmt != null && !stmt.isClosed()) {
				stmt.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/* ResultSet์„ closeํ•  ๋ฉ”์†Œ๋“œ */
	public static void close(ResultSet rset) {
		try {
			if(rset != null && !rset.isClosed()) {
				rset.close();
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
}

๐Ÿ‘€ Insert

๐Ÿ‘‰ MenuDTO

package com.greedy.model.dto;

public class MenuDTO {

	/* TBL_MENU ์™€ ๋™์ผํ•˜๊ฒŒ ํ•„๋“œ ์ž‘์„ฑ */
	/* 
    MENU_CODE	NUMBER
	MENU_NAME	VARCHAR2(30 BYTE)
	MENU_PRICE	NUMBER
	CATEGORY_CODE	NUMBER
	ORDERABLE_STATUS	CHAR(1 BYTE)
    */
	
	private int menuCode;
	private String menuName;
	private int menuPrice;
	private int categoryCode;
	private String orderableStatus;
	
	/* ์ƒ์„ฑ์ž */
	public MenuDTO() {}
	
	public MenuDTO(int menuCode, String menuName, int menuPrice, int categoryCode, String orderableStatus) {
		super();
		this.menuCode = menuCode;
		this.menuName = menuName;
		this.menuPrice = menuPrice;
		this.categoryCode = categoryCode;
		this.orderableStatus = orderableStatus;
	}

	
	/* getter & setter */
	public int getMenuCode() {
		return menuCode;
	}

	public void setMenuCode(int menuCode) {
		this.menuCode = menuCode;
	}

	public String getMenuName() {
		return menuName;
	}

	public void setMenuName(String menuName) {
		this.menuName = menuName;
	}

	public int getMenuPrice() {
		return menuPrice;
	}

	public void setMenuPrice(int menuPrice) {
		this.menuPrice = menuPrice;
	}

	public int getCategoryCode() {
		return categoryCode;
	}

	public void setCategoryCode(int categoryCode) {
		this.categoryCode = categoryCode;
	}

	public String getOrderableStatus() {
		return orderableStatus;
	}

	public void setOrderableStatus(String orderableStatus) {
		this.orderableStatus = orderableStatus;
	}
	
	@Override
	public String toString() {
		return "MenuDTO [menuCode=" + menuCode + ", menuName=" + menuName + ", menuPrice=" + menuPrice
				+ ", categoryCode=" + categoryCode + ", orderableStatus=" + orderableStatus + "]";
	}
}

๐Ÿ‘‰ ์‚ฌ์šฉ์ž ์ž…๋ ฅ ๊ฐ’์œผ๋กœ menu ์ถ”๊ฐ€

Scanner sc = new Scanner(System.in);
System.out.print("๋ฉ”๋‰ด ์ด๋ฆ„ : ");
String menuName = sc.nextLine();
System.out.print("๋ฉ”๋‰ด ๊ฐ€๊ฒฉ : ");
int menuPrice = sc.nextInt();
System.out.print("์นดํ…Œ๊ณ ๋ฆฌ ์ฝ”๋“œ : ");
int categoryCode = sc.nextInt();
sc.nextLine();
System.out.print("ํŒ๋งค ์—ฌ๋ถ€(Y/N) : ");
String orderableStatus = sc.nextLine().toUpperCase(); 
>>> toUpperCase() : ์†Œ๋ฌธ์ž๊ฐ€ ์ž…๋ ฅ๋˜์–ด๋„ ๋Œ€๋ฌธ์ž๋กœ ๋ณ€ํ™˜
		
MenuDTO newMenu = new MenuDTO();
newMenu.setMenuName(menuName);
newMenu.setMenuPrice(menuPrice);
newMenu.setCategoryCode(categoryCode);
newMenu.setOrderableStatus(orderableStatus);

/*-----------------------------------------------------------------------*/
// ์œ„ ์•„๋ž˜๋ฅผ ๋‹ค๋ฅธ ๋ฉ”์†Œ๋“œ๋กœ ๋งŒ๋“ ๋‹ค๋ฉด? 
// ๊ฐ’์„ ๋ญ‰์ณ์„œ ์ „์†กํ•˜๊ธฐ ์œ„ํ•ด MenuDTO์— ๊ฐ’์„ ๋‹ด๊ณ  ์ „์†กํ•œ๋‹ค๊ณ  ๊ฐ€์ • */
/*-----------------------------------------------------------------------*/
		
Connection conn = getConnection();
PreparedStatement pstmt = null;

>>> INSERT ๊ตฌ๋ฌธ์„ ์‹คํ–‰ ํ•  ์˜ˆ์ •์ด๋ฏ€๋กœ ResultSet์€ ํ•„์š”์—†์Œ 
>>> (ResultSet์€ ์กฐํšŒ๋œ ๊ฒฐ๊ณผ ์ง‘ํ•ฉ์ด๋ฏ€๋กœ SELECT์™€ ์“ฐ์ž„)
>>> DML(INSERT, UPDATE, DELETE)๊ตฌ๋ฌธ์€ ์ˆ˜ํ–‰๋œ ๊ฒฐ๊ณผ์˜ ํ–‰์˜ ๊ฐฏ์ˆ˜(int)๋ฅผ ๋ฐ˜ํ™˜

int result = 0;
		
Properties prop = new Properties();
		
try {
	prop.loadFromXML(new FileInputStream("mapper/menu-query.xml"));
	String query = prop.getProperty("insertMenu");
			
	pstmt = conn.prepareStatement(query);
    // ์œ„์น˜ํ™€๋”์— ์ž…๋ ฅ๋ฐ›์€ ๋ณ€์ˆ˜๋“ค ์ „๋‹ฌ (DTO์ด์šฉ)
	pstmt.setString(1, newMenu.getMenuName()); 
	pstmt.setInt(2, newMenu.getMenuPrice());
	pstmt.setInt(3, newMenu.getCategoryCode());
	pstmt.setString(4, newMenu.getOrderableStatus());
	
    result = pstmt.executeUpdate(); 
    // ์œ„์— int result = 0; ์„ ์–ธ ํ›„ result์— ์‹คํ–‰๋ฌธ ์ €์žฅ
    >>> SELECT์‹œ์—๋Š” executeQuery() : ResultSet
	>>> INSERT, UPDATE, DELETE์‹œ์—๋Š” executeUpdate() : int
			
} catch (IOException e) {
	e.printStackTrace();
} catch (SQLException e) {
	e.printStackTrace();
} finally {
	close(pstmt);
	close(conn);
}
		
if(result > 0) {
	System.out.println("๋ฉ”๋‰ด ๋“ฑ๋ก ์™„๋ฃŒ ! :)");			
} else {
	System.out.println("๋ฉ”๋‰ด ๋“ฑ๋ก ์‹คํŒจ :(");
}

๐Ÿ“Œ Ref.

* SELECT์‹œ์—๋Š” ์ˆ˜ํ–‰ ๊ฒฐ๊ณผ๊ฐ€ ResultSet์œผ๋กœ ๋ฐ˜ํ™˜๋˜์ง€๋งŒ INSERT, UPDATE, DELETE์‹œ์—๋Š” 
  ์ˆ˜ํ–‰๊ฒฐ๊ณผ๊ฐ€ ์‚ฝ์ž…/์ˆ˜์ •/์‚ญ์ œ๋œ ํ–‰์˜ ๊ฐฏ์ˆ˜(int)๋กœ ๋ฐ˜ํ™˜
  ๋”ฐ๋ผ์„œ ResertSet ๊ฐ์ฒด ๋Œ€์‹  int ๋ณ€์ˆ˜ ์ƒ์„ฑ ํ›„ executeUpdate()๋กœ ์‹คํ–‰ํ•œ ๊ตฌ๋ฌธ ์ €์žฅ

๐Ÿ‘€ Update

๐Ÿ‘‰ ๋ฉ”๋‰ด ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅ ๋ฐ›์•„ ํ•ด๋‹น ๋ฐ์ดํ„ฐ์˜ ๋ฉ”๋‰ด๋ช…๊ณผ ๋ฉ”๋‰ด ๊ฐ€๊ฒฉ ๋ณ€๊ฒฝ

Scanner sc = new Scanner(System.in);
System.out.print("๋ณ€๊ฒฝํ•  ๋ฉ”๋‰ด ๋ฒˆํ˜ธ : ");
int menuCode = sc.nextInt();
sc.nextLine(); 
System.out.print("๋ณ€๊ฒฝํ•  ๋ฉ”๋‰ด ์ด๋ฆ„ : ");
String menuName = sc.nextLine();
System.out.print("๋ณ€๊ฒฝํ•  ๋ฉ”๋‰ด ๊ฐ€๊ฒฉ : ");
int menuPrice = sc.nextInt();
		
MenuDTO changeMenu = new MenuDTO();
changeMenu.setMenuCode(menuCode);
changeMenu.setMenuName(menuName);
changeMenu.setMenuPrice(menuPrice);
		
Connection conn = getConnection();
PreparedStatement pstmt = null;
int result = 0;

Properties prop = new Properties();
		
try {
	prop.loadFromXML(new FileInputStream("mapper/menu-query.xml"));
			
	String query = prop.getProperty("updateMenu");
			
	pstmt = conn.prepareStatement(query);
     // ์œ„์น˜ํ™€๋”์— ์ž…๋ ฅ๋ฐ›์€ ๋ณ€์ˆ˜๋“ค ์ „๋‹ฌ (DTO์ด์šฉ)
	pstmt.setString(1, changeMenu.getMenuName());
	pstmt.setInt(2, changeMenu.getMenuPrice());
	pstmt.setInt(3, changeMenu.getMenuCode());
		
	result = pstmt.executeUpdate();
			
} catch (IOException e) {
	e.printStackTrace();
} catch (SQLException e) {
	e.printStackTrace();
} finally {
	close(pstmt);
	close(conn);
}
		
if(result > 0) {
	System.out.println("๋ฉ”๋‰ด ๋ณ€๊ฒฝ ์™„๋ฃŒ ! :)");			
} else {
	System.out.println("๋ฉ”๋‰ด ๋ณ€๊ฒฝ ์‹คํŒจ :(");
}

๐Ÿ‘€ Delete

๐Ÿ‘‰ ์ž…๋ ฅ ๋ฐ›์€ ๋ฉ”๋‰ด ์ฝ”๋“œ์— ํ•ด๋‹นํ•˜๋Š” ํ–‰ ์‚ญ์ œ

Scanner sc = new Scanner(System.in);
System.out.print("์‚ญ์ œํ•  ๋ฉ”๋‰ด ์ฝ”๋“œ : ");
int menuCode = sc.nextInt();
		
// ๋ฉ”๋‰ด ์ฝ”๋“œ๋งŒ์„ ๋‹ค๋ฃจ๋ฏ€๋กœ DTO ์‚ฌ์šฉํ•  ํ•„์š” X
		
Connection conn = getConnection();
PreparedStatement pstmt = null;
int result = 0;
		
Properties prop = new Properties();
		
try {
	prop.loadFromXML(new FileInputStream("mapper/menu-query.xml/"));
			
	String query = prop.getProperty("deleteMenu");
			
	pstmt = conn.prepareStatement(query);
	pstmt.setInt(1, menuCode);
			
	result = pstmt.executeUpdate();
			
} catch (IOException e) {
	e.printStackTrace();
} catch (SQLException e) {
	e.printStackTrace();
} finally {
	close(pstmt);
	close(conn);
}
		
if(result > 0) {
	System.out.println("๋ฉ”๋‰ด ์‚ญ์ œ ์™„๋ฃŒ ! :)");
} else {
	System.out.println("๋ฉ”๋‰ด ์‚ญ์ œ ์‹คํŒจ :(");
}

profile
Tiny little habits make me

0๊ฐœ์˜ ๋Œ“๊ธ€