Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;

public class Main {
    public static void main(String[] args) throws Exception {
        String url = "jdbc:mysql://localhost/sampledb";
        String username = "root";
        String password = "";

        Class.forName("com.mysql.jdbc.Driver");
        Connection connection = DriverManager.getConnection(url, username, password);

        String sql = "DELETE FROM users WHERE user_id = ?";
        int userId = 2;

        PreparedStatement statement = connection.prepareStatement(sql);

        statement.setInt(1, userId);

        int rows = statement.executeUpdate();

        System.out.println(rows + " record(s) deleted.");
        connection.close();
    }
}