Hello,

I have one method for checking fields entered, if they are consistent enough then I am inserting into the database. Another on to check duplicates:

//check fields and if alright insert in table

public void checkFields(String a, String b, String c, String d){

      if(a.length() >= 4 && b.length() >= 4 && c.matches("\\d+") && d.length() >= 4){ 
         goodLength = true;

      }

      if( goodLength) { 
         System.out.println("You entered a good password!");

    String e = cb5.getSelectedItem().toString();
        String f = cb6.getSelectedItem().toString();
        String g = cb7.getSelectedItem().toString();



        Properties conProps = new Properties();
        conProps.setProperty("user", "root");
        conProps.setProperty("password", "root");


        try {
            con = DriverManager.getConnection("jdbc:mysql://localhost:3306/stock", conProps);
            con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE);
        } catch (SQLException ex) {
            Logger.getLogger(NewProduct.class.getName()).log(Level.SEVERE, null, ex);
        }

        String sql = ("INSERT INTO Products(Name, Description, Quantity,Price,  Location, Category, Supplier, Notes, Image, Class1, Class2) VALUES (?,?,?,?,?,?,?,?,?,?,?)");

        try {
            con.setAutoCommit(false);

            st = (PreparedStatement) con.prepareStatement(sql);
            st.setString(1, t1.getText());
            st.setString(2, t2.getText());
            st.setString(3, t3.getText());
             st.setString(4, t4.getText());

            st.setString(5, e);
            st.setString(6, f);
            st.setString(7, g);

            st.setString(8, t12.getText());

            st.setBytes(9, person_image);

            st.setString(10, txt.getText());
            st.setString(11, txt2.getText());


            st.execute();
            st.close();
            con.commit();
            JOptionPane.showMessageDialog(null, "saved!");

        } catch (Exception se) {
            se.printStackTrace();
        }

        updateTable();
        displayImage(t3.getText());



      } else if(!goodLength) {
            JOptionPane.showMessageDialog(null, "Field have to be at least 5 Characters, make sure you enter a correct number in Quantity");



      }
   }

My other method is checkDuplicates of the first field only

 public void checkDuplicates(String pass) {
         ResultSet rs = null;
         Properties conProps = new Properties();
        conProps.setProperty("user", "root");
        conProps.setProperty("password", "root");



     try {
             con = DriverManager.getConnection ("jdbc:mysql://localhost:3306/stock", conProps);
              con.setTransactionIsolation(Connection.TRANSACTION_SERIALIZABLE );
        } catch (SQLException ex) {

        }    
     String sql = ("select * from Products where Name = '" + pass + "'");

        try {

             con.setAutoCommit(false);
            st = (PreparedStatement) con.prepareStatement(sql);
         // st.setString(1,pass);

            st.executeQuery();
            rs = st.getResultSet();
            if(rs.next()){

System.out.print("Exist");
 JOptionPane.showMessageDialog(null, "Product Already Exist!");

            } else{

           checkFields(t1.getText(), t2.getText(), t3.getText(),t4.getText());  

            }

             st.close();
            con.commit();            
}
catch (Exception e) {

    System.out.println(e);
}
       //notifyListener();
}

It does not work, I do not get any error message but I have tested each method differently. They work fine, however when I try to have them like that it does not work.

When I enter a duplicate field I get the error message, when I enter a unique field that is not in the database I get you have entered a good password but not the save part.

firstly, your title makes no sense: you can't declare methods inside methods.
are the above the methods as how they work? if so ... what is the code that doesn't work?
I really do not understand what you mean by this:

They work fine, however when I try to have them like that it does not work.

could you elaborate on that?

Sorry for confusion, I have changed the title. My checkFields method works fine, but as I need to check for duplicates too I have inserted the checkfields method in the Checkduplicates() method. This does not seems to work.

I think I will have to structure my code differently but how? I have no idea

ehm .... how did you combine them? just saying "it doesn't seem to work" doesn't mean much to us, if we don't know what "it" is.
what you could also do (inefficiƫnt, I know) is have those two methods return a boolean , and just call these two methods using the && operator.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.