본문 바로가기
Programming/토비의 스프링 예제

예제1

by 막이 2014. 8. 18.

사용자 정보 저장용 자바빈 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
package springbook.user.domain;
 
public class User {
    
        String id;
        String name;
        String 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;
        }
 
}
 



JDBC를 이용한 등록과 조회 기능이 있는 UserDAO 클래스

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
package springbook.user.dao;
 
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
 
import springbook.user.domain.User;
 
public class UserDao {
    
    public void add(User user) throws ClassNotFoundException, SQLException{
        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection c = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:testDB""otep""otep");
        
        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 ClassNotFoundException, SQLException{
        Class.forName("oracle.jdbc.driver.OracleDriver");
        Connection c = DriverManager.getConnection("jdbc:oracle:thin:@localhost:1521:testDB""otep""otep");
        
        PreparedStatement ps = c.prepareStatement("SELECT * FROM USERS WHERE id = ?");
        ps.setString(1, id);
        
        ResultSet rs = ps.executeQuery();
        rs.next();
        
        User 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;
        
    }
 
 




테스트용 main 메소드

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
package springbook.user.test;
 
import java.sql.SQLException;
 
import springbook.user.dao.UserDao;
import springbook.user.domain.User;
 
public class UserTest {
 
    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        UserDao dao = new UserDao();
        
        User user = new User();
        user.setId("otep");
        user.setName("막이");
        user.setPassword("password");
        
        dao.add(user);
        
        System.out.println(user.getId()+" 등록 성공 ");
        
        User user2 = dao.get(user.getId());
        System.out.println(user2.getName());
        System.out.println(user2.getPassword());
        System.out.println(user2.getId()+" 조회 성공 ");
 
    }
 
}