๐โโ๏ธ CRUD๋,
๋๋ถ๋ถ์ ์ปดํจํฐ ์ํํธ์จ์ด๊ฐ ๊ฐ์ง๋ ๊ธฐ๋ณธ์ ์ธ ๋ฐ์ดํฐ ์ฒ๋ฆฌ ๊ธฐ๋ฅ์ธ Create(์์ฑ), Read(์ฝ๊ธฐ), Update(๊ฐฑ์ ), Delete(์ญ์ )๋ฅผ ๋ฌถ์ด์ ์ผ์ปซ๋ ๋ง
- ์ฌ์ฉ์ ์ธํฐํ์ด์ค๊ฐ ๊ฐ์ถ์ด์ผ ํ ๊ธฐ๋ฅ(์ ๋ณด์ ์ฐธ์กฐ/๊ฒ์/๊ฐฑ์ )์ ๊ฐ๋ฆฌํค๋ ์ฉ์ด๋ก์๋ ์ฌ์ฉ
<?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>
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();
}
}
}
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 + "]";
}
}
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()๋ก ์คํํ ๊ตฌ๋ฌธ ์ ์ฅ
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("๋ฉ๋ด ๋ณ๊ฒฝ ์คํจ :(");
}
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("๋ฉ๋ด ์ญ์ ์คํจ :(");
}