检查重复的两个以上的值,如果没有这样的重复,则插入查询

内容来源于 Stack Overflow,并遵循CC BY-SA 3.0许可协议进行翻译与使用

  • 回答 (1)
  • 关注 (0)
  • 查看 (9)

注意:

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

我插了两行

第一排,在哪里name=ema email=ema@gmail.com,我的第二排在哪里name=ena email=fe

现在,当我想插入一个新记录时,它只检查第一行并且检查工作,但是如果我想插入它name=ena并且email=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();
    }
提问于
用户回答回答于

最后,我已经找到了逻辑,我只需要为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"); 
    } 

所属标签

可能回答问题的人

  • 嗨喽你好

    7 粉丝480 提问9 回答
  • uncle_light

    5 粉丝518 提问8 回答
  • 富有想象力的人

    3 粉丝0 提问7 回答
  • 无聊至极

    4 粉丝504 提问6 回答

扫码关注云+社区

领取腾讯云代金券