0% found this document useful (0 votes)
24 views3 pages

PR 188

ajp practical 18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views3 pages

PR 188

ajp practical 18
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Write a program to insert and retrieve the data from database using jdbc

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");

tf1 = new TextField(15);


tf2 = new TextField(15);
tf3 = new TextField(15);

btn1 = new Button("Submit");


btn2 = new Button("Reset");

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 :

You might also like