1.CRUD
(1) C -> Create -> insert -> HTTP(POST)
(2) R -> Read -> select -> HTTP(GET)
(3) U -> Update - > update -> HTTP(PUT)
(4) D -> Delete -> delete -> HTTP(DELETE)
2.User CRUD ํ ์คํธ
(1) model/User.java
package com.cos.board.model;
import java.sql.Timestamp;
// model, vo(value object), bean(์ฝฉ)
public class User {
private int id;
private String username;
private String password;
private String email;
private Timestamp createTime;
// All Constructor
public User(int id, String username, String password, String email, Timestamp createTime) {
this.id = id;
this.username = username;
this.password = password;
this.email = email;
this.createTime = createTime;
}
// Getter, Setter
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public Timestamp getCreateTime() {
return createTime;
}
public void setCreateTime(Timestamp createTime) {
this.createTime = createTime;
}
}
(2)test/UserDaoTest.java
package com.cos.board.test;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.List;
import com.cos.board.db.DButil;
import com.cos.board.model.User;
// User Test
// DAO -> data Access object : ๋ฐ์ดํฐ๋ฒ ์ด์ค์ ์ ์ํด์
public class UserDaoTest {
public int save() {
// 1. Stream ์ฐ๊ฒฐ
Connection conn = DButil.getConnection();
PreparedStatement pstmt = null;
try {
// 2. ์ฟผ๋ฆฌ ๋ฉ์์ง ์ ์ก ํด๋์ค(๊ท์ฝ์ ๋ง๊ฒ), sql์ ์ฟผ๋ฆฌ
final String SQL = "INSERT INTO user(username, password, email, createTime) VALUES(?,?,?,now())";
pstmt = conn.prepareStatement(SQL);
// 3. SQL๋ฌธ ์์ฑํ๊ธฐ
pstmt.setString(1, "love");
pstmt.setString(2, "1234");
pstmt.setString(3, "love@nate.com");
// 4. SQL๋ฌธ ์ ์ก write,read์ ์ ์ก ๋ฐฉ๋ฒ์ด ๋ค๋ฅด๋ค. ํ๋กํ ์ฝ์ด 2๊ฐ ํ์. executeQuery๋ ๊ฒฐ๊ณผ๋ฅผ ์๋ ค๋ฌ๋ผ๋ ๋ฌธ,
// executeUpdate๋ DB์ ๊ธฐ๋กํ๊ณ return์ด ํ์์๋ ๋ฌธ
// pstmt.executeQuery();
int result = pstmt.executeUpdate();// PSMT๊ฐ ๋ซํ๋ DB์ ์๋์ผ๋ก ์ปค๋ฐ ํด์ค.
return result;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.close();
// conn.commit(); ์๋๋ ์๋์ผ๋ก ์ปค๋ฐ๋๋ ๋๊ฐ์ง์ ์ผ์ด ํ๊ฐ์ ํธ๋์ญ์
์ผ๋๋ ๋ฐ๋ก ์ปค๋ฐํด์ผํจ
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return -1;
}
public int update() {
// 1. Stream ์ฐ๊ฒฐ
Connection conn = DButil.getConnection();
PreparedStatement pstmt = null;
try {
// 2. ์ฟผ๋ฆฌ ๋ฉ์์ง ์ ์ก ํด๋์ค(๊ท์ฝ์ ๋ง๊ฒ), sql์ ์ฟผ๋ฆฌ
final String SQL = "UPDATE user SET password=? where id=?";
pstmt = conn.prepareStatement(SQL);
// 3. SQL๋ฌธ ์์ฑํ๊ธฐ
pstmt.setString(1, "5678");
pstmt.setInt(2, 2); // where์ ์๋ ์ ์ผํ ๊ฐ, pk๋ index, ๋ฒ์๊ฐ ๊ฐ์ฅ ์์์์๋ ๊ฐ ์ฌ์ฉ
// ๋นํธ๋งต ์ธ๋ฑ์ค : ์ ์ฅํ ๋ ๊ตฐ์งํํ์ฌ ์ ์ฅํ๋ฉด where์ ์ ๋น ๋ฅด๊ฒ ์ฐพ์์์๊ฒ ์ฒ๋ฆฌํ ์ ์์.
// 4. SQL๋ฌธ ์ ์ก write,read์ ์ ์ก ๋ฐฉ๋ฒ์ด ๋ค๋ฅด๋ค. ํ๋กํ ์ฝ์ด 2๊ฐ ํ์. executeQuery๋ ๊ฒฐ๊ณผ๋ฅผ ์๋ ค๋ฌ๋ผ๋ ๋ฌธ,
// executeUpdate๋ DB์ ๊ธฐ๋กํ๊ณ return์ด ํ์์๋ ๋ฌธ
// pstmt.executeQuery();
int result = pstmt.executeUpdate();// PSMT๊ฐ ๋ซํ๋ DB์ ์๋์ผ๋ก ์ปค๋ฐ ํด์ค.
return result;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.close();
// conn.commit(); ์๋๋ ์๋์ผ๋ก ์ปค๋ฐ๋๋ ๋๊ฐ์ง์ ์ผ์ด ํ๊ฐ์ ํธ๋์ญ์
์ผ๋๋ ๋ฐ๋ก ์ปค๋ฐํด์ผํจ
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return -1;
}
public int delete() {
// 1. Stream ์ฐ๊ฒฐ
Connection conn = DButil.getConnection();
PreparedStatement pstmt = null;
try {
// 2. ์ฟผ๋ฆฌ ๋ฉ์์ง ์ ์ก ํด๋์ค(๊ท์ฝ์ ๋ง๊ฒ), sql์ ์ฟผ๋ฆฌ
final String SQL = "delete from user where id= ?";
pstmt = conn.prepareStatement(SQL);
// 3. SQL๋ฌธ ์์ฑํ๊ธฐ
pstmt.setInt(1, 1);
int result = pstmt.executeUpdate();// PSMT๊ฐ ๋ซํ๋ DB์ ์๋์ผ๋ก ์ปค๋ฐ ํด์ค.
return result;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.close();
// conn.commit(); ์๋๋ ์๋์ผ๋ก ์ปค๋ฐ๋๋ ๋๊ฐ์ง์ ์ผ์ด ํ๊ฐ์ ํธ๋์ญ์
์ผ๋๋ ๋ฐ๋ก ์ปค๋ฐํด์ผํจ
pstmt.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return -1;
}
public List<User> findAll() {
// 0. ์ปฌ๋ ์
๋ง๋ค๊ธฐ ALL, ์ฌ๋ฌ๊ฑด์ returnํ ๋ ๊ผญ ํ์
List<User> users = new ArrayList<>();
// 1. Stream ์ฐ๊ฒฐ
Connection conn = DButil.getConnection();
PreparedStatement pstmt = null;
ResultSet rs = null;
try {
// 2. ์ฟผ๋ฆฌ ๋ฉ์์ง ์ ์ก ํด๋์ค(๊ท์ฝ์ ๋ง๊ฒ), sql์ ์ฟผ๋ฆฌ
final String SQL = "select * from user";
pstmt = conn.prepareStatement(SQL);
rs = pstmt.executeQuery();// resultset์ ๊ฒฐ๊ณผ์ ์ง์
ํ๊ธฐ์ํ ์ฃผ์๊ฐ์ ๊ฐ๋ฆฌํค๊ณ ์์.
while(rs.next()) {
int id = rs.getInt("id"); // ์ปฌ๋ผ ๋ค์
String username = rs.getString("username");
String password = rs.getString("password");
String email = rs.getString("email");
Timestamp createTime = rs.getTimestamp("createTime");
User user = new User(id, username, password, email, createTime);
users.add(user);
}
return users;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
conn.close();
// conn.commit(); ์๋๋ ์๋์ผ๋ก ์ปค๋ฐ๋๋ ๋๊ฐ์ง์ ์ผ์ด ํ๊ฐ์ ํธ๋์ญ์
์ผ๋๋ ๋ฐ๋ก ์ปค๋ฐํด์ผํจ
pstmt.close();
rs.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
public User findById() {
// 1. Stream ์ฐ๊ฒฐ
Connection conn = DButil.getConnection();
PreparedStatement pstmt= null;
ResultSet rs = null;
try {
// 2. ์ฟผ๋ฆฌ ์ ์ก ํด๋์ค (๊ท์ฝ์ ๋ง๊ฒ)
final String SQL = "SELECT * FROM user WHERE id = ?";
pstmt = conn.prepareStatement(SQL);
// 3. SQL๋ฌธ ์์ฑํ๊ธฐ
pstmt.setInt(1, 2);
// 4. SQL๋ฌธ ์ ์ก
rs = pstmt.executeQuery();
User user = null;
if(rs.next()) {
int id = rs.getInt("id"); // ์ปฌ๋ผ ๋ค์
String username = rs.getString("username");
String password = rs.getString("password");
String email = rs.getString("email");
Timestamp createTime = rs.getTimestamp("createTime");
user = new User(id, username, password, email, createTime);
}
return user;
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
pstmt.close();
rs.close();
conn.close();
} catch (Exception e) {
e.printStackTrace();
}
}
return null;
}
}
(3)test/UserControllerTest.java
package com.cos.board.test;
import java.io.IOException;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.cos.board.action.Action;
import com.cos.board.action.board.BoardFactory;
import com.cos.board.db.DButil;
import com.cos.board.model.User;
@WebServlet("/test")
public class UserControllerTest extends HttpServlet {
@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
doPost(req, resp);
}
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
// DB Test
UserDaoTest t4 = new UserDaoTest();
// int result = t4.save();
// System.out.println("reault : " + result);
//
// int result = t4.update();
// System.out.println("reault : " + result);
//
// int result = t4.delete();
// System.out.println("reault : " + result);
//
// List<User> users = t4.findAll();
// for (User user : users) { // forEach๋ฌธ
// System.out.println("id : " + user.getId());
// System.out.println("username : " + user.getUsername());
// System.out.println("password : " + user.getPassword());
// System.out.println("email : " + user.getEmail());
// System.out.println("createTime : " + user.getCreateTime());
// System.out.println();
// }
User user = t4.findById();
System.out.println("id : " + user.getId());
System.out.println("username : " + user.getUsername());
System.out.println("password : " + user.getPassword());
System.out.println("createTime : " + user.getCreateTime());
System.out.println("");
}
}
3.MVC์ ์ญํ
C - ์์ฒญ์ ๋ฐ์
M - ํ์ํ ๋ฐ์ดํฐ๋ฅผ controller์๊ฒ ์ ๋ฌ
==========================================================
* ์ฐธ๊ณ ์ฌํญ๋ค
ํฉํ ๋ฆฌ : ๋ ๋์ ๊ฐ์ฒด ๋ง๋ค์ด ์ฃผ๋ ํด๋์ค๋ค
๋ชจ๋ ์ธ์ด์ ์ค๋ฅ ์์น
๋ฐฐํฌ ์ web.xml์ ๋ฒ์ ์ด 2.4 3.1๋ฑ์ ๋ฒ์ ์ ๋ฐ๋ผ ์๋ฒ์ ์ฐ๋์ด ์๋ ์๋ ์๋ค.
์๋ฒ์ ํฐ์บฃ๋ฒ์ ์ ๋ฐ๋ผ ๋์ ๋ฒ์ ์ ๋ฐ๊ฟ์ฃผ์ด์ผํจ.
์ํ์น์ ์ค๋ผํด๋ถ๋ถ์์ ์๋ฐ์ฝ๋ ๋ณต์ฌํ๊ธฐ
์์ธ์ฒ๋ฆฌ ๋ฐ ์ํฌํธ๋ชฉ๋ก
ํฐ์บฃ์ ์ญํ
์๋ก ์ฝ์๋์ง์์ ๋ฉ์์ง๋ฅผ ๋ณด๋ด๋ฉด ๊ท์ฝ, ์ฝ์ ์ฆ ํ๋กํ ์ฝ์ด ์๋ค.
์ฟผ๋ฆฌ๋ฅผ ๋งจ ์ฒ์ ๋ ๋ฆฌ๋ฉด ๋ฉํ๋ฐ์ดํฐ์ ๋ฐ์ดํฐ๋์ ๋๋ฆฌ๋ก ์ฒซ๋ฒ์จฐ๋ก ๋ฐฉ๋ฌธํจ.
๋ฉํ๋ฐ์ดํฐ๋ ๋ชจ~๋ ๋ฐ์ดํฐ๋ฅผ ๋ค๊ณ ์๋ ๋ฌธ์, ์ด๋ค ๋ฐ์ดํฐ๊ฐ ์ง์ ์ ์ผ๋ก ๋ณด์ด๋๊ฒ์ด ์๋ emp{empno, empname{ ์ด๋ผ๋ ์ ๋ณด๊ฐ ์๋ค๋ผ๋ ์ด๋ค ์ ๋ณด๊ฐ ์๋์ง๋ฅผ ์ฌ์ง์ฒ๋ผ ์ ์ฅํด๋์. ์ค๋ ์ท
prepareStmt์ where์ ? ๋ณ์๋ก ์ฌ์ฉํ๊ธฐ๋๋ฌธ์ ์๋๊ฐ ๋น ๋ฅด๋ค.
ํธ๋์ญ์ : ์ผ์ด ์ฒ๋ฆฌ ๋จ์ ex.๊ณ์ข์ด์ฒด
ORM : ๋ด๊ฐ ๋ง๋ ์ค๋ธ์ ํธ์ ๋ฐ๋ผ์ ๋ง๋ค์ด์ง
'java' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
JAVA - Thread์ดํดํ๊ธฐ (0) | 2020.11.09 |
---|---|
JSP - ๊ฒ์ํ๋ง๋ค๊ธฐ(7) ์ค๋ฅ๋ฌธ์ ๋ฆฌ:-) (0) | 2020.11.09 |
JSP - ๊ฒ์ํ ๋ง๋ค๊ธฐ(5) DB Connection Pool(CP) (0) | 2020.11.09 |
JSP - ๊ฒ์ํ๋ง๋ค๊ธฐ(2) - MySQL ํ๊ธ ์ ํ ํ๊ธฐ, ์ ์ ์์ฑ (0) | 2020.11.09 |
JSP - ๊ฒ์ํ๋ง๋ค๊ธฐ(1) - ํฉํ ๋ฆฌ ํจํด ์์ (0) | 2020.11.09 |