Delete record from table : Table « Database « Java Tutorial






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();
  }
}








20.26.Table
20.26.1.Create a table in database
20.26.2.Delete record from table
20.26.3.Description of Database Table
20.26.4.Adding a New Column Name in Database Table
20.26.5.Change Column Name of a Table
20.26.6.Make Unique Column in Database Table
20.26.7.Remove Unique Column in Database Table
20.26.8.Arrange a Column of Database Table
20.26.9.Sum of Column in a Database Table
20.26.10.Delete a Column from a Database Table
20.26.11.Copy data from one table to another in a database
20.26.12.Drop table from database
20.26.13.Copy One Database Table to Another