๐โ 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
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();
}
}
}
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 + "]";
}
}
<?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();
}
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]
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]
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
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
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