Improve company productivity with a Business Account.Sign Up

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 365
  • Last Modified:

Java Swing SQL Question

The following function is supposed to add values into an ms access table.  All other parts of the program work fine.  But when I add this it gives one error '{' expected.  I think I have the correct amount of brackets, Do you know where I'm going wrong??  Thank you

     public void addMemberSQL{
                    try      {
           
                              Statement st = conn.createStatement();
                              st.executeUpdate("INSERT INTO Members " +
                                  "VALUES (1001, 'Simpson', 'Mr.', 'Springfield', 2001)");
                          conn.close();
                          }
       
      
                              catch (SQLException sqlex)
                              {
                              System.err.println(sqlex.getMessage());
                              success = false;
                        }      }
0
mcowman
Asked:
mcowman
  • 6
  • 3
  • 3
3 Solutions
 
GrandSchtroumpfCommented:
    public void addMemberSQL{
should be
     public void addMemberSQL(){
0
 
mcowmanAuthor Commented:
Excellent!!! This gave me a few more errors after I made that change but I resolved them.  Just one more thing.  The idea is when you click a button the above fuction should be called.  I get the error cannot define symbol the listener calls it.  Do you know whats wrong??

    class MemberaddDetails implements ActionListener
     {
          public void actionPerformed(ActionEvent e)
          {

              //System.out.println("Test");
             addMemberSQL();

          }
     }
0
 
warturtleCommented:
Hi,

Your mistake is -

public void addMemberSQL(), it should have brackets around it.

Best Regards,

Warturtle
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
GrandSchtroumpfCommented:
can please you post the exact error message?
0
 
GrandSchtroumpfCommented:
make sure your MemberaddDetails class is a nested class (it needs to be declared inside the {} of the class that defines the addMemberSQL method).
0
 
warturtleCommented:
You can actually import the ActionListener class like -

import java.awt.event.ActionListener;

You are not associating the actionPerformed method with the button. It doesn't say it, anywhere in the code that actionPerformed should be called, when the button A(say) is clicked.

You can associate an actionPerformed(ActionEvent e) with a button like -

JButton connectButton = new JButton("Connect");
    connectButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event)
      {
          addMemberSQL();
        } });

Best Regards,

Warturtle
0
 
mcowmanAuthor Commented:
Sure!!!, its

Test.java 158 cannot resolve symbol
symbol: method addMemberSQL()
location: class Test.MemberaddDetails
                  addMemberSQL();


1 error

Thanks!!

Warturtle,
I'm not sure what you mean, can you please clarify

Thanks!!
0
 
warturtleCommented:
Hi,

I was trying to tell you that - you need to associate the button with the actionPerformed method.

Just as for opening a .jpeg file, you can set the default program to be Internet Explorer, so its like telling the computer that whenever the user clicks on the .jpeg file, it should open it with Internet Explorer.

Associations are like -

// Associate the connectButton with the ActionListener class method ...
connectButton.addActionListener(new ActionListener() {
      public void actionPerformed(ActionEvent event)
      {
          addMemberSQL();
        } });

Best Regards,

Warturtle
0
 
GrandSchtroumpfCommented:
the class MemberaddDetails should be nested, which means it should be declared inside your other class.  this way you can call the method of the class that contains your nested class.  So your MemberaddDetails class should be declared a little bit like you declare a method... inside your main class.
this is what your class should look like:

class YourClass {

  public void addMemberSQL() {
    try {
      Statement st = conn.createStatement();
      st.executeUpdate("INSERT INTO Members " +
      "VALUES (1001, 'Simpson', 'Mr.', 'Springfield', 2001)");
      conn.close();
    }
    catch (SQLException sqlex) {
      System.err.println(sqlex.getMessage());
      success = false;
    }
  }

  class MemberaddDetails implements ActionListener {
    public void actionPerformed(ActionEvent e) {
      //System.out.println("Test");
      addMemberSQL();
    }
  }

}
0
 
GrandSchtroumpfCommented:
there are other ways to do it, but using a nested class is a verry common way to do it.
another way is for your main class to implement ActionListener.
then you would use "this" as ActionListener:
   yourButton.addActionListener(this);
and your class would look like this:

class YourClass implements ActionListener {

  public void addMemberSQL() {
    try {
      Statement st = conn.createStatement();
      st.executeUpdate("INSERT INTO Members " +
      "VALUES (1001, 'Simpson', 'Mr.', 'Springfield', 2001)");
      conn.close();
    }
    catch (SQLException sqlex) {
      System.err.println(sqlex.getMessage());
      success = false;
    }
  }

  public void actionPerformed(ActionEvent e) {
    //System.out.println("Test");
    addMemberSQL();
  }

}

0
 
mcowmanAuthor Commented:
Success!!! Thanks for your help!!
0
 
GrandSchtroumpfCommented:
thanks for the points :°)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 6
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now