//////
Search

스프링 , 스택

날짜
2022/10/20
작성자
우주완
카테고리

Stack

스택은 제한적으로 접근할 수 있는 나열된 구조 입니다. 후입선출의 자료구조이다. 스택에서 입력은 push, 출력은 pop , peek 은Top위치에 있는 데이터 확인합니다.

Push

스택의 가장 최상위(마지막)에 데이터를 삽입합니다.
@Test @DisplayName("push가 잘되는지") void push(){ Stack02 st = new Stack02(); st.push(10); st.push(20); Integer[] arr = st.getArr(); // assertEquals 만들어서 test 확인 assertEquals(20,arr[1]); assertEquals(10,arr[0]); }
Java
복사
public class Stack02 { private Integer[] arr; public Stack02() { this.arr = new Integer[10000]; } public Integer[] getArr() { return arr; } public void push(int value) { // 10을 넣으면 arr[0] = 10 this.arr[top++] = value; }
Java
복사

POP

스택의 가장 최상위(마지막)에 데이터를 삽입합니다.
@public int pop() { return this.arr[--this.top];
Java
복사
@Test void pushAndPop(){ Stack02 st = new Stack02(); st.push(10); st.push(20); assertEquals(20, st.pop()); assertEquals(10, st.pop()); // st.pop() 비어 있을땐? // Exception 예외의 검증 assertThrows(RuntimeException.class,()->{ st.pop(); }); }
Java
복사

isEmpty

스택이 empty 상태인지 확인합니다.
@Test void isEmpty() { Stack02 st = new Stack02(); assertTrue(st.isEmpty()); // isEmpty 상태 확인 st.push(20); assertFalse(st.isEmpty()); // isEmpty 상태 확인 st.pop(); assertTrue(st.isEmpty()); // isEmpty 상태 확인 }
Java
복사
public boolean isEmpty() { boolean isEmpty = this.top == 0; return isEmpty; }
Java
복사

Peek

스택의 가장 최상위(마지막)에 위치한 데이터를 추출합니다.
pop 메서드와 달리 스택에서 삭제하지 않습니다.
@Test void peek() { Stack02 st = new Stack02(); // 스택이 비었는데 peek 할 때 assertThrows(EmptyStackException.class,()->{ st.peek(); }); st.push(10); int peeked = st.peek(); assertEquals(10,peeked); }
Java
복사
public int peek() { if (this.isEmpty()) { throw new EmptyStackException(); } return this.arr[this.top - 1]; }
Java
복사

최종 코드

예외 처리

스프링

deleteAll(), getCount() 메서드 추가

deleteAll()

DB데이터를 모두 삭제 처리 하는 메서드 이다.
public void deleteAll() throws SQLException { Connection c = connectionMaker.makeConnection(); PreparedStatement ps = c.prepareStatement( "delete from users"); ps.executeUpdate(); ps.close(); c.close(); }
Java
복사

getCount()

DB에 저장된 데이터를 반환하는 메서드 이다.
public int getCount() throws SQLException { Connection c = connectionMaker.makeConnection(); PreparedStatement ps = c.prepareStatement( "select count(*) from users"); ResultSet rs = ps.executeQuery(); rs.next(); int count = rs.getInt(1); rs.close(); ps.close(); c.close(); return count; }
Java
복사

Test

@Test void addAndSelect() throws SQLException { UserDao userDao = context.getBean("awsUserDao", UserDao.class); userDao.deleteAll(); // 데이터 삭재 String id = "10"; User user = new User(id,"juwan", "110055qwe"); userDao.add(user); assertEquals(1, userDao.getCount()); // 데이터 개수 정보 User userSelecte = userDao.findById(id); assertEquals("110055qwe",userSelecte.getPassword()); }
Java
복사

deleteAll(), getCount() 예외 처리

connection, PreparedStatement 사용할 때 에러가 발생할 수 있다.
그래서 ps.close(), c.close() 메서드 부분을 처리해주지 않으면 리소스 반환이 되지 않아 서버가 다운될 수 있다고 한다.
꼭 try/catch/finally 으로 예외를 잡아야 한다.

deleteAll() 예외 처리

public void deleteAll() throws SQLException, ClassNotFoundException { Connection c = null; PreparedStatement ps = null; try { c = connectionMaker.makeConnection(); ps = c.prepareStatement("DELETE FROM users"); ps.executeUpdate(); // 이부분을 빼먹어서 30분동안 해맸었다.. } catch (SQLException e) { throw new RuntimeException(e); }finally { // error 가 나도 실행되는 블럭 if (ps != null){ try { ps.close(); } catch (SQLException e) { throw new RuntimeException(e); } } if (c != null) { try { c.close(); } catch (SQLException e) { throw new RuntimeException(e); } } } }
Java
복사

getCount() 예외 처리

public int getCount() throws SQLException, ClassNotFoundException { Connection c = null; PreparedStatement ps = null; ResultSet rs = null; try { c = connectionMaker.makeConnection(); ps = c.prepareStatement("SELECT count(*) FROM users"); rs = ps.executeQuery(); rs.next(); return rs.getInt(1); } catch (SQLException e) { throw new RuntimeException(e); }finally { // error 가 나도 실행되는 블럭 if (rs!= null){ try { rs.close(); } catch (SQLException e) { throw new RuntimeException(e); } } if (ps != null){ try { ps.close(); } catch (SQLException e) { throw new RuntimeException(e); } } if (c != null) { try { c.close(); } catch (SQLException e) { throw new RuntimeException(e); } } } }
Java
복사

Test

@Test void count() throws SQLException, ClassNotFoundException { User user1 = new User("1", "juwan", "1234"); User user2 = new User("2", "kissup", "2345"); User user3 = new User("3", "jeongseok", "3456"); UserDao userDao = context.getBean("awsUserDao", UserDao.class); userDao.deleteAll(); assertEquals(0, userDao.getCount()); userDao.add(user1); assertEquals(1, userDao.getCount()); userDao.add(user2); assertEquals(2, userDao.getCount()); userDao.add(user3); assertEquals(3, userDao.getCount()); }
Java
복사
수업 로그