首页
学习
活动
专区
工具
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,它不会检查第二行。有人能建议我如何克服这个问题吗?

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
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/55878417

复制
相关文章

相似问题

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