#Insert function
private void insertActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
Connection conn;
conn=DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Angle\\Desktop\\APT
project\\APTproject.sqlite");
Statement stmt=conn.createStatement();
String sql ="insert into employee values ('"+id.getText()+("','")+name.getText()+
("','")+cnic.getText() +("','")+contact.getText()+("','")+address.getText()+"')";
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(null, "Data inserted");}
catch(Exception e){
JOptionPane.showMessageDialog(null, e+"Data not inserted");
}
}
#Show function:
private void showActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
Connection conn;
conn=DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Angle\\Desktop\\APT
project\\APTproject.sqlite");
Statement st=conn.createStatement();
String sql="Select *from employee";
ResultSet rs=st.executeQuery(sql);
//showing in jtables data
jTable3.setModel(DbUtils.resultSetToTableModel(rs));
}catch(Exception e){
}
}
#Update function:
:
private void updateActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
Connection con=DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Angle\\Desktop\\APT
project\\APTproject.sqlite");
Statement stmt=con.createStatement();
String sql;
sql = "update employee set name ='"+name.getText()+"', cnic='"+cnic.getText()+ "',contact='"
+contact.getText()+"',address='" +address.getText()+"' where id='"+id.getText()+"'";
stmt.executeUpdate(sql);
JOptionPane.showMessageDialog(null,"data updated");
}
catch(Exception e)
{
JOptionPane.showMessageDialog(null,e);
}
}
#Search function
private void SearchActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try
{
Connection conn;
conn=DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Angle\\Desktop\\APT
project\\APTproject.sqlite");
String SQL="Select *from employee where id='"+id.getText()+"'";
PreparedStatement pst = conn.prepareStatement(SQL);
ResultSet rs = pst.executeQuery();
name.setText(rs.getString("Name"));
cnic.setText(rs.getString("cnic"));
contact.setText(rs.getString("contact"));
address.setText(rs.getString("address"));
}
catch(Exception e)
{JOptionPane.showMessageDialog(null, "No Result found");
}
}
#Delete Function
private void DeleteActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
try{
Connection con=DriverManager.getConnection("jdbc:sqlite:C:\\Users\\Angle\\Desktop\\APT project\\APTproject.sqlite");
String r=id.getText();
String SQL;
SQL ="Select *from employee where id=?";
PreparedStatement pst = con.prepareStatement(SQL);
pst.setString(1, id.getText());
ResultSet res=pst.executeQuery();
if(res.next())
{
JOptionPane.showMessageDialog(null, " result found and deleted");
SQL="Delete from employee where id='"+r+"'";
Statement stmt=con.createStatement();
stmt.executeUpdate(SQL);
}
else
{
JOptionPane.showMessageDialog(null, "not found");
}
}
catch( SQLException e){
JOptionPane.showMessageDialog(null, e);
}
}
4 Comments
Super helpful! Thank you for this
ReplyDeleteSeems pretty useful xx Thanks
ReplyDeleteVery helpful report. Step by step everything is explained.
ReplyDeleteInteresting to know
ReplyDelete