Results 1 to 2 of 2
  1. #1
    sami is offline Member
    Join Date
    Aug 2007
    Posts
    1
    Rep Power
    0

    Default jdbc/ swing code problem

    jdbc/swing
    ============

    Following is my code to retrive data from my ms-access table NAME to display the employee name and his id.

    But iam getting the error as

    can not resolve the symbol varaible addbutton
    in the line (e.getSource == addbutton)

    also can not resolve symbol
    variable rs ( which is my Resultset that i have created in my code).

    can any body help in solving my problem to retrieve and display the values from the table to the textfields.

    Thanks in advance.

    code starts:
    ========================================



    import java.sql.*;
    import javax.swing.*;
    import java.awt.*;
    import java.awt.event.*;

    public class sample1 extends JFrame implements ActionListener
    {
    public sample1()
    {
    super("Sample application");

    // JPanel p = new JPanel();

    JLabel empid = new JLabel("Employee id");

    JLabel empnamelabel = new JLabel("Employee name");


    JTextField id = new JTextField(20);

    JTextField empname = new JTextField(20);

    JButton addbutton = new JButton("Add");

    Container c = getContentPane();

    // c.add(p);


    c.add(addbutton);
    c.add(empnamelabel);
    c.add(empname);
    c.add(id);
    c.add(empid);



    addbutton.addActionListener( new ActionListener);
    add( addbutton );




    //load the JDBC-ODBC bridge driver


    try {
    Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");

    }

    catch(java.lang.ClassNotFoundException e) {
    System.err.print("ClassNotFoundException: ");
    System.err.println(e.getMessage());
    }


    // Get the connection

    Connection conn = DriverManager.getConnection("jdbc:odbc:empdsn");

    // create a statement object, resultset object and prepared statement to pass sql queries.

    Statement st = conn.createStatement();

    st.executeQuery("Select * from NAME");

    ResultSet rs = st.getResultSet();


    pack();
    setVisible(true);

    }


    public static void main(String args[])
    {

    sample1 sam = new sample1(sam);


    sam.addWindowListener(
    new WindowAdapter() {
    public void windowClosing( WindowEvent e ) {
    System.exit( 0 );
    }
    }
    );
    }

    }



    public void actionPerformed(ActionEvent e)
    {
    if(e.getSource()==addbutton) {
    try
    {
    while(rs.next())
    {
    id.setText(rs.getString(1));
    empname.setText(rs.getString(2));
    }
    }
    catch(Exception ee) {}

    }
    }


    }

  2. #2
    shanePreater is offline Member
    Join Date
    Jul 2007
    Location
    England, Bath
    Posts
    47
    Rep Power
    0

    Default

    From a very quick look it would appear your first major problem is in the creation of addButton:
    Java Code:
    addbutton.addActionListener([COLOR="DarkRed"] new ActionListener[/COLOR]);
    add( addbutton );
    The ActionListener is an interface and therefore you need to either provide a class which implements the interface in it's place or provide the implementation that you want for the ActionListener in place :
    Java Code:
    addbutton.addActionListener( new ActionListener(){
    
    	public void actionPerformed(ActionEvent arg0) {
    		//TODO Replace this with your actual implementation
    		System.out.println("You have clicked add");
    	}});
    add( addbutton );
    That will get you past the first hurdle but you have some other very serious issues with your code.

    I would suggest you grab yourself a basic java book and work through that.
    Shane.

Similar Threads

  1. Jdbc problem
    By abhiN in forum JDBC
    Replies: 1
    Last Post: 03-21-2008, 05:10 AM
  2. EventHandling code for Swing application
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-07-2007, 12:06 PM
  3. code for making a java swing program a demo verson
    By fakhruddin in forum AWT / Swing
    Replies: 1
    Last Post: 11-27-2007, 08:54 PM
  4. JDBC problem
    By Swamipsn in forum New To Java
    Replies: 1
    Last Post: 08-13-2007, 06:05 PM
  5. JDBC problem
    By peiceonly in forum Threads and Synchronization
    Replies: 2
    Last Post: 08-03-2007, 01:42 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •