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:
http://screensnapr.com/u/nx5t1r.png
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.
Some of your your errors are referencing lines of code which you did not post.
EDIT: Corrected assumptions which were invalid
精彩评论