Solved

Java Swing SQL Question

Posted on 2004-04-03
12
348 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
 
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
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
SPLUNK REST  API call to Splunk to create and index? 2 71
pairstar challenge 2 42
topping2 challenge 13 62
mockito example issue 8 38
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

747 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now