不要将重复值插入mysql

时间:2014-04-22 22:47:33

标签: mysql

我想在字段中插入没有重复值的mysql中的用户" User" 。如何在用户写一个"用户"在完成其余字段之前,在他点击按钮Login?

之前,表已经存在于表中
 public void Registrar() throws SQLException {

        Connection con = null;
        ResultSet rs = null;
        Statement st = null;

        String user;
        String pass;
        String pass2;
        String email;
        String country;

        user = jTextField1.getText();
        pass = jPasswordField1.getText();
        pass2 = jPasswordField2.getText();
        email = jTextField2.getText();
        country = jComboBox1.getSelectedItem().toString();


  if (user.isEmpty()) {
            jLabel8.setText("Please enter an Username");
        } else if (pass.isEmpty()) {

            jLabel8.setText("Please enter an password");

        } else if (pass2.isEmpty()) {
            jLabel8.setText("please you must confirm your password");

        } else if (!pass2.equals(pass)) {
            jLabel8.setText("passwords do not match");

        } else if (email.isEmpty()) {
            jLabel8.setText("Please enter an valid email");

        } else if (country.equals("Choose your country")) {
            jLabel8.setText("Please choose a Country");

        } else {

               String sql = "INSERT INTO usuarios(User,Pass,Email,Country) "
                    + "VALUES('" + user + "','" + pass + "','" + email + "','" + country + "')";

            try{


                Class.forName("com.mysql.jdbc.Driver");
                con = DriverManager.getConnection("jdbc:mysql://localhost/whichmovie", "Asis", "dekrayat24");
                System.out.println("Conectado a la base de datos SQLite");

                PreparedStatement pstm = con.prepareStatement(sql);
                pstm.execute();
                pstm.close();
                JOptionPane.showMessageDialog(rootPane, "Congratulations you have created your account");
                dispose();
                Login l = new Login(null, rootPaneCheckingEnabled);
                l.setVisible(true);
            } catch (Exception e) {

                JOptionPane.showMessageDialog(rootPane, "An error has occurred ,please try later");
            }

        }
    }

0 个答案:

没有答案