Example : How to Edit or Modify or Update or Change the unique record from Oracle 10g/11g using NetBeans 8.2?
// Write Connectivity code first here globally.
private void btnUpdateActionPerformed(java.awt.event.ActionEvent evt) {
try {
String studentId = txtStudentId.getText();
String name = txtName.getText();
String password =
new String(txtPassword.getPassword());
String dob = txtDob.getText();
String email = txtEmail.getText();
String address = txtAddress.getText();
String mobile = txtMobile.getText();
String nationality =
txtNationality.getText();
String gender = "";
if(rbMale.isSelected())
{
gender = "Male";
}
else if(rbFemale.isSelected())
{
gender = "Female";
}
else if(rbOther.isSelected())
{
gender = "Other";
}
String education = "";
if(cb10th.isSelected())
{
education += "10th ";
}
if(cb12th.isSelected())
{
education += "12th ";
}
if(cbGraduate.isSelected())
{
education += "Graduate ";
}
if(cbPostGraduate.isSelected())
{
education += "Post Graduate ";
}
if(studentId.equals(""))
{
JOptionPane.showMessageDialog(this,
"Enter Student ID");
return;
}
int confirm =
JOptionPane.showConfirmDialog(
this,
"Are you sure to update this record?",
"Confirm Update",
JOptionPane.YES_NO_OPTION);
if(confirm == JOptionPane.YES_OPTION)
{
String sql =
"UPDATE student_register SET "
+ "student_name=?, "
+ "password=?, "
+ "dob=TO_DATE(?,'DD-MM-YYYY'), "
+ "email=?, "
+ "address=?, "
+ "gender=?, "
+ "mobile=?, "
+ "nationality=?, "
+ "education=? "
+ "WHERE student_id=?";
PreparedStatement pst =
conn.prepareStatement(sql);
pst.setString(1, name);
pst.setString(2, password);
pst.setString(3, dob);
pst.setString(4, email);
pst.setString(5, address);
pst.setString(6, gender);
pst.setString(7, mobile);
pst.setString(8, nationality);
pst.setString(9, education);
pst.setString(10, studentId);
int i = pst.executeUpdate();
if(i > 0)
{
JOptionPane.showMessageDialog(this,
"Record Updated Successfully");
resetForm();
}
else
{
JOptionPane.showMessageDialog(this,
"Record Not Found");
}
}
}
catch(Exception e)
{
JOptionPane.showMessageDialog(this,
"Update Error : " + e);
}
}
-------------------- 2nd Method ------------------------
// Write Connectivity code first here globally.
private void jBtnRegUpdateActionPerformed(java.awt.event.ActionEvent evt)
{
String gender = " ";
if(jRdbRegMale.isSelected())
{
gender="Male";
}
if(jRdbRegFemale.isSelected())
{
gender="Female";
}
if(jRdbRegOther.isSelected())
{
gender="Other";
}
String CmbState1 = (String)jCmbRegState.getSelectedItem();
String ChkMatric = "";
String ChkInter = "";
String ChkGrad = "";
String ChkPg = "";
if(jChkRegMatric.isSelected())
{
ChkMatric="Matric";
}
else
{
ChkMatric ="null";
}
if(jChkRegInter.isSelected())
{
ChkInter="Inter";
}
else
{
ChkInter ="null";
}
if(jChkRegGrad.isSelected())
{
ChkGrad="Graduation";
}
else
{
ChkGrad ="null";
}
if(jChkRegPg.isSelected())
{
ChkPg="PG";
}
else
{
ChkPg ="null";
}
Date dd=jDtRegDob.getDate(); //import java.util.Date;
Format xx= new SimpleDateFormat("dd-MMM-yyyy"); //import java.text.Format; & import
java.text.SimpleDateFormat;
String pp =xx.format(dd);
String TxtSlno = jTxtRegSlno.getText();
String TxtName = jTxtRegName.getText();
String TxtFname = jTxtRegFname.getText();
String TxtMobile = jTxtRegMobile.getText();
String TxtEmail = jTxtRegEmail.getText();
String RtbAddress = jRtbRegAddress.getText();
String TxtUname = jTxtRegUname.getText();
String PdPsd = new String(jPdRegPsd.getPassword());
try
{
pst=conn.prepareStatement("update REGISTRATION set REGSLNO='"+TxtSlno+"',REGNAME=
'"+TxtName+"',REGFNAME='"+TxtFname+"',REGDOB='"+pp+"',REGGENDER='"+gender+"',
REGMATRIC= '"+ChkMatric+"',REGINTER='"+ChkInter+"',REGGRAD='"+ChkGrad+"',
REGPG='"+ChkPg+"',REGMOBILE='"+TxtMobile+"',REGEMAIL='"+TxtEmail+"',
REGSTATE='"+CmbState1+"',REGADDRESS='"+RtbAddress+"', REGPSD='"+PdPsd+"'
where REGUNAME='"+TxtUname+"'");
pst.executeUpdate();
showMessageDialog(null, "Data Update Successfully...");
}
catch(Exception e)
{
System.out.println(e);
}
}
![]()
0 Comments