[JDBC] CRUD Exercise

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

๐Ÿค– Exercise

๋ชฉ๋ก ๋ณด๊ธฐ
1/6
post-thumbnail

๐Ÿงฉ TBL_CATEGORY ํ…Œ์ด๋ธ”์„ ๋Œ€์ƒ์œผ๋กœ ํ•œ CRUD ๊ธฐ๋Šฅ ๋งŒ๋“ค๊ธฐ

๐Ÿ’โ€ Java์™€ Oracle SQL์„ JDBC๋กœ ์—ฐ๋™ํ•˜์—ฌ TBL_CATEGORY ํ…Œ์ด๋ธ”์„ ์กฐํšŒ(SELECT), ํ…Œ์ด๋ธ”์— ๊ฐ’์„ ์‚ฝ์ž…(INSERT), ์ˆ˜์ •(UPDATE), ์‚ญ์ œ(DELETE)ํ•  ์ˆ˜ ์žˆ๋Š” CRUD ๊ธฐ๋Šฅ์„ ๋งŒ๋“ค์–ด์ฃผ์„ธ์š”!


๐Ÿ™‹โ€ ์ž ๊น ! ์ฝ”๋“œ ์ž‘์„ฑ ์ „, ๊ธฐ๋ณธ Setting ํ™•์ธ ๐Ÿ”ฅ์ค‘์š”๐Ÿ”ฅ

  • config/jdbc-config.properties
  • lib/ojdbc8.jar (Classpath add ํฌํ•จ)
  • mapper/category-query.xml
  • src/com/greedy/common/JDBCTemplate.java

๐Ÿ‘‰ ๊ธฐ๋ฐ˜ ํด๋ž˜์Šค ๋ฐ ํŒŒ์ผ

โ—ผ JDBCTemplate Class

public class JDBCTemplate {
	
    /* Connection์„ return ์‹œ์ผœ์ฃผ๋Š” ๋ฉ”์†Œ๋“œ (DB์™€ ์—ฐ๋™ํ•  ๋•Œ๋งˆ๋‹ค ํ˜ธ์ถœ ์˜ˆ์ •) */
	public static Connection getConnection() {
		
	Connection conn = null;
	Properties prop = new Properties();
		
	try {
		prop.load(new FileReader("config/jdbc-config.properties"));
		
        // properties ํŒŒ์ผ์—์„œ ์„ค์ • ์ •๋ณด๋ฅผ ์ฝ์–ด์˜ค๋Š” ์ฝ”๋“œ
		String driver = prop.getProperty("driver");
		String url = prop.getProperty("url");
			
		Class.forName(driver);
		
        // url๊ณผ prop๋งŒ ์ „๋‹ฌํ•˜๋Š” getConnection() ๋ฉ”์†Œ๋“œ ํ˜ธ์ถœ
		conn = DriverManager.getConnection(url, prop);
		>>> Properties์•ˆ์— ์žˆ๋Š” ๊ฐ’์„ ์ „๋‹ฌํ•˜๊ธฐ ๋•Œ๋ฌธ์—,
        >>> user์™€ password๋ฅผ ์œ„์—์„œ ๋”ฐ๋กœ ์ž…๋ ฅํ•˜์ง€ ์•Š์•„๋„ OK
			
	} catch (IOException e) {
		e.printStackTrace();
	} catch (ClassNotFoundException e) {
		e.printStackTrace();
	} catch (SQLException e) {
		e.printStackTrace();
	} 
		
	return conn;
    >>> ์ƒ์„ฑ๋œ Connection์„ ๊ฐ’์œผ๋กœ ๋ฐ˜ํ™˜
}
	
	/* Connection์„ closeํ•  ๋ฉ”์†Œ๋“œ */
    public static void close(Connection conn) {
		try {
			if(conn != null && !conn.isClosed()) { // : conn์ด null์ด ์•„๋‹ˆ๊ณ  ๋‹ซํžˆ์ง€ ์•Š์•˜์„ ๊ฒฝ์šฐ,
				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();
		}
	}
}

โ—ผ CategoryDTO Class

public class CategoryDTO {

