首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >检查两个以上值的重复项,如果没有重复项,则插入查询

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

Stack Overflow用户
提问于 2019-04-27 15:47:24
回答 1查看 38关注 0票数 0

备注:

我有两个键列,(Name(primary键),Email__(primary键)

我已经插入了两行的

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

现在,当我想要插入一个新的记录时,它只检查第一行,并且检查起作用,但是如果我想要插入name=enaemail=something,它不会检查第二行。有人能建议我如何克服这个问题吗?

代码语言:javascript
复制
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();
    }
EN

回答 1

Stack Overflow用户

发布于 2019-04-28 01:24:25

最后,我已经弄清楚了逻辑,我只需要为姓名和电子邮件创建一个单独的查询。这样我可以搜索两个以上的值:D.如果有任何错误,请让我知道。

代码语言:javascript
复制
    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"); 
    } 
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55878417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档