How to Update the data in the Database

Hello guys, This is vishal, and if you already done with the insert and delete the data but don’t know how to Update that data, don’t worry because in this I’m telling you how to update the data in the database.

Updating the data is also very easy task because in this you only have to make few changes and then you code will work perfectly.
But before begin with coding part i am telling you when you update any think then at that time you have to specify that which data you wants to update.

Example:
suppose that in PHPMyAdmin there are 4 columns (Id, Name, Email, Phone) that have 5 rows (with id from 1 to 5) Now this time you have to specify that who users data you want to update. if I update the data without specifying the column then the SQL command can’t understand which user data will update and which is not. So, for this have to specify the column name.
If the table name is the user. then,
String user_id = Id.getText(); //Get the Id from the textBox.
UPDATE user SET Name = ?, Email = ?, Phone = ? WHERE Id = ?

And if you still have some problem then I am providing a video, In this video, I am clearly showing that how to update the data.


Now, I hope you understand the concept clearly by watching this video and if you guys still have some doubt, don’t hesitate to contact me comment down below or DM me on telegram or Instagram as soon as possible to solve your doubts.
I also provide the code so that you simply copy and paste this code to do it quickly.

Connection conn;
PreparedStatement st;
These 2 Above lines write outside the button section. And in the button section past this code.

        String name = uname.getText();
        String mail = email.getText();
        String mob = mobile.getText();
        try {
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/managedata","root",""s);
            st = conn.prepareStatement("update insertdata set email=?, mobile=? where username = ?");
            
            st.setString(3,name);
            st.setString(1,mail);
            st.setString(2,mob);
            
            st.executeUpdate();
            JOptionPane.showMessageDialog(this, "Data Updated Successfully");
            
            uname.setText("");
            email.setText("");
            mobile.setText("");
            uname.requestFocus();
            
            
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(insertdata.class.getName()).log(Level.SEVERE, null, ex);
        } catch (SQLException ex) {
            Logger.getLogger(insertdata.class.getName()).log(Level.SEVERE, null, ex);
        }

So, guys thanks for now, I hope you guys understand the things clearly. And bye for now see you in the next Article.

Leave a Comment

Your email address will not be published. Required fields are marked *

Shopping Cart