GUI
package UI;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JTextField;
import com.toedter.calendar.JDateChooser;
import myLibs.bioJDBC;
import myLibs.bioLibs;
public class Practice implements ActionListener{
JFrame frame;
JButton btn;
JButton update;
JTextField nametxt,emailtxt,idtxt;
JComboBox agetxt;
JDateChooser c1;
public Practice() {
frame=new JFrame("Hancie e-Learning Studio");
frame.setSize(350,200);
frame.setLocationRelativeTo(null);
frame.setLayout(new GridLayout(5,2));
JLabel idlbl=new JLabel("ID");
JLabel namelbl=new JLabel("Name");
JLabel agelbl=new JLabel("Age");
JLabel emaillbl=new JLabel("DOB");
idtxt=new JTextField();
nametxt=new JTextField();
Object[]h1= {10,200};
agetxt=new JComboBox(h1);
c1=new JDateChooser();
c1.setDateFormatString("yyyy-MM-dd");
update=new JButton("Update");
update.addActionListener(this);
frame.add(idlbl);
frame.add(idtxt);
frame.add(namelbl);
frame.add(nametxt);
frame.add(agelbl);
frame.add(agetxt);
frame.add(emaillbl);
frame.add(c1);
frame.add(new JLabel());
frame.add(update);
frame.setVisible(true);
}
public void actionPerformed (ActionEvent ae) {
if(ae.getSource()==update) {
int id=Integer.parseInt(idtxt.getText());
String name=nametxt.getText();
int age1=Integer.parseInt(agetxt.getSelectedItem().toString());
String date=((JTextField) c1.getDateEditor().getUiComponent()).getText();
bioLibs bio=new bioLibs();
bio.setID(id);
bio.setName(name);
bio.setAge(age1);
bio.setDOB(date);
bioJDBC jdbc=new bioJDBC();
boolean result=jdbc.update(bio);
if(result==true) {
JOptionPane.showMessageDialog(null, "Data Updated");
}
}
}
public static void main(String[] args) {
new Practice();
}
}
JDBC Library
package myLibs;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.text.SimpleDateFormat;
public class bioJDBC {
public boolean update(bioLibs bio) {
Connection conn;
PreparedStatement pstat;
boolean result = false;
String sql = "UPDATE bio SET Name=?, Age=?,DOB=? WHERE ID=?;";
try {
Class.forName("com.mysql.cj.jdbc.Driver");
conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/practice", "root", "");
pstat = conn.prepareStatement(sql);
pstat.setString(1, bio.getName());
pstat.setInt(2, bio.getAge());
pstat.setString(3, bio.getDOB());
pstat.setInt(4, bio.getID());
pstat.executeUpdate();
pstat.close();
conn.close();
result = true;
} catch (Exception ex) {
System.out.println("Error" + ex.getMessage());
}
return result;
}
}
Library
package myLibs;
public class bioLibs {
int ID;
String Name;
int Age;
String DOB;
public bioLibs() {
this.ID=0;
this.Name = "";
this.Age = 0;
this.DOB = "";
}
public bioLibs(int id,String name, int age, String dOB) {
this.ID=id;
this.Name = name;
this.Age = age;
this.DOB = dOB;
}
public int getID() {
return ID;
}
public void setID(int iD) {
ID = iD;
}
public String getName() {
return Name;
}
public void setName(String name) {
Name = name;
}
public int getAge() {
return Age;
}
public void setAge(int age) {
Age = age;
}
public String getDOB() {
return DOB;
}
public void setDOB(String dOB) {
DOB = dOB;
}
@Override
public String toString() {
return "bioLibs [ID=" + ID + ", Name=" + Name + ", Age=" + Age + ", DOB=" + DOB + "]";
}
}
Output