티스토리 뷰

JDBC

CRUD를 담당하는 NoticeService 생성

Programmers 2021. 5. 3. 09:35
반응형

CRUD란?

Create: 생성

Read: 읽기

Update: 수정

Delete: 삭제

 

목표
: SELECT 함수 구현

클래스 추가(서비스)
Package: com.newlecture.app.service
Name: NoticeService

클래스 추가(그릇을 정의)
Package: com.newlecture.app.entity
Name: Notice

NoticeService.java

/* NoticeService.java */
package com.newlecture.app.service;

import java.sql.Connection;
import java.sql.Date;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.newlecture.app.entity.Notice;

public class NoticeService {
	/* CRUD 서비스 목록을 반환하는 함수 */ 
	
	/*List-> java.util import */
	/*List<Notice>라는 목록으로 반환 */
	public List<Notice> getList() throws ClassNotFoundException, SQLException{	
			//getList()함수를 가지고 반환을 하겠다.
			//List<>라는 컬렉션에 담아서 준다.
		
		String url = "jdbc:oracle:thin:@192.168.219.103:1521/XE";
		String sql = "SELECT * FROM NOTICE WHERE hit >= 10";
		
		
		Class.forName("oracle.jdbc.driver.OracleDriver");
		Connection con = DriverManager.getConnection(url, "corini", "111111");
		Statement st = con.createStatement();
		ResultSet rs = st.executeQuery(sql);
		
		/*반환 하기 위한 목록을 만든다 */
		List<Notice> list = new ArrayList<Notice>();
		
		
		/* Notice를 담을 수있는 그릇이 필요 -> 클래스 추가 entity 이름 Notice.java 추가(그릇, 서비스x)*/
		while(rs.next()) {
			int id = rs.getInt("ID");
			String title = rs.getString("TITLE");
			String writerId = rs.getString("WRITER_ID");
			Date regDate = rs.getDate("REGDATE");
			String content = rs.getString("CONTENT");
			int hit = rs.getInt("hit");
			
			/*Notice라는 객체를 통해서 notice를 하나 만들면서 초기화하는 생성*/
			Notice notice = new Notice(
					/*Notice.java 생성자와 담을때 딱 맞게 객체를 만들어야 한다. */
					id,
					title,
					writerId,
					regDate,
					content,
					hit
					);
			/*notice라는 놈을 추가해서 목록에 하나하나 추가 */
			list.add(notice);
			}
				
		rs.close();
		st.close();
		con.close();
		
		return list;
	}

}

 

Notice.java

/* Notice.java */
package com.newlecture.app.entity;

import java.sql.Date;

public class Notice {
	/* 값을 담을 수 있는 그룹화된 그릇*/
	/* 기본적인 속성만 정의 */
	/* 구조가 노출 되지 않도록 private 보호모드 추가*/
	private int id;
	private String title;
	private String writerId;
	private Date regDate;
	private String content;
	private int hit;
	
	/*여러가지 값으로 초기화를 하면서 값을 채워야 하기 때문에 생성자를 만든다 Crtl+Space*/
	/* 매개변수 */
	public Notice() {
		// TODO Auto-generated constructor stub
	}
		
	/*값들을 셋팅 할수있도록 getter setter 생성*/
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	
	/* 필드를 가지고 있는 생성자도 필요 source */
	/* source -> generatre constructor usning fields */
	public Notice(int id, String title, String writerId, Date regDate, String content, int hit) {
		this.id = id;
		this.title = title;
		this.writerId = writerId;
		this.regDate = regDate;
		this.content = content;
		this.hit = hit;
	}

	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getWriterId() {
		return writerId;
	}
	public void setWriterId(String writerId) {
		this.writerId = writerId;
	}
	public Date getRegDate() {
		return regDate;
	}
	public void setRegDate(Date regDate) {
		this.regDate = regDate;
	}
	public String getContent() {
		return content;
	}
	public void setContent(String content) {
		this.content = content;
	}
	public int getHit() {
		return hit;
	}
	public void setHit(int hit) {
		this.hit = hit;
	}
}
반응형

'JDBC' 카테고리의 다른 글

공지사항 메뉴 붙이기  (0) 2021.05.03
사용자 인터페이스 붙이기(공지사항 목록)  (0) 2021.05.03
데이터 수정  (0) 2021.04.30
데이터 수정을 위한 쿼리 준비  (0) 2021.04.30
데이터 입력 / PreparedStatement  (0) 2021.04.30
댓글
공지사항