	>>> ํ•ด๋‹น ํ…Œ์ด๋ธ”์˜ ์ปฌ๋Ÿผ๋ช…๊ณผ ๋ฐ์ดํ„ฐํƒ€์ž…์„ ๊ธฐ๋ฐ˜์œผ๋กœ ํ•˜์—ฌ ์ž‘์„ฑ
	/* ํ•„๋“œ */
	private int categoryCode;
	private String categoryName;
	private int refCategoryCode;
	
	/* ์ƒ์„ฑ์ž */
	public CategoryDTO() {}
	
	public CategoryDTO(int categoryCode, String categoryName, int refCategoryCode) {
		super();
		this.categoryCode = categoryCode;
		this.categoryName = categoryName;
		this.refCategoryCode = refCategoryCode;
	}

	/* getter & setter */
	public int getCategoryCode() {
		return categoryCode;
	}

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

	public String getCategoryName() {
		return categoryName;
	}

	public void setCategoryName(String categoryName) {
		this.categoryName = categoryName;
	}

	public int getRefCategoryCode() {
		return refCategoryCode;
	}

	public void setRefCategoryCode(int refCategoryCode) {
		this.refCategoryCode = refCategoryCode;
	}

	/* toString */
	@Override
	public String toString() {
		return "CategoryDTO [categoryCode=" + categoryCode + ", categoryName=" + categoryName + ", refCategoryCode="
				+ refCategoryCode + "]";
	}
}

โ—ผ category-query.xml File

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
<properties>
	<!-- TBL_CATEGORY ํ…Œ์ด๋ธ”์„ ๋ชจ๋‘ ์กฐํšŒํ•˜๋Š” ์ฟผ๋ฆฌ -->
	<entry key="selectCategory">
		SELECT
				TC.*
			FROM TBL_CATEGORY TC
	</entry>

	<!-- ์ž…๋ ฅ๋ฐ›์€ ์นดํ…Œ๊ณ ๋ฆฌ ์ฝ”๋“œ์˜ ํ–‰์˜ ๋ชจ๋‘ ์กฐํšŒํ•˜๋Š” ์ฟผ๋ฆฌ -->
	<entry key="selectPickedCategory">
		SELECT
				TC.*
			FROM TBL_CATEGORY TC
			WHERE CATEGORY_CODE = ?
	
	</entry>


	<!--  TBL_CATEGORY ํ…Œ์ด๋ธ”์— ์นดํ…Œ๊ณ ๋ฆฌ 1๊ฐœ๋ฅผ ์‚ฝ์ž…ํ•˜๋Š” ์ฟผ๋ฆฌ -->
	<entry key="insertCategory">
		INSERT
			INTO TBL_CATEGORY
		(
			CATEGORY_CODE
		,	CATEGORY_NAME
		, 	REF_CATEGORY_CODE
		)
		VALUES
		(
			SEQ_CATEGORY_CODE.NEXTVAL <!-- ์‹œํ€€์Šค๋กœ CATEGORY_CODE ์ปฌ๋Ÿผ ๊ฐ’ ๊ณ„์† ์ƒ์„ฑ -->
		,	?
		,	?	
		)
	</entry>
	
	<!-- ์นดํ…Œ๊ณ ๋ฆฌ ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅ ๋ฐ›์•„ ์นดํ…Œ๊ณ ๋ฆฌ๋ช…, ์ƒ์œ„ ์นดํ…Œ๊ณ ๋ฆฌ ์ฝ”๋“œ ๋ณ€๊ฒฝํ•˜๋Š” ์ฟผ๋ฆฌ -->
	<entry key="updateCategory">
		UPDATE
				TBL_CATEGORY
			SET CATEGORY_NAME = ?
			,	REF_CATEGORY_CODE = ?
			WHERE CATEGORY_CODE = ? <!-- ์–ด๋–ค ์นดํ…Œ๊ณ ๋ฆฌ ์ฝ”๋“œ๋ฅผ ๊ฐ€์ง€๋Š” ๊ฒƒ์— ๋Œ€ํ•ด์„œ๋งŒ CATEGORY_NAME๊ณผ REF_CATEGORY_CODE๋ฅผ ์—…๋Žƒํ•˜๊ฒ ๋‹ค. -->	
	</entry>
	
