Solved

Java Swing SQL Question

Posted on 2004-04-03
12
357 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 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
MS Dynamics Made Instantly Simpler

Make Your Microsoft Dynamics Investment Count  & Drastically Decrease Training Time by Providing Intuitive Step-By-Step WalkThru Tutorials.

 
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

Salesforce Has Never Been Easier

Improve and reinforce salesforce training & adoption using WalkMe's digital adoption platform. Start saving on costly employee training by creating fast intuitive Walk-Thrus for Salesforce. Claim your Free Account Now

Question has a verified solution.

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

Suggested Solutions

Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.
Suggested Courses

738 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