JDBC - update data on database

From PU/ Programming in Java

Asked on 28 Jan, 2021

Like 0 Dislike 290 Views

write a program to update data on following table

Table:student

       Column

          Name Type

             id     number

        name     varchar

            age     number

Add a comment

1 Answers

Anonymous on 2 Feb, 2021 Like 0 Dislike

lets try setting name = 'adult' if age > 20

import java.sql.* ;

public class UpdateTable{

public static void main(String[] agrs){

Connection connection ;

PreparedStatement ps ;

try(

connection = DriverManager.getConnection("jdbc:mysql://localhost:3306/students", "user", "password") ;

ps = connection.prepareStatement("Update student set name = ? where age > 20") ;

ps.setString(1, "aged") ;

){

int updateCount = ps.executeUpdate() ;

System.out.println(updateCount + " rows updated") ;

}catch(Exception e){e.printStackTrace();}

}

}

Add a comment


Add your answer