title: Check if String in JTextField is equal to VarChar in MySql Database
body: <p>I am trying to insert a new record into a database. Input the primary key of the record manually into a JTextField in my gui. I have a button labeled "Check" beside the JTextField. When I input the primary key into the textfield I want to click on the "Check" button to check if that primary key already exists in the database as I do not want a duplicate primary key.</p>
  
  <p>For example if I have 6 records in the database. The primary keys are (a,b,c,d,e,f). If the check scans through the existing primary keys I want the check/scan to stop if it finds a match (one that is the same as the one in TextField). The problem is that is does not checking/scanning after its found a match. This is a problem as I want a little tick to appear beside the textfield only if there is no match. I am using a System.out.print to see if the results from the db equal the TextField. The System.out.print looks like</p>
  
  <pre><code>no match
  no match 
  no match 
  no match 
   match   
  no match
  
      String url = "jdbc:mysql://localhost:3306/";
      String dbName = "dublinzoo";
      String driver = "com.mysql.jdbc.Driver";
      String userName = "root";
      String password = "root";
  
      //   String str = AnimalIdTf.getText().toString();
      try {
          Class.forName(driver).newInstance();
          Connection conn = DriverManager.getConnection(url + dbName, userName, password);
          System.out.println("Database Connection Successful");
  
          // Set up a create statement
          java.sql.Statement stmn = conn.createStatement();
  
          // Set Up SQL
          String sql = "SELECT MAMMAL_ID FROM MAMMAL GROUP BY MAMMAL_ID";
  
          // Excute Query and send result set to rs
          ResultSet rs = stmn.executeQuery(sql);
  
          while (rs.next()) {
  
          // Retrieve by column name
          String MAMMAL_ID = rs.getString("MAMMAL_ID");
  
          String str = AnimalIdTf.getText().toString();
  
          if (MAMMAL_ID.equals(str)) {
              System.out.println("match");
              JOptionPane.showMessageDialog(rootPane, "Animal id already exists!" + "\n" + "Please enter a different id");
              AnimalIdTf.setText("");
          } else { 
              System.out.println(" no match");
              tickLabel.setVisible(true);
              JOptionPane.showMessageDialog(rootPane, "id ok");
          }
  
          // Close the connection to the database
          conn.close();
          }
      } catch (Exception e) {
          e.printStackTrace();
      }
  </code></pre>