title: Java :How to connect to database using Jbutton taking user input data
body: <p>In gui user enter username and password then click ok button.But the problem is i don't know how to connect with my Connector Class with ok button. And Give restriction to that button when user enter correct fields then connect to database.<br/>
  Main Method</p>
  
  <pre><code>public class Main {
  
      public static void main(String[] args) {
          Gui obj = new Gui();
          Connector conn = new Connector(obj.Setproperty());  
      }
  
  }
  </code></pre>
  
  <p>Gui Class</p>
  
  <pre><code>public class Gui extends JFrame implements ActionListener{
  
      private JLabel label1;
      private JLabel label2;
      private JTextField t1;
      private JTextField t2;
      private JButton ok;
      private JPanel p1,p2;
  
      Properties property;
  
      public Gui(){
          super("GUI");
          p1 = new JPanel();
          p1.setLayout(new GridLayout(0,2));
          p2=new JPanel();
          label1 = new JLabel("User Name");
          t1 = new JTextField();
          label2 = new JLabel("Password");
          t2 = new JTextField();
          ok= new JButton("OK");
          ok.addActionListener(this);
          add(p1,BorderLayout.NORTH);
          add(p2,BorderLayout.SOUTH);
          p1.add(label1);
          p1.add(t1);
          p1.add(label2);
          p1.add(t2);
          p2.add(ok);
  
          setSize(300,200);
          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
          setLocationRelativeTo(null);
          setResizable(false);
          setVisible(true);
          property = new Properties();
      }
  
      @Override
      public void actionPerformed(ActionEvent e) {
          if(e.getSource()==ok){
          }
  
      }
  
      public Properties Setproperty(){
  
          property.setProperty("UNAME",t1.getText());
          property.setProperty("PWD",t2.getText());
          return property;
      }
  
  }
  </code></pre>
  
  <p>Connector Class</p>
  
  <pre><code>public class Connector {
      Connection conn;
      Statement state;
      String username;
      String pass;
      String url; 
  
      public Connector(Properties p) {
          username = p.getProperty("UNAME");
          pass = p.getProperty("PWD");
          url= "jdbc:mysql://localhost/world";
      }
  
      public boolean open() {
          try {
              conn = DriverManager.getConnection(url, username, pass);
              state = conn.createStatement();
  
          } catch (Exception e) {
              JOptionPane.showMessageDialog(null, e.getMessage());
              e.printStackTrace();
          }
  
          if(conn==null){
              System.out.println("Connection is NULL or server not connected");
              return false;
  
          }
  
          System.out.println("Connection Successfull");
  
          return true;
      }
  }
  </code></pre>