In this tutorial, you will be able to learn how to update the data stored in sql database using java.
Please watch the video because there are some task to be done beside code
Code to Update data in SQL server using Java
try{
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url="jdbc:sqlserver://localhost:1433;databaseName=testdb;user=sa;password=orchid";
Connection con = DriverManager.getConnection(url);
int row= jTable_Display_User.getSelectedRow();
String value=(jTable_Display_User.getModel().getValueAt(row, 0).toString()); String query = "UPDATE users SET name=?, address=?, gender=?, knowledge=?, subject=? where sno="+value;
PreparedStatement pst = con.prepareStatement(query);
pst.setString(1, name.getText());
pst.setString(2, address.getText());
if(male.isSelected()){
gender="Male";
}
if(female.isSelected()){
gender="Female";
}
pst.setString(3, gender);
String knowledge="";
if(coreJava.isSelected()){
knowledge+=coreJava.getText()+" ";
}
if(python.isSelected()){
knowledge+=python.getText()+" ";
}
pst.setString(4, knowledge);
String course;
course=subject.getSelectedItem().toString();
pst.setString(5, course);
pst.executeUpdate();
DefaultTableModel model = (DefaultTableModel)jTable_Display_User.getModel();
model.setRowCount(0);
show_user();
JOptionPane.showMessageDialog(null, "Updated Sucessfully!");
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
Class.forName("com.microsoft.sqlserver.jdbc.SQLServerDriver");
String url="jdbc:sqlserver://localhost:1433;databaseName=testdb;user=sa;password=orchid";
Connection con = DriverManager.getConnection(url);
int row= jTable_Display_User.getSelectedRow();
String value=(jTable_Display_User.getModel().getValueAt(row, 0).toString()); String query = "UPDATE users SET name=?, address=?, gender=?, knowledge=?, subject=? where sno="+value;
PreparedStatement pst = con.prepareStatement(query);
pst.setString(1, name.getText());
pst.setString(2, address.getText());
if(male.isSelected()){
gender="Male";
}
if(female.isSelected()){
gender="Female";
}
pst.setString(3, gender);
String knowledge="";
if(coreJava.isSelected()){
knowledge+=coreJava.getText()+" ";
}
if(python.isSelected()){
knowledge+=python.getText()+" ";
}
pst.setString(4, knowledge);
String course;
course=subject.getSelectedItem().toString();
pst.setString(5, course);
pst.executeUpdate();
DefaultTableModel model = (DefaultTableModel)jTable_Display_User.getModel();
model.setRowCount(0);
show_user();
JOptionPane.showMessageDialog(null, "Updated Sucessfully!");
}
catch(Exception e){
JOptionPane.showMessageDialog(null, e);
}
}
Please don't forget To Like, Share and Subscribe my Youtube video and Channel.
Youtube Channel Subscription Click Here
No comments:
Post a Comment