Hello, I've made registering system to my little test program, It works well until you try to create two accounts with same name, It shouldn't allow it but it still does it. I have tried to solve this problem all day now by trying diffrent methods but sadly nothing helped. So if any of you geniuses could help me out.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * Regform.java
 *
 * Created on 21.8.2009, 14:29:46
 */

/**
 *
 * @author Samuli
 */

import java.sql.*;
import java.util.logging.Level;
import java.util.logging.Logger;

public class Regform extends javax.swing.JFrame {

    /** Creates new form Regform */
    public Regform() {
        initComponents();
        this.setLocation(800, 400);
        this.setTitle("Registeration");
        this.setResizable(false);
    }


    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")

    public static boolean Cancreate = true;

    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        jSeparator1 = new javax.swing.JSeparator();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        Password = new javax.swing.JPasswordField();
        Name = new javax.swing.JTextField();
        Key = new javax.swing.JTextField();
        Proceed = new javax.swing.JButton();
        Cancel = new javax.swing.JButton();
        Status = new javax.swing.JLabel();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        jLabel1.setText("Registeration");

        jLabel2.setText("Welcome to registeration if you don't know what to do, go away");

        jLabel3.setText("Username:");

        jLabel4.setText("Password:");

        jLabel5.setText("Registeration Key:");

        Proceed.setText("Proceed");
        Proceed.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                ProceedActionPerformed(evt);
            }
        });

        Cancel.setText("Cancel");
        Cancel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                CancelActionPerformed(evt);
            }
        });

        Status.setText("Status text");

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(44, 44, 44)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jLabel5)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(Key, javax.swing.GroupLayout.PREFERRED_SIZE, 236, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                        .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(jLabel3)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(Name))
                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                    .addComponent(jLabel4)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                    .addComponent(Password, javax.swing.GroupLayout.PREFERRED_SIZE, 93, javax.swing.GroupLayout.PREFERRED_SIZE)))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                                .addComponent(Cancel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(Proceed, javax.swing.GroupLayout.DEFAULT_SIZE, 91, Short.MAX_VALUE)))
                        .addComponent(jLabel2, javax.swing.GroupLayout.Alignment.LEADING)))
                .addContainerGap(61, Short.MAX_VALUE))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, 356, javax.swing.GroupLayout.PREFERRED_SIZE))
            .addGroup(layout.createSequentialGroup()
                .addGap(104, 104, 104)
                .addComponent(Status, javax.swing.GroupLayout.DEFAULT_SIZE, 320, Short.MAX_VALUE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(16, 16, 16)
                .addComponent(Status)
                .addGap(2, 2, 2)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jSeparator1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addComponent(jLabel2)
                .addGap(27, 27, 27)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(Name, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(Proceed))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(Cancel)
                    .addComponent(Password, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(Key, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>                        

    @SuppressWarnings("empty-statement")
    private void ProceedActionPerformed(java.awt.event.ActionEvent evt) {                                        
        

        Connection con = null;

        try {
        Class.forName("com.mysql.jdbc.Driver");
        } catch(Exception e) {
            return;
        }

        try {

		 con = DriverManager.getConnection("jdbc:mysql://MY IP/breakpoint","root", "Secret");
                 Statement st = con.createStatement ();
                 Statement stm = con.createStatement();
                 Statement st2 = con.createStatement();
                 st.executeQuery("SELECT Users.Username,Users.Password,Avaimet.Avainrekisteriin FROM Users INNER JOIN Avaimet ");
                 ResultSet rss = st.getResultSet();

                 String Accname = Name.getText();
                 String Accpass = Password.getText();
                 String Regkey = Key.getText();
                 boolean Created = false;

                 while ( rss.next() )
                 {
                     String name = rss.getString("Username");
                     String key = rss.getString("Avainrekisteriin");


                     if (Accname.length() <= 3)
                         Status.setText("Your account name has to be longer than 3 letters");

                     else if (Accpass.length() <= 3)
                         Status.setText("Your account password has to be longer than 3 letters");



                         if ( Created == false && Accname.equalsIgnoreCase(name) && Regkey.equalsIgnoreCase(key)  )
                         {
                            String Deletekey = "DELETE FROM Avaimet WHERE Avainrekisteriin = '"+key+"'";
                            String StrSQL = "INSERT INTO Users (Username,Password,Banned,Level,Admin) " +
                                    "VALUES ('"+Accname+"','"+Accpass+"',"+0+",'3','0')";
                            stm.executeUpdate(StrSQL);
                            st2.executeUpdate(Deletekey);
                            Created = true;
                            System.out.print("ACC CREATED");
                            Status.setText("Account created!");
                            Form1.Status.setText("Account created!");
                            this.dispose();
            
                         }
                         else
                         {
                            Status.setText("Something failed!");
                         }

                 }


                 st.close();
                 rss.close();





	  } catch (SQLException e) {
	    System.out.println("Connection Failed! Check output console");
	    e.printStackTrace();
	    return;
	  }
    }                                       

    private void CancelActionPerformed(java.awt.event.ActionEvent evt) {                                       
        this.dispose();
    }                                      

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new Regform().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify                     
    private javax.swing.JButton Cancel;
    private javax.swing.JTextField Key;
    private javax.swing.JTextField Name;
    private javax.swing.JPasswordField Password;
    private javax.swing.JButton Proceed;
    private javax.swing.JLabel Status;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JSeparator jSeparator1;
    // End of variables declaration                   

}

I think that maybe that INNER JOIN affects it somehow, I tried with left and right but then it gives sql error. Im hopeless.
By the way if you need to test the program in it self add me in msn samppa.l@hotmail.com.

Recommended Answers

All 2 Replies

hi, i could not test your program because it did not compile..but i think your problem could be in compairing objects rather than their values.
if ( Created == false && Accname.equalsIgnoreCase(name) && Regkey.equalsIgnoreCase(key) )

i think your problem could be in this line..here you are comparing if the two objects(in this case Accname and name) are the same object. You should try comparing the values. An '==' operator would do that.
Hope this helpss

I will try it and it didn't compile because it takes values from other file I have.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.