0 votes
861 views
in JDBC by
How to update or modify data into database in java? Anyone can help me to modify data into database using JDBC API.

1 Answer

0 votes
by

With the help of following code you can modify or update database record into database:

package com.developerhelpway.jdbc;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.Statement;

public class UpdateDataInToDatabase {

    /**
     * @param args
     */
    public static void main(String[] args) {
        String driverName="com.mysql.jdbc.Driver";
        String url="jdbc:mysql://localhost:3306/";
        String databaseName="jdbcexamples";
        String userName="root";
        String password="";
        String tableName = "student";
        Connection con = null;
        try{
          Class.forName(driverName);
          con=(Connection) DriverManager.getConnection(url+databaseName, userName, password);
          Statement stat=con.createStatement();
          String sqlQuery = "UPDATE " + tableName +" SET studentName = 'MBBS' WHERE id = 2";
          int num = stat.executeUpdate(sqlQuery);
          System.out.println(num + " rows updated.");
        }
        catch(Exception e){
          e.printStackTrace();
        }
    }

}
 

Output:

1 rows updated.
 

Share:- Whatsapp Facebook Facebook


Welcome to Developerhelpway Q&A, where you can ask questions and receive answers from other members of the community.

Categories

...