영보의 SystemOut.log

[Java] db와 연동하여 우편번호 검색 프로그램 만들기 본문

Language/JAVA

[Java] db와 연동하여 우편번호 검색 프로그램 만들기

영보로그 2020. 8. 5. 17:15
반응형

ZipcodeVO.java

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
package com.sist.dao;
// VO는 클래스가 아니라 사용자정의 데이터형이다 
 
public class ZipcodeVO {
    private String zipcode; //우편번호
    private String sido;
    private String gugun;
    private String dong;
    private String bunji;
    private String address;
    public String getZipcode() {
        return zipcode;
    }
    public void setZipcode(String zipcode) {
        this.zipcode = zipcode;
    }
    public String getSido() {
        return sido;
    }
    public void setSido(String sido) {
        this.sido = sido;
    }
    public String getGugun() {
        return gugun;
    }
    public void setGugun(String gugun) {
        this.gugun = gugun;
    }
    public String getDong() {
        return dong;
    }
    public void setDong(String dong) {
        this.dong = dong;
    }
    public String getBunji() {
        return bunji;
    }
    public void setBunji(String bunji) {
        this.bunji = bunji;
    }
    public String getAddress() {
        return sido+" "+gugun+" "+dong+" "+bunji;
    }
}
cs

 

 

ZipcodeDAO

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
56
57
58
59
60
61
62
63
64
65
package com.sist.dao;
import java.sql.*;
import java.util.*;
public class ZipcodeDAO {
    // 연결
    private Connection conn;
    // 문장 전송 => SQL
    private PreparedStatement ps;
    //연결 => 오라클 주소
    private final String URL="jdbc:oracle:thin:@localhost:1521:XE";
    
    // 드라이버 등록
    public ZipcodeDAO()
    {
        try {
            Class.forName("oracle.jdbc.driver.OracleDriver");
        }catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }
    // 연결
    public void getConnection() {
        try {
            conn=DriverManager.getConnection(URL,"hr","happy");
            //conn hr/happy
        }catch(Exception ex) {}
    }
    //닫기
    public void disConnection() {
        try {
            if(ps!=null)ps.close();
            if(conn!=null)conn.close();
            //exit
        }catch(Exception ex) {}
    }
    // 우편번호 찾기
    public ArrayList<ZipcodeVO> postfind(String dong){
        ArrayList<ZipcodeVO> list=
                new ArrayList<ZipcodeVO>();
        try {
            // 연결
            getConnection();
            // SQL 문장 전송
            String sql="SELECT * FROM zipcode "+"WHERE dong LIKE '%'||?||'%'";
            ps=conn.prepareStatement(sql);
            ps.setString(1, dong);
            ResultSet rs=ps.executeQuery();//실행
            while(rs.next()){
                ZipcodeVO vo=new ZipcodeVO();
                vo.setZipcode(rs.getString(1));
                vo.setSido(rs.getString(2));
                vo.setGugun(rs.getString(3));
                vo.setDong(rs.getString(4));
                vo.setBunji(rs.getString(5));
                
                list.add(vo);
            }
        }catch(Exception ex) {
            System.out.println(ex.getMessage());
        }finally {
            disConnection();
        }return list;
    }
}
cs

- VO와 DAO 클래스로 오라클과 연결 해 줍니다.

 

 

PostMain.java

 

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
56
57
58
59
60
61
62
package com.sist.user;
import com.sist.dao.*;
import java.util.*;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.table.*;
public class PostMain extends JFrame implements ActionListener{
    JTextField tf;
    JLabel la;
    DefaultTableModel model;
    JTable table;
    public PostMain() {
        tf=new JTextField(15);
        la=new JLabel("입력");
        
        String[] col= {"우편번호","주소"};
        String[][] row=new String[0][2];
        
        model=new DefaultTableModel(row,col);
        table=new JTable(model);
        JScrollPane js=new JScrollPane(table);
        JPanel p=new JPanel();
        p.add(la);
        p.add(tf);
        add("North",p);
        add("Center",js);
        
        setSize(450500);
        setVisible(true);
        
        tf.addActionListener(this);
    }
    public static void main(String[] args) {
        new PostMain();
    }
    
    @Override
    public void actionPerformed(ActionEvent e) {
        // TODO Auto-generated method stub
        if(e.getSource()==tf) {
            String dong=tf.getText();
            if(dong.length()<1)//입력이 안된 경우
            {
                JOptionPane.showMessageDialog(this"동/읍/면을 입력하세요");
                return;
            }
            //처리
            for(int i=model.getRowCount()-1;i>=0;i--) {
                model.removeRow(i);
            }
            ZipcodeDAO dao=new ZipcodeDAO();
            ArrayList<ZipcodeVO> list=dao.postfind(dong);
            
            // 출력
            for(ZipcodeVO vo:list) {
                String[] data= {vo.getZipcode(), vo.getAddress()};
                model.addRow(data);
            }    
        }
    }
}
cs

- main클래스에서 UI를 만들어줍니다.

 

 

 

 

 

 

실행 화면

 

커서에 아무것도 안쓰고 엔터키를 누르면 뜨는 창

 

 

반응형