์น ํ๋ก์ ํธ๋ฅผ ํ๊ธฐ ์ํด์๋ ์ ๊ตฌ์กฐ์ ๋ํ ์ดํด๋ฅผ ํด์ผ ํ๋ค.
์คํค๋ง๋ผ๋ ๋จ์ด.
๋ด๊ฐ ์๋ ์คํค๋ง๋ ์ฌ๋ฆฌํ ์ฉ์ด ๋ฟ์ด์๋๋ฐ..
์คํค๋ง๋ ๋ฌด์์ธ๊ฐ?
- ์, ๋ ๋ ๋น ์์ ์์ผ๋ก ๋ถ๊ณ ๊ธฐ๋ฒ๊ฑฐ๋ผ๊ณ ํฉ์๋ค ! --> ์ฉ์ด ์ฌ์ ์ ์
- ์ฐ๋ฆฌ ํ์ฌ์์ ์ธ์ฌ ์์คํ ์ ๋ง๋๋๋ฐ, ์ง์์ด๋ผ๋ ๋ฐ์ดํฐ๋ฅผ ๋ณด๊ดํ๋ DB์ ์ ํ ์ด๋ธ ์ ์ํ๋ ๊ฒ : ์คํค๋ง๋ฅผ ์ ํ๋ ๊ฒ.
- RDB(๊ด๊ณํ๋๋น)์ ๋ชฉ์ : ๋น์ฆ๋์ค ์์์ ํ์ํ ๊ฒ์ ํ ์ด๋ธํ ํ๋ ๊ฒ.
- ๊ตญ์ด ์ฌ์ : ์คํค๋ง ์ ์ง
- ์ฌ๋ฌผ / ์ฉ์ด / ๊ฐ์ฒด ์ฌ์ ์ ์
- ๋ฐ์ดํฐ ๋ฒ ์ด์ค: Table์ด ์ฃผ ์คํค๋ง. (๋ฐ์ดํฐ ๋ฒ ์ด์ค์๋ ๋ทฐ, stored procedure ๋ฑ์ด ์๋๋ฐ๋)
- Java : VO / DTO
๊ฒ์๋ง ์ํ๋ฉด ์์ค์ ๋๋ ค์๋ ๊ฒ์ด ์ฝ๋.
MCP(Move Copy Paste) ๋ ๊ทธ๋ฆผ ์ดํด๋ฅผ ์ ํด์ผํจ.
Dept Class ์ ์
- DB Connection Class๋ฅผ ๋ง๋ค๊ณ , : DAO
- // JDBC ๋๋ผ์ด๋ฒ ๋ก๋
- // Database Connection ์ฐ๊ฒฐ
- // Statement ์์ฑ
- // SQL ์ ์ก --> executeQuery(), executeUpdate()
- // ResultSet(๊ฒฐ๊ณผ) ๋ฐ๊ธฐ
- // Database Connection ํด์
- Data ๊ฐ์ฒด: ex) Dept Class๋ฅผ ๋ง๋ค๊ณ , : VO / DTO
- //๋ฉค๋ฒ ํ๋
private int deptno
private String dname;
private String loc;
๋ฉค๋ฒ ์์ฑ์ - Dept(){ }
Dept(deptno, dname, loc){ } - ๋ฉค๋ฒ ๋ฉ์๋
getDeptno( ){ }; getDname( ){ }; getLoc( ){ }
// setDeptno( ){ }; getDname( ){ }; setLoc( ){ }
- //๋ฉค๋ฒ ํ๋
- CRUD ์ฒ๋ฆฌ ๊ฐ์ฒด: Dept CRUD Class : DAO
DB Connection ํธ์ถ:
3. // Statement ์์ฑ
4. // SQL ์ ์ก --> executeQuery(), executeUpdate()
5. // ResultSet(๊ฒฐ๊ณผ) ๋ฐ๊ธฐ
selectDept( ){ } ๋ถ์๋ช
--> ๋ถ์๋ฒํธ, ๋ถ์๋ช
, ๋ถ์์์น ๋ฑ๋ฑ
insertDept( ) { }
undateDept( ) { }
deleteDept( ) { }
listDept( ) { } : ๋ถ์๋ช
๋ง ๋์ค๋ ๊ฒ --> ์ด ๋ถ์๋ช
์ ํด๋ฆญํ๋ฉด (๊ฒ์ํ ์๊ฐํ๋ฉด ์ข์)
----------------------------------------
- Main ๊ธฐ๋ฐ Class
main() {
// ๋ถ์๊ด๋ฆฌ ์์คํ ์์
while() {
1. ๋ถ์๋ชฉ๋ก
2. ๋ถ์๋ณด๊ธฐ
3. ๋ถ์์ ๋ ฅ
4. ๋ถ์์์
5. ๋ถ์์ญ์
}
// ๋ถ์๊ด๋ฆฌ ์์คํ ์ข ๋ฃ
DBConn.java
package com.my;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
public class DBConn {
public static Connection getConnection ( ) throws SQLException {
//1. JDBC ๋๋ผ์ด๋ฒ ๋ก๋
//2. Database Connection ์ฐ๊ฒฐ
Connection conn = null;
String jdbcDriver = "com.mysql.cj.jdbc.Driver";
String jdbcUrl = "jdbc:mysql://localhost:3306/empdb?serverTimezone=UTC";
String dbUser = "root";
String dbPwd = "1234";
conn = DriverManager.getConnection(jdbcUrl,dbUser,dbPwd ); // ์์ธ์ฒ๋ฆฌ ์ํด์ฃผ๋ฉด ๋นจ๊ฐ์ค, ์ฌ๊ธฐ์ ํธ๋ผ์ด์บ์น ์์ฐ๊ณ throws
return conn;
}
}
DeptVO.java
package com.my;
// ๋ฐ์ดํฐ ๊ฐ์ฒด
public class DeptVO {
//๋ฉค๋ฒ ํ๋
private int deptno;
private String dname;
private String loc;
// ์์ฑ์
DeptVO(){ } // ์๋๋ ๊ฒ
DeptVO(int deptno, String dname, String loc){
this.deptno = deptno; this.dname = dname; this.loc = loc; }
// ๋ฉค๋ฒ ๋ฉ์๋ ( ๋ฉ๋ด source์์ --> Generate getters and setter์์ ์๋ ์์ฑ)
public int getDeptno() { return deptno; }
public String getDname() { return dname; }
public String getLoc() { return loc; }
@Override
public String toString() {
return "DeptVO [deptno=" + deptno + ", dname=" + dname + ", loc=" + loc + ", toString()=" + super.toString()
+ "]";
}
}
DeptDAO.java
package com.my;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
public class DeptDAO {
// Singleton
private static DeptDAO dao = new DeptDAO();
private DeptDAO(){ }
public static DeptDAO getInstance() { return dao; }
/////////////////
DeptVO selectDept(int deptno, Connection conn) {
DeptVO dept = null;
try {
//String sql = "select * from dept where deptno = "+ deptno;
String sql = "select * from dept10 where deptno = ?";
//Statement stmt = conn.createStatement();
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, deptno);
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
dept = new DeptVO(rs.getInt(1),rs.getString(2),rs.getString("loc"));
System.out.printf("%d | %-10s | %-10s %n",rs.getInt("deptno"),rs.getString(2),rs.getString("loc"));
}
pstmt.close();
//stmt.close();
}
catch (SQLException e) {
System.out.println("์ฐ๊ฒฐ ์๋จ");
}
return dept;
}
int insertDept(DeptVO deptObj, Connection conn) {
int resultCount = 0;
DeptVO dept = null;
try {
String sql = "insert into dept10 values(?,?,?)";
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, deptObj.getDeptno());
pstmt.setString(2, deptObj.getDname());
pstmt.setString(3, deptObj.getLoc());
resultCount = pstmt.executeUpdate();
pstmt.close();
}
catch (SQLException e) {
System.out.println("์ฐ๊ฒฐ ์๋จ");
}
return resultCount;
}
int updateDept(DeptVO deptObj,Connection conn) {
int resultCount = 0;
try {
String sql = "update dept10 set loc = ? where deptno = ?"; ;
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(2, deptObj.getDeptno());
pstmt.setString(1, deptObj.getLoc());
resultCount = pstmt.executeUpdate();
pstmt.close();
}
catch (SQLException e) {
System.out.println("์ฐ๊ฒฐ ์๋จ");
}
return resultCount;
}
int deleteDept(int deptno,Connection conn ) {
int resultCount = 0;
try {
String sql = "delete from dept10 where deptno = ?"; ;
PreparedStatement pstmt = conn.prepareStatement(sql);
pstmt.setInt(1, deptno);
resultCount = pstmt.executeUpdate();
pstmt.close();
}
catch (SQLException e) {
System.out.println("์ฐ๊ฒฐ ์๋จ");
}
return resultCount;
}
List<DeptVO> listDept(Connection conn) {
List<DeptVO> deptList = new ArrayList<DeptVO>();
try {
String sql = "select * from dept10";
PreparedStatement pstmt = conn.prepareStatement(sql);
ResultSet rs = pstmt.executeQuery();
while(rs.next()) {
System.out.printf("%d | %-10s | %-10s %n",rs.getInt("deptno"),rs.getString(2),rs.getString("loc"));
deptList.add(new DeptVO(rs.getInt(1),rs.getString(2),rs.getString(3)));
}
pstmt.close();
}
catch (SQLException e) {
System.out.println("์ฐ๊ฒฐ ์๋จ");
}
return deptList;
}
}
DeptMain.java
// DAO, VO ๋ถ๋ฆฌํด์ ๋ง๋ค ์ ์๋ ๋๋์ด ์ค์ํ๋ค
package com.my;
import java.sql.Connection;
import java.util.List;
import java.util.Scanner;
public class DeptMain {
static String jdbcDriver = "com.mysql.cj.jdbc.Driver";
static DeptDAO dDao = DeptDAO.getInstance();
static void selectD(int deptno,Connection conn) {
DeptVO dSel = dDao.selectDept(deptno, conn);
System.out.println(dSel.toString());
}
static void insertD(DeptVO deptObj,Connection conn) {
int resultCount = dDao.insertDept(deptObj, conn);
if(resultCount > 0 ) {System.out.println("์
๋ ฅ ์๋ฃ "); }
else {System.out.println("๋ฌธ์ ๋ฐ์ "); }
}
static void updateD(DeptVO deptObj,Connection conn) {
int resultCount = dDao.updateDept(deptObj,conn);
if(resultCount > 0 ) {System.out.println("์์ ์๋ฃ "); }
else {System.out.println("๋ฌธ์ ๋ฐ์ "); }
}
static void deleteD(int deptno,Connection conn ) {
int resultCount = dDao.deleteDept(deptno,conn );
if(resultCount > 0 ) {System.out.println("์ญ์ ์๋ฃ "); }
else {System.out.println("์ค๋ฅ ๋ฐ์ "); }
}
static void listD(Connection conn){
List<DeptVO> deptList = dDao.listDept(conn);
for(DeptVO dept: deptList) {
System.out.println(dept.toString()); }
}
public static void main(String[] args) {
System.out.println("=========== ๋ถ์ ๊ด๋ฆฌ ์์คํ
์์ =============");
Scanner sc = new Scanner(System.in);
try {
Class.forName(jdbcDriver);
Connection conn = DBConn.getConnection();
int iDeptno = 0; String sDname = null; String sLoc = null;
DeptVO deptObj = null;
while(true) {
System.out.println("----------------------------------------------");
System.out.println("1. ์กฐํ, 2.์
๋ ฅ 3. ์์ , 4. ์ญ์ , 5. ์ ์ฒด์กฐํ, 9.์ข
๋ฃ ==> ");
int choiceNum = sc.nextInt();
switch (choiceNum) {
case 1:
System.out.println("[1. ์กฐํ : ๊ฒ์ํ ๋ถ์๋ฒํธ ์
๋ ฅ => ");
int deptno = sc.nextInt();
selectD(deptno,conn);
break;
case 2:
System.out.println("[2.์
๋ ฅ : ๋ถ์ ๋ฒํธ ์
๋ ฅ => "); iDeptno = sc.nextInt();
System.out.println("[2.์
๋ ฅ : ๋ถ์ ์ด๋ฆ ์
๋ ฅ => "); sDname = sc.next();
System.out.println("[2.์
๋ ฅ : ๋ถ์ ์์น ์
๋ ฅ => "); sLoc = sc.next();
deptObj = new DeptVO(iDeptno,sDname,sLoc);
insertD(deptObj,conn);
break;
case 3:
System.out.println("[3.์์ : ๋ถ์ ๋ฒํธ ์
๋ ฅ => "); iDeptno = sc.nextInt();
System.out.println("[3.์์ : ๋ณ๊ฒฝ ์ง์ญ ์
๋ ฅ => "); sLoc = sc.next();
deptObj = new DeptVO(iDeptno,sDname,sLoc);
updateD(deptObj,conn);
break;
case 4:
System.out.println("[4.์ญ์ : ๋ถ์ ๋ฒํธ ์
๋ ฅ => "); iDeptno = sc.nextInt();
deleteD(iDeptno,conn);
break;
case 5:
System.out.println("[5.์ ์ฒด ์กฐํ : ๋ฆฌ์คํธ๋ฅผ ์ ์ฒด ์กฐํํฉ๋๋ค ");
listD(conn);
break;
case 9:
System.out.println("[9. ์ข
๋ฃ : ํ๋ก๊ทธ๋จ์ ์ข
๋ฃํฉ๋๋ค . ^^");
System.out.println("=========== ์๋
ํ ๊ฐ์ธ์ =============");
conn.close();
return;
default:
System.out.println("1~5 ์ค ํ๋๋ฅผ ์ ํํด์ฃผ์ธ์. ");
}
}
}
catch(Exception e) {
System.out.println("๋ฌธ์ ๋ฐ์ ");
}
sc.close();
}
}
DAO์์์ ํจ์๋ฅผ main์์ ์ฌ์ฉํ๋ฉด ๋๋ค.
์๋ค๊ฐ๋ค ๋ณต๋ถ์ ์ฌ๊ธฐ์ ๊ธฐํ๊ณ ๋น ๋ฅด๊ฒ ์น๋๊น ๋ชจ๋ฅด๊ฒ ๋ค! ์๋ ๋๋ฌด ๋น ๋ฅธ๋ฐ. ๋ฐ๋ผ ์ฝ๋ ๋ฐ์์ฐ๊ธฐ๋ ๋ถ๊ฐ.
์ ๊ต์ก ๊ณผ์ ์๋ ๋ฏธ์น๊ฑด๊ฐ. ๋งค์ฐ ํญ์ฃผํ๋ค๋ ๋๋์ ํ๋ฃจ์ข ์ผ ๋ฐ์๋ค.
์ง์ง ์ด์ง์คํด์ ๋ฐ๋ผ์ณค๋๋ฐ,
๋ด ํ๋ฉด ๋ณด๋ ์ฌ์ด์
์ฌ๊ธฐ ์ด๊ฑฐ๋ ์ ๊ธฐ๋ก ๋ ๋ฆด๊ฒ์ ํด์ ์ฎ๊ฒจ๋ฒ๋ฆฐ ์ฝ๋๋ค์ด ํ๋๊ฐ๊ฐ ์๋์ด์,
๊ทธ๋ด ๋๋ง๋ค ์ฐธ์ ์ธ์ ๋ฐฑ๊ฐ์ ๋ ์๊ธด ๊ฒ ๊ฐ๋ค! ใ ใ ใ
๊ทธ๋ฌ๋ค๊ฐ ๊ฒฐ๊ตญ ๋ฐ๋ผ ์น๊ธฐ ํฌ๊ธฐ.
์ค๋ช ์ ๋ค์ผ๋ฉด ๋ด ์ฝ๋๋ฅผ ๋ชป์น๊ณ , ๋ด ์ฝ๋ ์น๊ณ ์์ผ๋ฉด ์์ ์ ์ ๋ฉ๋ฆฌ ๊ฐ๋ฒ๋ฆฌ๊ณ ์๋ค!
์ปด๊ณต ์ ๊ณต์ ๋ถ๋ค์ ์ด์ ์ข ์ฌ๋ฐ์ดํ์๋๊ฒ ๋ณด์ด๊ณ ..
๊ทผ๋ฐ ๋ํ 4๋ ๋์ ๊ด๋ จ ์ ๊ณต ๋ฐฐ์ ์ํ ๋ฐ ์ด ์์ ์ ์ ๋ฃ๋๊ฑฐ์ง ใ _ใ ????
๊ณผ์ :
'PlayData > MySQL' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Day 17 | ๊ณผ์ ] DAO, DTO, VO ์ฐจ์ด(~ing) (0) | 2021.12.03 |
---|---|
[Day 16 | MySQL] Table, Variable, ๋ฐ์ดํฐ ๋ชจ๋ธ๋ง, (0) | 2021.12.01 |
[Day 15 | MySQL] SubQuery, DML, Table and ๊ณผ์ (0) | 2021.11.29 |
[Day14 | MySQL] Join(ANSI Join, inner join, outer join, cross join etc.) (0) | 2021.11.26 |
[Day13 | MySQL] SQL ๊ธฐ๋ณธ RDB, where, groupby, having (0) | 2021.11.25 |