PR 188
PR 188
package jdbcdemo2;
import java.awt.*;
import java.awt.event.*;
import java.sql.*;
class JdbcDemo2 extends Frame implements ActionListener {
TextField tf1, tf2, tf3;
Label lb1, lb2, lb3, lb;
Button btn1, btn2;
public JdbcDemo2() {
setLayout(new FlowLayout());
lb1 = new Label("Enter Employee ID ");
lb2 = new Label("Enter Employee Name ");
lb3 = new Label("Enter City ");
lb = new Label("See the message here");
add(lb1);
add(tf1);
add(lb2);
add(tf2);
add(lb3);
add(tf3);
add(lb);
add(btn1);
add(btn2);
btn1.addActionListener(this);
btn2.addActionListener(this);
setSize(300, 300);
setTitle("Demonstrating JDBC Insert on Frame GUI");
setVisible(true);
}
public static void main(String[] args) {
new JdbcDemo2();
}
public void actionPerformed(ActionEvent ae) {
String s = ae.getActionCommand();
if (s.equals("Submit")) {
String s1 = tf1.getText();
String s2 = tf2.getText();
String s3 = tf3.getText();
String query = "INSERT INTO emp_info (emp_id, emp_name, emp_city) VALUES
(?, ?, ?)";
try {
Class.forName("org.apache.derby.jdbc.ClientDriver");
Connection con =
DriverManager.getConnection("jdbc:derby://localhost:1527/YourDB", "user", "password");
PreparedStatement pst = con.prepareStatement(query);
pst.setString(1, s1);
pst.setString(2, s2);
pst.setString(3, s3);
int t = pst.executeUpdate();
lb.setText(t + " record(s) inserted");
con.close();
} catch (ClassNotFoundException cnfe) {
lb.setText("Could not load Driver");
} catch (SQLException ex) {
lb.setText("Query went wrong");
}
}
if (s.equals("Reset")) {
tf1.setText(null);
tf2.setText(null);
tf3.setText(null);
lb.setText("See the message here");
}
}
}
Output :