0

Good day

I am still in school studying Java programming, and there is a project I have been working on. Everything has been working fine, however one feature of my program which allows the user to change their login credentials is no longer functioning properly. I don't recall changing anything and it worked fine before.

The problem occurs when I click the "Apply" button I have which executes a SQL query to change the username and password field values in a database. The JFrame freezes and I have to forcibly terminate it via WTM. Also to be noted is that in the console, I get a message in red saying:

Java Result: -805306369

It has that same value every time. I've done some Googling and all that I've found is that problems like the freezing are caused by looping errors, but to my knowledge I don't have any for this particular part of code.

If anyone could help me out I'd be so grateful! Pulling my hair out at this.

Below is my code for the "Apply" button, as well as the SQL query.

private void btnApplyActionPerformed(java.awt.event.ActionEvent evt) {                                         
        try {
            if (txtNewUsername.equals("")) {
                labelFlagUsername.setVisible(false);
                JOptionPane.showMessageDialog(this, "You Need To Fill In The New Username Field", null, JOptionPane.ERROR_MESSAGE);
                System.out.println("You Need To Fill In The New Username Field");
            } else if (txtNewPassword.equals("")) {
                labelFlagPassword.setVisible(false);
                JOptionPane.showMessageDialog(this, "You Need To Fill In The New Password Field", null, JOptionPane.ERROR_MESSAGE);
                System.out.println("You Need To Fill In The New Password Field");
            } else {
                UN = txtNewUsername.getText();//Gets value that user entered into field
                PW = txtNewPassword.getText();//Gets value that user entered into field
                DM.editLoginDetails(UN, PW);//Executes SQL query editLoginDetails
            }

        }
        catch (Exception e)
        {
            JOptionPane.showMessageDialog(this, e.getMessage(), null, JOptionPane.ERROR_MESSAGE);
            System.out.println(e.getMessage());
        }
        txtCurrentUsername.setText("   " + DM.getUsername(UN));
        txtCurrentPassword.setText("   " + DM.getPassword(PW));
        String temp = "";
        txtNewPassword.setText(temp);
        txtNewUsername.setText(temp);
    }

SQL Query:

public String editLoginDetails(String UN, String PW)
    {

        try {
            Statement st = con.createStatement();
            String query = "UPDATE Users SET Username = '" + UN + "',Password = '" + PW + "';";
            st.execute(query);
            ResultSet rs = st.getResultSet();
            JOptionPane.showMessageDialog(null, "Your login details were changed successfully!");
            System.out.println("Your login details were changed successfully!");
        catch (Exception e)
        {
            JOptionPane.showMessageDialog(null, e.getMessage());
            System.out.println(e.getMessage());
        }
        return "Your login details were changed successfully!";
    }

Finally, I thought it would be useful to know what this JFrame looked like. You'll get a better understanding of why my code is the way it is, hopefully.

http://tinypic.com/r/ao9udg/5

I hope somebody can help me! Desperate to fix this annoying issue.

Andrew Thompson
  • 168,117
  • 40
  • 217
  • 433
  • 3
    1) Don't block the EDT (Event Dispatch Thread) - the GUI will 'freeze' when that happens. Instead of calling `Thread.sleep(n)` implement a Swing `Timer` for repeating tasks or a `SwingWorker` for long running tasks. See [Concurrency in Swing](http://docs.oracle.com/javase/tutorial/uiswing/concurrency/) for more details. 2) *"Kind regards, Brandon"* Noise, leave it out. – Andrew Thompson Oct 05 '13 at 16:31
  • 1
    Possible duplicate of [Chat program freezes JFrame](http://stackoverflow.com/questions/16718198/chat-program-freezes-jframe) or [this one](http://stackoverflow.com/q/17627104/418556) or [this one](http://stackoverflow.com/q/14305747/418556) (itself a duplicate of [this one](http://stackoverflow.com/q/14271902/418556)).. – Andrew Thompson Oct 05 '13 at 16:36
  • 1
    *"..useful to know what this JFrame looked like. You'll get a better understanding of why my code is the way it is, hopefully."* Actually all I noted before I commented was the first two words of the title (JFrame freezes..) and the SQL tag. That's enough to identify the likely culprit here. – Andrew Thompson Oct 05 '13 at 16:44
  • 1
    Not necessarily related, but you should really learn prepared statements (because your query will fail if the password contains a quote, for example), and your query updates the name and the password of ALL THE USERS! Moreover, there should be no semi-colon at the end of the query. – JB Nizet Oct 05 '13 at 16:56
  • @ JB Nizet: There is only one user, so no worries :) – Brandon Scott Oct 05 '13 at 19:39
  • @ Andrew Thompson: Thanks I'll take a look and let you know if I sort it out. And seriously? You're calling me out for being polite? Irony right there. – Brandon Scott Oct 05 '13 at 19:40

1 Answers1

0

After failed attempts at trying to correct things I didn't really understand, or thought was wrong in the first place, I finally discovered my problem. All that was happening was that in my JFrame properties, I had set this window to be always on top. When I clicked the apply button, the frame only appeared to freeze because success JOptionPane was being displayed, only it was behind the GUI, so I couldn't respond to it. After disabling the 'Always On Top' option, it now works perfectly.

Thanks for all the suggestions though! :)