자바 레이블이나 텍스트박스에 DB에서 select한 값을 출력하려면 어떻게 해야하나요?

조회수 1615회
@Override 
public void actionPerformed(ActionEvent e) { 
    if(e.getSource() == 버튼) { 
        int i=JOptionPane.showConfirmDialog(this, "투표하시겠습니까?", "확인", JOptionPane.YES_NO_OPTION);

        if(i==0){
            try {
                //String sql = "update VoteNum set count=? where num=?";
                PreparedStatement pstmt=null;
                if(A.isSelected()){
                    ++num0;
                    String sql = "update VoteNum set count="+num0+" where num=1";
                    pstmt=con.prepareStatement(sql);
                    //pstmt.setInt(1, num0);
                    //pstmt.setInt(2, 1);
                    pstmt.executeUpdate(sql);
                    new Result();
                }else if(B.isSelected()){

                    ++num1; 
                    String sql = "update VoteNum set count="+num1+" where num=2";
                    pstmt=con.prepareStatement(sql);
                    //pstmt.setInt(1, num1);
                    //pstmt.setInt(2, 2);
                    pstmt.executeUpdate(sql);
                    new Result();
                }else if(C.isSelected()){

                    ++num2; 
                    String sql = "update VoteNum set count="+num2+" where num=3";
                    pstmt=con.prepareStatement(sql);
                    //pstmt.setInt(1, num2);
                    //pstmt.setInt(2, 3);
                    pstmt.executeUpdate(sql);
                    new Result();
                }else if(D.isSelected()){

                    ++num3;
                    String sql = "update VoteNum set count="+num3+" where num=4";
                    pstmt=con.prepareStatement(sql);
                    //pstmt.setInt(1, num3);
                    //pstmt.setInt(2, 4);
                    pstmt.executeUpdate(sql);
                    new Result();
                }else if(E.isSelected()){

                    ++num4;
                    String sql = "update VoteNum set count="+num4+" where num=5";
                    pstmt=con.prepareStatement(sql);
                    //pstmt.setInt(1, num4);
                    //pstmt.setInt(2, 5);
                    pstmt.executeUpdate(sql);
                    new Result();
                }

            } catch (SQLException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }
        }



위가 DB에 값을 삽입하는 다른 클래스 부분인데 사실 값이 잘 안 들어가서 DB에 넣은 값인데도 값이 계속 변하는 것도 고민입니다 ㅠㅠ;



stmt.executeQuery("select * from VoteNum"); 

            while(rs.next()){
                str=str+"\n"+rs.getInt("count");
                sum=sum+rs.getInt("count");

            }
            JLabel A.setText(str.charAt(1)+"");
            JLabel B.setText(str.charAt(3)+"");
            JLabel C.setText(str.charAt(5)+"");
            JLabel D.setText(str.charAt(7)+"");
            JLabel E.setText(str.charAt(9)+"");

            try {
                rs.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}

간단한 투표형식을 프로그램에 추가하고 싶습니다 아래 소스코드 부분이 출력하는 부분인데 어떻게 넣어야할지 모르겠어서 위의 내용은 꾸역꾸역 일단 오류만 나지않게 값을 넣었더니 str에 계속 null값이 들어가는 중이고 그렇다고 next 안에 바로 label값을 넣자니 계속 레이블 5개에 전부 A값이 들어가서...

어떻게 해야할지 잘 모르겠습니다 ..ㅠㅠ 조언 부탁드립니다.

답변을 하려면 로그인이 필요합니다.

프로그래머스 커뮤니티는 개발자들을 위한 Q&A 서비스입니다. 로그인해야 답변을 작성하실 수 있습니다.

(ಠ_ಠ)
(ಠ‿ಠ)