0 votes
1.1k views
in JDBC by
How to delete data from database in java? Please give suitable example with code.

1 Answer

0 votes
by
You see the following code to delete record from database:

package com.developerhelpway.jdbc;

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

public class DeleteDataInFromDatabase {

    /**
     * @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 = "DELETE FROM " + tableName +" WHERE id = 2";
          int num = stat.executeUpdate(sqlQuery);
          System.out.println(num + " rows deleted.");
        }
        catch(Exception e){
          e.printStackTrace();
        }
    }

}
 

Output:

1 rows deleted.

Share:- Whatsapp Facebook Facebook


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

Categories

...