	<!-- ์นดํ…Œ๊ณ ๋ฆฌ ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅ ๋ฐ›์•„ ํ•ด๋‹น ํ–‰ ์‚ญ์ œํ•˜๋Š” ์ฟผ๋ฆฌ -->
	<entry key="deleteCategory">
		DELETE
			FROM TBL_CATEGORY
			WHERE CATEGORY_CODE = ?
	</entry>
</properties>

๐Ÿ™‹โ€ ์ž ๊น ! category-query.xml ํŒŒ์ผ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•œ ์ฝ”๋“œ ์ž‘์„ฑ๋ฒ•

  • ๋จผ์ € ํ”„๋กœ์ ํŠธ ํ•˜์œ„์— mapper ์†Œ์Šคํด๋” ์ƒ์„ฑ
  • ์•„๋ž˜์˜ ์ฝ”๋“œ ์‹คํ–‰ ํ›„, mapper ์†Œ์Šคํด๋” Refreshํ•˜๋ฉด ํŒŒ์ผ ์ƒ์„ฑ ์™„๋ฃŒ
Properties prop = new Properties();
	
		prop.setProperty("keyString", "valueString");
		
		try {
			prop.storeToXML(new FileOutputStream("mapper/category-query.xml"), "");
		} catch (IOException e) {
			e.printStackTrace();
		}

๐Ÿ‘‰ [SELECT] TBL_CATEGORY ํ…Œ์ด๋ธ” ์ „์ฒด ๋ฐ์ดํ„ฐ ์กฐํšŒ

List<CategoryDTO> ํƒ€์ž…์œผ๋กœ ์ฒ˜๋ฆฌ

Connection conn = getConnection();
PreparedStatement pstmt = null;
ResultSet rset = null;
		
CategoryDTO category = null;
List<CategoryDTO> cateList = null;
		
try {
	Properties prop = new Properties();
	prop.loadFromXML(new FileInputStream("mapper/category-query.xml"));
			
	String query = prop.getProperty("selectCategory");
			
	pstmt = conn.prepareStatement(query);
			
	rset = pstmt.executeQuery();
			
	cateList = new ArrayList<>();
			
	while(rset.next()) {
		category = new CategoryDTO();
				
		category.setCategoryCode(rset.getInt("CATEGORY_CODE"));
		category.setCategoryName(rset.getString("CATEGORY_NAME"));
		category.setRefCategoryCode(rset.getInt("REF_CATEGORY_CODE"));
				
		cateList.add(category);
	}
			
} catch (IOException e) {
	e.printStackTrace();
} catch (SQLException e) {
	e.printStackTrace();
} finally {
	close(rset);
	close(pstmt);
	close(conn);
}
		
for(CategoryDTO cate : cateList) {
	System.out.println(cate);
}

๐Ÿ’ป Mini Console

CategoryDTO [categoryCode=1, categoryName=์‹์‚ฌ, refCategoryCode=0]
CategoryDTO [categoryCode=2, categoryName=์Œ๋ฃŒ, refCategoryCode=0]
CategoryDTO [categoryCode=3, categoryName=๋””์ €ํŠธ, refCategoryCode=0]
CategoryDTO [categoryCode=4, categoryName=ํ•œ์‹, refCategoryCode=1]
CategoryDTO [categoryCode=5, categoryName=์ค‘์‹, refCategoryCode=1]
CategoryDTO [categoryCode=6, categoryName=์ผ์‹, refCategoryCode=1]
CategoryDTO [categoryCode=7, categoryName=ํ“จ์ „, refCategoryCode=1]
CategoryDTO [categoryCode=8, categoryName=์ปคํ”ผ, refCategoryCode=2]
CategoryDTO [categoryCode=9, categoryName=์ฅฌ์Šค, refCategoryCode=2]
CategoryDTO [categoryCode=10, categoryName=๊ธฐํƒ€, refCategoryCode=2]
CategoryDTO [categoryCode=11, categoryName=๋™์–‘, refCategoryCode=3]
CategoryDTO [categoryCode=12, categoryName=์„œ์–‘, refCategoryCode=3]

๐Ÿ‘‰ [SELECT] Scanner๋กœ ์ž…๋ ฅ ๋ฐ›์€ Category code์— ํ•ด๋‹นํ•˜๋Š” ํ–‰ ๋ฐ์ดํ„ฐ ์กฐํšŒ

CategoryDTO ํƒ€์ž…์œผ๋กœ ์ฒ˜๋ฆฌ

Scanner sc = new Scanner(System.in);
System.out.println("---------- ์นดํ…Œ๊ณ ๋ฆฌ ์กฐํšŒ ----------");
System.out.print("์กฐํšŒํ•  ํ–‰์˜ ์นดํ…Œ๊ณ ๋ฆฌ ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” :) \n => ");
String code = sc.nextLine();
		
Connection conn = getConnection();
PreparedStatement pstmt = null;
ResultSet rset = null;
		
CategoryDTO category = null;
		
try {
	Properties prop = new Properties();
	prop.loadFromXML(new FileInputStream("mapper/category-query.xml"));
			
	String query = prop.getProperty("selectPickedCategory");
			
	pstmt = conn.prepareStatement(query);
	pstmt.setString(1, code);
			
	rset = pstmt.executeQuery();
			
	while(rset.next()) {
		category = new CategoryDTO();
				
		category.setCategoryCode(rset.getInt("CATEGORY_CODE"));
		category.setCategoryName(rset.getString("CATEGORY_NAME"));
		category.setRefCategoryCode(rset.getInt("REF_CATEGORY_CODE"));
	}

} catch (IOException e) {
	e.printStackTrace();
} catch (SQLException e) {
	e.printStackTrace();
} finally {
	close(rset);
	close(pstmt);
	close(conn);
}
		
System.out.println(category);	

๐Ÿ’ป Mini Console

---------- ์นดํ…Œ๊ณ ๋ฆฌ ์กฐํšŒ ----------
์กฐํšŒํ•  ํ–‰์˜ ์นดํ…Œ๊ณ ๋ฆฌ ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” :) 
 => 3
