2017-02-10 4 views
1

저는 3 개의 테이블 국가 (country_name), state (state_name, country_name), city (city_name, state_name)을 포함하는 country라는 데이터베이스를 가지고 있으며 ountry, state 및 city를 다음과 같이 추가하려고합니다. 이 프레임하지만 컨트롤의 actionPerformed에서 if 문 2()컨트롤이 if 문을 통과하지 않습니다.

here is the image of console and frame

import java.awt.*; 
import java.awt.event.*; 
import java.sql.*; 

public class Admin extends Frame implements ActionListener 
{ 
    MenuBar mb; 
    Menu edit; 
    MenuItem country,state,city; 
    Label l1,l2,l3,l4,l5; 
    TextField t1,t2,t3; 
    Button b1,b2,b3; 
    Choice ch1,ch2; 
    String str1,str2,str3,str4; 
    Connection con; 
    PreparedStatement ps; 
    ResultSet rs; 

    public Admin() 
    { 
     super("Admin"); 

     setLayout(null); 
     setSize(700,700); 
     mb=new MenuBar(); 
     edit=new Menu("Edit"); 
     country=new MenuItem("Add Country"); 
     state=new MenuItem("Add State"); 
     city=new MenuItem("Add City"); 
     edit.add(country); 
     edit.add(state); 
     edit.add(city); 
     mb.add(edit); 

     country.addActionListener(this); 
     state.addActionListener(this); 
     city.addActionListener(this); 
     setMenuBar(mb); 
     setVisible(true); 
     try 
     { 
      Class.forName("net.ucanaccess.jdbc.UcanaccessDriver"); 
          con=DriverManager.getConnection 
("jdbc:ucanaccess://D:/java/country.mdb"); 
     } 
     catch(Exception e){} 
    } 
    public void actionPerformed(ActionEvent ae) 
    { 
     if(ae.getSource()==country){ 
      removeAll(); 
      l1=new Label("Country Name"); 
      t1=new TextField(20); 
      b1=new Button("Update"); 
      l1.setBounds(150,100,100,25); 
      t1.setBounds(290,100,100,25); 
      b1.setBounds(280,140,70,25); 

      add(l1); 
      add(t1); 
      add(b1); 
      b1.addActionListener(this); 
      setVisible(true); 
      //System.out.println("Outside of if"); 

      if(ae.getSource()==b1){ 
       System.out.println("Inside of if"); 
       try 
       { 
        ps=con.prepareStatement("insert into country 
    (country_name) values('"+t1.getText()+"')"); 
        ps.executeUpdate(); 

       } 
       catch(Exception e){} 
      } 
      //Add_country ob=new Add_country(); 
      //ob.setVisible(true); 
     } 
     else if(ae.getSource()==state){ 
      removeAll(); 
      l2=new Label("Select Country"); 
      ch1=new Choice(); 
      l3=new Label("Add State"); 
      t2=new TextField(); 
      b2=new Button("Update"); 

      l2.setBounds(150,100,100,25); 
      ch1.setBounds(290,100,100,25); 
      l3.setBounds(150,160,100,25); 
      t2.setBounds(290,160,100,25); 
      b2.setBounds(280,200,70,25); 

      add(l2); 
      add(ch1); 
      add(l3); 
      add(t2); 
      add(b2); 
      b2.addActionListener(this); 
      setVisible(true); 

      try 
      { 
       ps=con.prepareStatement("select country_name from country"); 
       rs=ps.executeQuery(); 
       while(rs.next()) 
       { 
        str1=rs.getString("country_name"); 
        ch1.addItem(""+str1); 
       } 
       if(ae.getSource()==b2){ 
        System.out.println("sdfghjk"); 
        str2=ch1.getSelectedItem(); 
        ps=con.prepareStatement("insert into state 
    (state_name,country_name) values('"+t2.getText()+"','"+str2+"')"); 
        ps.executeUpdate(); 


       } 
      } 
      catch(Exception e){} 
      //Add_state obj=new Add_state(); 
      //obj.setVisible(true); 
     } 
     else if(ae.getSource()==city){ 
      removeAll(); 
      l4=new Label("Select State"); 
      ch2=new Choice(); 
      l5=new Label("Add City"); 
      t3=new TextField(); 
      b3=new Button("Update"); 

      l4.setBounds(150,100,100,25); 
      ch2.setBounds(290,100,100,25); 
      l5.setBounds(150,160,100,25); 
      t3.setBounds(290,160,100,25); 
      b3.setBounds(280,200,70,25); 

      add(l4); 
      add(ch2); 
      add(l5); 
      add(t3); 
      add(b3); 
      b3.addActionListener(this); 
      setVisible(true); 
      try 
      { 
       ps=con.prepareStatement("select state_name from state"); 
       rs=ps.executeQuery(); 
       while(rs.next()) 
       { 
        str3=rs.getString("state_name"); 
        ch2.addItem(""+str3); 
       } 

       if(ae.getSource()==b3){ 
        System.out.println("sdfghjk"); 
        str4=ch2.getSelectedItem(); 
        ps=con.prepareStatement("insert into city 
    (city_name,state_name) values('"+t3.getText()+"','"+str4+"')"); 
        ps.executeUpdate(); 
       } 
      } 
      catch(Exception e){} 
      //Add_city obj=new Add_city(); 
      //obj.setVisible(true); 
     } 
    } 
    public static void main(String s[]) 
    { 
     Admin ob=new Admin(); 
    } 
} 
+0

당신이 확인 했습니까? –

+0

첫 번째 if 문은 실행 중이지만 중첩 된 if는 작동하지 않습니다. – PanicLion

+1

== 연산자를 사용하여 Object를 비교할 수 없습니다. equals() - 메서드를 사용해야합니다. 따라서 "ae.getSource() == state"- 비교 결과가 예상 결과와 함께 나타나지 않습니다. – DiabolicWords

답변

1

이 ...

if(ae.getSource()==country){ 
    //... 
    if(ae.getSource()==b1){ 

요, 불가능을 통과하지 않습니다 u는 두 개의 서로 다른 컨트롤에 의해 트리거되는 ActionEvent이 (가 하나씩 발생하는 것)

관찰

하나의 actionPerformed 이벤트를 생성 할 수 없습니다
  • null 레이아웃을 사용하지 않는, 완벽한 픽셀 레이아웃은있다 현대 UI 디자인 내 환상. 구성 요소의 개별 크기에 영향을주는 요소가 너무 많습니다. 어느 요소도 제어 할 수없는 요소가 너무 많습니다. 스윙은 핵심 레이아웃 관리자와 함께 작동하도록 설계되었습니다. 이러한 문제를 해결하면 더 이상 문제를 해결할 수 없으므로
  • AWT가 오래되었습니다. 스윙에 대한 경험이 많습니다. 아무 일도없고 심지어 JavaFX
  • 당신은 (데이터베이스) 리소스를 닫지 않아서 데이터베이스에 문제가 생길 수 있습니다. 나는 또한 구성 요소와 전망
  • 의 더 나은 관리를 제공하기 위해 How to Use CardLayout을 권하고 싶습니다 가능한 솔루션
  • 에 대한 The try-with-resources Statement에서보세요
==이의 MenuItem 개체를 비교하여 작동하는 경우
관련 문제