본문 바로가기

웹서비스 구축

DAO와 DTO

Dao는...

Data Access Object의 줄임말입니다..

데이터베이스에 접근할 수 있도록 만든 Object라 이말이지요...

package dao;
import java.sql.*;
import java.util.ArrayList;
import java.util.List;
import dao.Book;

public class BookDao {
	

	
	public static Connection getConnection() {
		
		 Connection con = null;
				try {
			        Class.forName("com.mysql.cj.jdbc.Driver");  
			        con= DriverManager.getConnection
 ("jdbc:mysql://???/???useUnicode=true&characterEncoding=UTF-8","??","??");

				}catch(Exception e) {
					System.out.println(e);}		
				return con;
	}
	
	public static int save(Book book) {
		int status = 0;
		try {
			Connection con = getConnection();
			PreparedStatement ps = con.prepareStatement
            ("insert into books(title,author,comment,file1)values(?,?,?,?)");
			System.out.println("여기 출력안되면 getConnection 오류");
			ps.setString(1, book.getName());
			ps.setString(2, book.getAuthor());
			ps.setString(3, book.getStory());
			ps.setString(4, book.getFile());
			status = ps.executeUpdate();
			ps.close();
			con.close();
			
		}catch(Exception e) {
			System.out.println(e);
			}
		
		return status;
	}
	

예 익숙하시죠...? jdbc를 이용해서 어떠한 DB를 쓸 것인지를 정의해주고....

getConnection메소드를 통해서 DB와 연결해준다음, prepareStatement메소드로와 setString으로 DB에서 실행할

명령어들을 입력해주면 되겠습니다. 저의경우는 책에 대한 정보를 제 DB에다가 저장하는 형식이 되겠군요.

 

 

DTO는...

Data Transfer Object의 줄임말입니다.

말 그대로 데이터의 교환이 일어나는 장소인데요, 아래와 같은 형식을 DTO라고 합니다.

많이 보셨죠...?희희

package dao;

public class Book {
private String name, author, story, file;
private String time ;
int id;

public int getId() {
	return id;
}

public void setId(int id) {
	this.id = id;
}
public String getName() {
	return name;
}

public void setName(String name) {
	this.name = name;
}

public String getAuthor() {
	return author;
}

public void setAuthor(String author) {
	this.author = author;
}

public String getStory() {
	return story;
}

public void setStory(String story) {
	this.story = story;
}

public String getFile() {
	return file;
}

public void setFile(String file) {
	this.file = file;
}

public String getTime() {
	return time;
}

public void setTime(String time) {
	this.time = time;
}


}

 

그럼 이상 포스팅을 마칩니다...!

'웹서비스 구축' 카테고리의 다른 글

Model2형 CRUD 페이지 만들어보기(1)  (0) 2020.08.12
AOP란...?  (0) 2020.08.11
POJO란...?  (0) 2020.08.11
Spring FrameWork란...?  (0) 2020.08.10
Model1, Model2(Spring MVC)  (0) 2020.08.10