CategoryDTO [categoryCode=3, categoryName=๋””์ €ํŠธ, refCategoryCode=0]

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

Scanner sc = new Scanner(System.in);
System.out.println("---------- ์นดํ…Œ๊ณ ๋ฆฌ ์ถ”๊ฐ€ ----------");
System.out.print("์นดํ…Œ๊ณ ๋ฆฌ ์ด๋ฆ„์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” :) \n => ");
String cateName = sc.nextLine();
System.out.print("์ƒ์œ„ ์นดํ…Œ๊ณ ๋ฆฌ ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” :) \n => ");
int refCateCode = sc.nextInt();
		
CategoryDTO newCategory = new CategoryDTO();
newCategory.setCategoryName(cateName);
newCategory.setRefCategoryCode(refCateCode);
		
Connection conn = getConnection();
PreparedStatement pstmt = null;
		
int result = 0;
		
try {
	Properties prop = new Properties();
	prop.loadFromXML(new FileInputStream("mapper/category-query.xml"));
	String query = prop.getProperty("insertCategory");
					
	pstmt = conn.prepareStatement(query);
	pstmt.setString(1, newCategory.getCategoryName());
	pstmt.setInt(2, newCategory.getRefCategoryCode());
			
	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("๋ฉ”๋‰ด ๋“ฑ๋ก ์‹คํŒจ :(");
}

๐Ÿ’ป Mini Console

---------- ์นดํ…Œ๊ณ ๋ฆฌ ์ถ”๊ฐ€ ----------
์นดํ…Œ๊ณ ๋ฆฌ ์ด๋ฆ„์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” :) 
 => ๋น„๊ฑด
์ƒ์œ„ ์นดํ…Œ๊ณ ๋ฆฌ ์ฝ”๋“œ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” :) 
 => 1
๋ฉ”๋‰ด ๋“ฑ๋ก ์™„๋ฃŒ ! :)

๐Ÿ’ป TBL_CATEGORY TABLE


๐Ÿ‘‰ [UPDATE] ์‚ฌ์šฉ์ž ์ž…๋ ฅ ๊ฐ’์œผ๋กœ Category ์ˆ˜์ •

