Solved

Java Swing SQL Question

Posted on 2004-04-03
12
356 Views
Last Modified: 2013-11-23
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
Comment
Question by:mcowman
  • 6
  • 3
  • 3
12 Comments
 
LVL 30

Accepted Solution

by:
GrandSchtroumpf earned 350 total points
ID: 10747861
    public void addMemberSQL{
should be
     public void addMemberSQL(){
0
 

Author Comment

by:mcowman
ID: 10747939
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
 
LVL 16

Expert Comment

by:warturtle
ID: 10747969
Hi,

Your mistake is -

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

Best Regards,

Warturtle
0
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 10747988
can please you post the exact error message?
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 10747999
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
 
LVL 16

Assisted Solution

by:warturtle
warturtle earned 150 total points
ID: 10748000
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
 

Author Comment

by:mcowman
ID: 10748005
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
 
LVL 16

Expert Comment

by:warturtle
ID: 10748023
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
 
LVL 30

Assisted Solution

by:GrandSchtroumpf
GrandSchtroumpf earned 350 total points
ID: 10748067
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
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 10748080
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
 

Author Comment

by:mcowman
ID: 10748195
Success!!! Thanks for your help!!
0
 
LVL 30

Expert Comment

by:GrandSchtroumpf
ID: 10748207
thanks for the points :°)
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

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.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
servlet requestdispatcher include and forward differences 1 39
login jsp example 24 69
mysql jsp example issue 32 53
sql import cannot be resolved jsp 3 50
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

820 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question