Java Program to update record using JFrame and Prepaired Statement
Java Program to edit record using JFrame and Prepaired Statement
Here is table Design
Create table demo using two column as id and Dname , id is primary key
Program source code
import javax.swing.*;
import java.awt.event.*;
import java.sql.*;
public class FrameUpdate extends JFrame implements ActionListener{
JTextField tf1,tf2,tf3;
JButton b1,b2;
JLabel l1,l2;
Connection con; PreparedStatement psmt; int result;
//declaration of control
FrameUpdate(){
JFrame f= new JFrame("UpdateDemo");
//
l1=new JLabel("Enter RollNo");
l1.setBounds(150,10,150,50);
tf1=new JTextField();
tf1.setBounds(150,50,150,20);
l2=new JLabel("Enter Name");
l2.setBounds(150,60,150,50);
tf2=new JTextField();
tf2.setBounds(150,100,150,20);
tf3=new JTextField();
tf3.setBounds(150,150,150,20);
tf3.setEditable(false);
b1=new JButton("Add");
b1.setBounds(150,200,100,50);
b2=new JButton("Clear");
b2.setBounds(250,200,100,50);
b1.addActionListener(this); // event handing power
b2.addActionListener(this);
f.add(l1); f.add(l2);
f.add(tf1);f.add(tf2);f.add(tf3);f.add(b1);f.add(b2);
f.setSize(500,300);
f.setLayout(null);
f.setVisible(true);
f.setDefaultCloseOperation(EXIT_ON_CLOSE);
}
public void actionPerformed(ActionEvent e) {
String s1=tf1.getText();
int id =Integer.parseInt(s1);
String nm=tf2.getText();
if(e.getSource()==b1){
try{
Class.forName("sun.jdbc.odbc.JdbcOdbcDriver");
// loding Driver
con=DriverManager.getConnection("jdbc:odbc:demodb");
psmt=con.prepareStatement("update demo set dname=? where id=?");
psmt.setString(1,nm);
psmt.setInt(2,id);
int result=psmt.executeUpdate();
tf3.setText("record updated ");
con.close();
}
catch(Exception e1)
{
tf3.setText("Error" +e1);
}
}else if(e.getSource()==b2){
tf1.setText(" ");
tf2.setText(" ");
tf3.setText("record cleared");
}
}
public static void main(String[] args) {
new FrameUpdate();
} }