package com.bean;
public class User {
private String id;
private String name;
private String password;
public User() {
}
public User(String id, String name, String password) {
this.id = id;
this.name = name;
this.password = password;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
UserDao.java
package com.dao;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import javax.naming.Context;
import javax.naming.InitialContext;
import javax.sql.DataSource;
import com.bean.User;
public class UserDao {
private Connection getConnection() throws Exception {
Connection con = null;
// 1 드라이버 로딩
Class.forName("oracle.jdbc.driver.OracleDriver");
// 2 DB연결
con = DriverManager.getConnection("jdbc:oracle:thin:@127.0.0.1:1521:xe", "jspid", "jsppass");
return con;
}
// import 단축키 ctrl shift o
public void add(User user) throws Exception {
Connection c = this.getConnection();
PreparedStatement ps = c.prepareStatement(
"insert into users(id, name, password) values(?,?,?)");
ps.setString(1, user.getId());
ps.setString(2, user.getName());
ps.setString(3, user.getPassword());
ps.executeUpdate();
ps.close();
c.close();
}
public User get(String id) throws Exception {
Connection c = this.getConnection();
PreparedStatement ps = c.prepareStatement("select * from users where id = ?");
ps.setString(1, id);
ResultSet rs = ps.executeQuery();
User user = null;
if (rs.next()) {
user = new User();
user.setId(rs.getString("id"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
}
rs.close();
ps.close();
c.close();
return user;
}
public User get2(String id) throws Exception {
Connection c = this.getConnection();
PreparedStatement ps = c.prepareStatement("select * from users where id = ?");
ps.setString(1, id);
ResultSet rs = ps.executeQuery();
User user = null;
if (rs.next()) {
user = new User();
user.setId(rs.getString("id"));
user.setName(rs.getString("name"));
user.setPassword(rs.getString("password"));
}
rs.close();
ps.close();
c.close();
if (user == null) {
throw new SQLException("해당되는 user 정보없음");
}
return user;
}
public void deleteAll() throws Exception {
Connection c = getConnection();
PreparedStatement ps = c.prepareStatement("delete from users");
ps.executeUpdate();
ps.close();
c.close();
}
public int getCount() throws Exception {
Connection c = getConnection();
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;
}
}
UserDaoTest.java
package com.dao;
import static org.junit.Assert.*;
import org.junit.Test;
import org.junit.runner.JUnitCore;
import com.bean.User;
public class UserDaoTest {
UserDao userDao = new UserDao();
@Test
public void addAndGet() throws Exception {
User user1 = new User("gyumee", "박성철", "springno1");
User user2 = new User("leegw700", "이길원", "springno2");
userDao.deleteAll();
assertEquals(0, userDao.getCount());
userDao.add(user1); // 검증
userDao.add(user2);
assertEquals(2, userDao.getCount());
User userget1 = userDao.get(user1.getId());
assertEquals(user1.getName(), userget1.getName());
assertEquals(user1.getPassword(), userget1.getPassword());
User userget2 = userDao.get(user2.getId());
assertEquals(user2.getName(), userget2.getName());
assertEquals(user2.getPassword(), userget2.getPassword());
}
@Test
public void count() throws Exception {
User user1 = new User("gyumee", "박성철", "springno1");
User user2 = new User("leegw700", "이길원", "springno2");
User user3 = new User("bumjin", "박범진", "springno3");
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());
}
@Test
public void getUserNull() throws Exception {
userDao.deleteAll();
assertEquals(0, userDao.getCount());
assertNull(userDao.get("unknown_id"));
}
@Test(expected=Exception.class)
public void getUserException() throws Exception {
userDao.deleteAll();
assertEquals(0, userDao.getCount());
userDao.get2("unknown_id");
}
public static void main(String[] args) {
JUnitCore.main("com.dao.UserDaoTest");
}
}
DateUtil.java
package com.gilbut;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
public class DateUtil {
// 20181114
public static String getCurDateByCalendar() {
StringBuffer sb = new StringBuffer();
Calendar cal = Calendar.getInstance();
sb.append(cal.get(Calendar.YEAR));
sb.append(String.format("%02d", cal.get(Calendar.MONTH) + 1));
sb.append(String.format("%02d", cal.get(Calendar.DATE)));
return sb.toString();
}
public String getCurDateByDate() {
Date date = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMdd");
String str = sdf.format(date);
return str;
}
}
package com.gilbut;
import static org.junit.Assert.*;
import org.junit.Assert;
import org.junit.Test;
// 애노테이션 기반으로 테스트케이스 구현
public class DateUtilTest2 {
// 테스트 대상(target) 객체 준비
DateUtil dateUtil = new DateUtil();
@Test
public void getCurDateByCalendar() {
String strDate = dateUtil.getCurDateByCalendar();
assertNotNull(strDate);
assertEquals(dateUtil.getCurDateByDate(), strDate);
}
@Test
public void getCurDateByDate () {
assertEquals("20181114", dateUtil.getCurDateByDate());
}
}
'IT > Java' 카테고리의 다른 글
java - 파일입출력 (0) | 2018.11.16 |
---|---|
Java - JUnit 픽스처, setUp, tearDown, Before, After, BeforeClass, AfterClass (0) | 2018.11.14 |
자바 이벤트, 간단한 계산기 연습, 메세지 다이얼로그 (0) | 2018.08.07 |
자바 마우스 이벤트, MouseListener, MouseAdapter (0) | 2018.08.07 |
자바 이벤트 ActionListener, 독립클래스, 멤버내부클래스, 지역내부클래스, 익명클래스 (0) | 2018.08.07 |