views:

81

answers:

2

Sorry last program had lot of errors (i forgot to save the file and hence posted a incomplete code) so i am posting this

Here is the Code:

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

public class GUIPreparedStatement {
    private JFrame frame1;
    private JPanel panel1;
    private JTextField tf1;
    private JTextField tf2;
    private JButton b1;
    public JLabel lb1;
    PreparedStatement ps;

    GUIPreparedStatement() {
        frame1 = new JFrame();
        panel1 = new JPanel();
        tf1 = new JTextField(10);
        tf2 = new JTextField(10);
        b1 = new JButton("Enter Record");
        lb1 = new JLabel("Press the Button");

        try {
            Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
            Connection con = DriverManager.getConnection("jdbc:odbc:Employees");
            ps = con.prepareStatement("INSERT INTO Employees VALUES(?,?)");
        } catch(SQLException e) {
            lb1.setText("SQL Statement not executed");
        } catch(Exception e) {
            lb1.setText("General Error");
        }

        b1.addActionListener(this);

        panel1.add(tf1);
        panel1.add(tf2);
        panel1.add(b1);
        panel1.add(lb1);

        Container contentPane = frame1.getContentPane();
        contentPane.add(panel1);

        frame1.setDefaultCloseOperation(frame1.EXIT_ON_CLOSE);
        frame1.setVisible(true);
        frame1.pack();
    }

    public void actionPerformed(ActionEvent e) {
        if (e.getSource() == b1) {
            try {
                ps.setString(1,tf1.getText());
                ps.setString(2,tf2.getText());
                ps.executeUpdate();
            } catch (SQLException f) {
                lb1.setText("SQL Record not Entered");
            }       
        }
    }

    public static void main (String args[]) {
        new GUIPreparedStatement();
    }
}

Error in code:

A: 

Some of your your errors are referencing lines of code which you did not post.

EDIT: Corrected assumptions which were invalid

Aaron
-1 there are no JDBC errors
Erick Robertson
Those undefined ps vars I'm willing to wager are PreparedStatements. Not sure a -1 was in order...but if you feel the need...
Aaron
@Aaron: Most of the warnings are about undefined variables. Linking or similar things will not fix the error.
DarkDust
@DarkDust, agreed, edited the answer...and -1 the question...
Aaron
There are however serious errors in how JDBC is used. But that's indeed beyond the scope of the *actual* problem/question.
BalusC
Just FYI - I removed by -1 once the question changed and the answer changed. I think it was appropriate for the first answer, which missed the mark. But now everything misses the mark through no fault of anyone's except the OP.
Erick Robertson
+2  A: 

To answer your heavily edited question, your GUIPreparedStatment needs to implement the ActionListener interface.

public class GUIPreparedStatement implements ActionListener

Whilst you have already implemented the actionPerformed(ActionEvent e) you have not declared to the compiler that you've done this to satisfy the interface contract.

Kevin D
Is there a another way to do this without implementing action-listener.
subanki
You could move the actionPerformed method out into an anonymous inner class that implements ActionListener. But if you want to listen for an action, you need an ActionListener.
Kevin D