?
Solved

Java Swing SQL Question

Posted on 2004-04-03
12
Medium Priority
?
359 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 3
  • 3
12 Comments
 
LVL 30

Accepted Solution

by:
GrandSchtroumpf earned 1400 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
Get real performance insights from real users

Key features:
- Total Pages Views and Load times
- Top Pages Viewed and Load Times
- Real Time Site Page Build Performance
- Users’ Browser and Platform Performance
- Geographic User Breakdown
- And more

 
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 600 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 1400 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

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

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

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
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…
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
How to fix incompatible JVM issue while installing Eclipse While installing Eclipse in windows, got one error like above and unable to proceed with the installation. This video describes how to successfully install Eclipse. How to solve incompa…
Suggested Courses
Course of the Month10 days, 8 hours left to enroll

765 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