检查重复项是否包含两个以上的值,如果没有重复项,则插入查询

时间:2019-04-27 07:47:24

标签: java

注意:

我有两个名称(主键),电子邮件(主键))

我插入了两行

第一行,name=ema email=ema@gmail.com,第二行,name=ena email=fe

现在,当我要插入新记录时,它仅检查第一行,并且检查有效,但是如果我要插入name=enaemail=something,则不检查第二行。有人可以建议我如何克服这个问题吗?

try
        {
        Connection connection=DriverManager.getConnection("jdbc:mysql://localhost:3306/testing","root","");

    //block of code to check user exists or not.
    //Statement statement = connection.createStatement();
    PreparedStatement Pstatement;

    String query = "select Name,Email from detail";
    PreparedStatement ps = connection.prepareStatement(query);
    ResultSet rs = ps.executeQuery() ;


    if(rs.next())
    {
        //from database
        String name_db1 = rs.getString("Name").trim(); //using trim removes all white spaces
        String email_db2 = rs.getString("Email").trim();

        //from user GUI
        String entered_name = name.getText().trim(); //using trim removes all white spaces
        String entered_email = email.getText().trim();

        boolean valid = true;

        if(entered_name.equals(""))
        {
            JOptionPane.showMessageDialog(null,"Enter name");
            valid = false;
        }
        else if(name_db1.equals(entered_name))
        {
            JOptionPane.showMessageDialog(null,"Enter name taken");
            name.setText(null);
            valid = false;
        }
        else if(entered_email.equals(""))
        {
            JOptionPane.showMessageDialog(null,"Enter email");
            valid = false;
        }
        else if(email_db2.equals(entered_email))
        {
            JOptionPane.showMessageDialog(null,"email taken");
            email.setText(null);
            valid = false;
        }
        else if(valid == true)
        {
            Pstatement=connection.prepareStatement("insert into detail values(?,?)");

            //Specifying the values of preparestatement parameter                  
            Pstatement.setString(1,name.getText());
            Pstatement.setString(2,email.getText());
            Pstatement.executeUpdate();
            JOptionPane.showMessageDialog(null,"registration successful");  
            //x++;
        }

    }
    else
    {
        //incase if the user click without filling up the fields
        JOptionPane.showMessageDialog(null,"not yet registered"); 
    }
    }
    catch(SQLException e)
    {
        e.printStackTrace();
    }

1 个答案:

答案 0 :(得分:0)

最后,我已经弄清楚了逻辑,我只需要为Name和Email创建一个单独的查询。这样,我可以搜索两个以上的值:D。如果有任何错误,请通知我。

    try
    {            
        Class.forName("com.mysql.jdbc.Driver");
        Connection con = (Connection) DriverManager.getConnection("jdbc:mysql://localhost:3306/testing","root",""); 
        //creating a query for Name
        String query1 = "select  Name, Email from detail where Name like '"+name.getText()+"'";
        PreparedStatement statement1 = con.prepareStatement(query1);
        //creating a query for Email
        String query2 = "select  Name, Email from detail where Email like '"+email.getText()+"'";
        PreparedStatement statement2 = con.prepareStatement(query2);

        ResultSet result1 = statement1.executeQuery(); //resultset for name
        ResultSet result2 = statement2.executeQuery(); //resultset for email
        //checking name exception
        if (result1.next())
        {   
            String dbasename=result1.getString("Name").toString().trim();               
            String enteredname=new String(name.getText().trim());

            if(enteredname.equals(""))
            {
                JOptionPane.showMessageDialog(null, "enter name");//valid1 = false;
            }
            else if(dbasename.equals(enteredname))
            {
                JOptionPane.showMessageDialog(null, "name taken");//valid1 = false;
                name.setText(null);
            }                
        }
        //checking email exception
        else if(result2.next())
        {                  
            String dbaseemail=result2.getString("Email").toString().trim();
            String enteredemail=new String(email.getText().trim());

            if(enteredemail.equals(""))
            {
                JOptionPane.showMessageDialog(null, "enter email");//valid1 = false;
            }
            else if(dbaseemail.equals(enteredemail))
            {
                JOptionPane.showMessageDialog(null, "email taken");//valid1 = false;
                email.setText(null);
            }                            
        }
        //if no exception is detect exectute the below statement
        else
        {
                PreparedStatement Pstatement=con.prepareStatement("insert into detail values(?,?)");                 
                Pstatement.setString(1,name.getText());
                Pstatement.setString(2,email.getText());
                Pstatement.executeUpdate();
                JOptionPane.showMessageDialog(null,"Registered Successfully");
        }
        statement1.close();
        statement2.close();
        con.close(); 
    }
    catch(SQLException se){
         se.printStackTrace();
    }
    catch(Exception e)
    {
        e.printStackTrace();     
        JOptionPane.showMessageDialog(null, "error during searching"); 
    }