QN044. Write a program to update a record of database table (bases on specific field).
1. Objective
To update a record of database table using java program.
2. Theory
In this program, we create statement and execute mysql query to update a record of database table.
3. Source Code
      import java.sql.*; 
      class JDBC44{
        public static void main(String[] args){
          try{
            Class.forName("com.mysql.cj.jdbc.Driver");
            String url = "jdbc:mysql://localhost:3306/bca_third_2020?serverTimezone=UTC";
            String un = "root";
            String pw = "";
            Connection cn = DriverManager.getConnection(url,un,pw);
            update(cn);
          }catch(Exception e){
            System.out.println(e);
          }
        }
        
        private static void update(Connection cn) throws SQLException {
          Statement stat = cn.createStatement(); // statement create
          String query = "update student set course = 'BIT' where name = 'Sajan'";
          stat.executeUpdate(query); // execute query
          System.out.println("Record updated!");
          }
        }
    
    4. Output
Record updated!
5. Conclusion
In this way, I would like to conclude that, we can update our database table by using this program.