Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6137011
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:40:27+00:00 2026-05-23T17:40:27+00:00

It’s me again and I just can’t seem to get this code to work.

  • 0

It’s me again and I just can’t seem to get this code to work. I’m basically asking for any advice on why the button does nothing when clicked. Would you like me to attach the source code?

The method I’m trying to implement:

   public static void UserInput() {
      try {
         stmt = connect.createStatement();
         ResultSet res = stmt.executeQuery("SELECT * FROM " + tableName);
         while (res.next()) {
            if (res.getString("Username").equals(usernameField.getText())) {
               if (res.getString("Password").equals(passwordField.getPassword())) {
                  JOptionPane.showMessageDialog(null, "Correct", "Correct",
                           JOptionPane.INFORMATION_MESSAGE);
               } else {
                  JOptionPane.showMessageDialog(null, "Error. Incorrect "
                           + "username or password.", "Error",
                           JOptionPane.ERROR_MESSAGE);
               }
            } else {
               JOptionPane.showMessageDialog(null, "Error. Incorrect "
                        + "username or password.", "Error",
                        JOptionPane.ERROR_MESSAGE);
            }
         }
         res.close();
         stmt.close();
         connect.close();

      } catch (SQLException sqlExcept) {
         sqlExcept.printStackTrace();
      }

   }

And here’s how I’m calling it:

             if(firstTime == false) {
              JavaDB jdb = new JavaDB();
         }

        JavaDB window = new JavaDB("");
        window.addWindowListener(new WindowAdapter(){
            public void windowClosing(WindowEvent e){
                    System.exit(0);
                }
            });
        }

And here’s the actionListner:

            submit = new JButton("Submit");
            c.add(submit);
              submit.addActionListener( new ActionListener() {  
                    public void actionPerformed( ActionEvent e ) { 
                      if(e.getSource().equals(submit)) {
                         UserInput();
            }
         }  
     });  ;        

If you need anymore let me know. I’ve been teaching myself Java and I don’t really know what to learn so any tips will be welcomed. I’m also new to stack overflow and posting code so any thing you can give me will be more than appreciated. Thanks in advance.

Edit: I now added a class for event handling with the Thread inside of it like this;

             public class ButtonHandler implements ActionListener{
    public void actionPerformed(ActionEvent e){
        if(e.getSource().equals(submit)){
            Thread th = new Thread(new JavaDB());
            th.start();
            th.run();
            try {
                th.wait();
            } catch (InterruptedException e1) {
            }
        }
        else{
        System.exit(0);
        }   
    }

And I changed UserInput to run(). However,now when I click the submit button,The GUI disappears. Just for a reference you might need, here’s my main method:

             public static void main(String args[]) throws SQLException,
        InterruptedException {
    createConnection();
    boolean firstTime = firstTime();
    if (firstTime) {
        JavaDB db = new JavaDB("");
        db.createAccount();
        try {
            connect = DriverManager
                    .getConnection("jdbc:derby:\\KeithDB;shutdown=true");
        } catch (SQLException XJ015) {
        }
    }
    if (firstTime == false) {
        JavaDB jdb = new JavaDB();
        Thread th = new Thread();
    }

    JavaDB window = new JavaDB("");
    window.addWindowListener(new WindowAdapter() {
        public void windowClosing(WindowEvent e) {
            System.exit(0);
        }
    });
}

Anything else you need,let me know

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:40:28+00:00Added an answer on May 23, 2026 at 5:40 pm

    PasswordDemo, as shown in How to Use Password Fields, would be a good starting point for your study, and it would make an effective sscce.

    Addendum: Absent a complete example or knowledge of what database you are using, I got the following result,

    Version: H2 1.3.157 (2011-06-25) 1.3
    

    by running the following modification to PasswordDemo against H2 Database:

    if (isPasswordCorrect(input)) {
        try {
            Connection conn = DriverManager.getConnection(
                "jdbc:h2:mem:", "sa", "secret");
            DatabaseMetaData metaData = conn.getMetaData();
            System.out.println("Version:"
                + " " + metaData.getDatabaseProductName()
                + " " + metaData.getDatabaseProductVersion()
                + " " + metaData.getDatabaseMajorVersion()
                + "." + metaData.getDatabaseMinorVersion());
        } catch (SQLException ex) {
            ex.printStackTrace(System.err);
        }
    } ...
    

    Addendum: I got the following result,

    Version: Apache Derby 10.6.2.1 - (999685) 10.6
    

    by running the following modification to PasswordDemo against Apache Derby:

    if (isPasswordCorrect(input)) {
        try {
            EmbeddedDataSource ds = new EmbeddedDataSource();
            ds.setDatabaseName("/home/trashgod/.netbeans-derby/dbtest");
            Connection conn = ds.getConnection("sa", "secret");
            DatabaseMetaData metaData = conn.getMetaData();
            System.out.println("Version:"
                + " " + metaData.getDatabaseProductName()
                + " " + metaData.getDatabaseProductVersion()
                + " " + metaData.getDatabaseMajorVersion()
                + "." + metaData.getDatabaseMinorVersion());
        } catch (SQLException ex) {
            ex.printStackTrace(System.err);
        }
    } ...
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Does anyone know how can I replace this 2 symbol below from the string
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I have some data like this: 1 2 3 4 5 9 2 6
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.