Scanner sc = new Scanner(System.in);
System.out.println("---------- ์นดํ…Œ๊ณ ๋ฆฌ ์ˆ˜์ • ----------");
System.out.print("๋ณ€๊ฒฝํ•  ์นดํ…Œ๊ณ ๋ฆฌ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” :) \n => ");
int cateCode = sc.nextInt();
System.out.print("์ƒˆ๋กœ ๋“ฑ๋กํ•  ์นดํ…Œ๊ณ ๋ฆฌ ์ด๋ฆ„์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” :) \n => ");
sc.nextLine();
String cateName = sc.nextLine();
System.out.print("์ƒˆ๋กœ ๋“ฑ๋กํ•  ์ƒ์œ„ ์นดํ…Œ๊ณ ๋ฆฌ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” :) \n => ");
int refCateCode = sc.nextInt();
		
CategoryDTO newCategory = new CategoryDTO();
newCategory.setCategoryCode(cateCode);
newCategory.setCategoryName(cateName);
newCategory.setRefCategoryCode(refCateCode);
		
Connection conn = getConnection();
PreparedStatement pstmt = null;
		
int result = 0;
		
		
try {
	Properties prop = new Properties();
	prop.loadFromXML(new FileInputStream("mapper/category-query.xml"));
			
	String query = prop.getProperty("updateCategory");
			
	pstmt = conn.prepareStatement(query);
	pstmt.setString(1, newCategory.getCategoryName());
	pstmt.setInt(2, newCategory.getRefCategoryCode());
	pstmt.setInt(3, newCategory.getCategoryCode());
			
	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("์นดํ…Œ๊ณ ๋ฆฌ ์ˆ˜์ • ์‹คํŒจ :(");
}

๐Ÿ’ป Mini Console

---------- ์นดํ…Œ๊ณ ๋ฆฌ ์ˆ˜์ • ----------
๋ณ€๊ฒฝํ•  ์นดํ…Œ๊ณ ๋ฆฌ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” :) 
 => 13
์ƒˆ๋กœ ๋“ฑ๋กํ•  ์นดํ…Œ๊ณ ๋ฆฌ ์ด๋ฆ„์„ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” :) 
 => ๋ฒ ์ง€ํ…Œ๋ฆฌ์•ˆ
์ƒˆ๋กœ ๋“ฑ๋กํ•  ์ƒ์œ„ ์นดํ…Œ๊ณ ๋ฆฌ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” :) 
 => 10
์นดํ…Œ๊ณ ๋ฆฌ ์ˆ˜์ • ์™„๋ฃŒ ! :)

๐Ÿ’ป TBL_CATEGORY TABLE


๐Ÿ‘‰ [DELETE] ์‚ฌ์šฉ์ž ์ž…๋ ฅ ๊ฐ’์œผ๋กœ Category ์‚ญ์ œ

Scanner sc = new Scanner(System.in);
System.out.println("---------- ์นดํ…Œ๊ณ ๋ฆฌ ์‚ญ์ œ ----------");
System.out.print("์‚ญ์ œํ•  ์นดํ…Œ๊ณ ๋ฆฌ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” :) \n => ");
int cateCode = sc.nextInt();
		
Connection conn = getConnection();
PreparedStatement pstmt = null;
		
int result = 0;
		
Properties prop = new Properties();
try {
	prop.loadFromXML(new FileInputStream("mapper/category-query.xml"));
			
	String query = prop.getProperty("deleteCategory");
			
	pstmt = conn.prepareStatement(query);
	pstmt.setInt(1, cateCode);
			
	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("์นดํ…Œ๊ณ ๋ฆฌ ์‚ญ์ œ ์‹คํŒจ :(");
}

๐Ÿ’ป Mini Console

---------- ์นดํ…Œ๊ณ ๋ฆฌ ์‚ญ์ œ ----------
์‚ญ์ œํ•  ์นดํ…Œ๊ณ ๋ฆฌ ๋ฒˆํ˜ธ๋ฅผ ์ž…๋ ฅํ•ด์ฃผ์„ธ์š” :) 
 => 13
์นดํ…Œ๊ณ ๋ฆฌ ์‚ญ์ œ ์™„๋ฃŒ ! :)

๐Ÿ’ป TBL_CATEGORY TABLE


profile
Tiny little habits make me

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