次に、JDBCステートメントを使用してテーブルからレコードを削除する方法を示す例を示します。 delete文を発行するには、次のように `Statement.executeUpdate()`メソッドを呼び出します。

Statement statement = dbConnection.createStatement();//execute the delete SQL stetement
statement.executeUpdate(deleteTableSQL);

完全な例…​

package com.mkyong.jdbc;

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

public class JDBCStatementDeleteExample {

    private static final String DB__DRIVER = "oracle.jdbc.driver.OracleDriver";
    private static final String DB__CONNECTION = "jdbc:oracle:thin:@localhost:1521:MKYONG";
    private static final String DB__USER = "user";
    private static final String DB__PASSWORD = "password";

    public static void main(String[]argv) {

        try {

            deleteRecordFromDbUserTable();

        } catch (SQLException e) {

            System.out.println(e.getMessage());

        }

    }

    private static void deleteRecordFromDbUserTable() throws SQLException {

        Connection dbConnection = null;
        Statement statement = null;

        String deleteTableSQL = "DELETE DBUSER WHERE USER__ID = 1";

        try {
            dbConnection = getDBConnection();
            statement = dbConnection.createStatement();

            System.out.println(deleteTableSQL);

           //execute delete SQL stetement
            statement.execute(deleteTableSQL);

            System.out.println("Record is deleted from DBUSER table!");

        } catch (SQLException e) {

            System.out.println(e.getMessage());

        } finally {

            if (statement != null) {
                statement.close();
            }

            if (dbConnection != null) {
                dbConnection.close();
            }

        }

    }

    private static Connection getDBConnection() {

        Connection dbConnection = null;

        try {

            Class.forName(DB__DRIVER);

        } catch (ClassNotFoundException e) {

            System.out.println(e.getMessage());

        }

        try {

            dbConnection = DriverManager.getConnection(
                             DB__CONNECTION, DB__USER,DB__PASSWORD);
            return dbConnection;

        } catch (SQLException e) {

            System.out.println(e.getMessage());

        }

        return dbConnection;

    }

}

結果

「user__id = 1」のレコードがテーブルから削除されます。

DELETE DBUSER WHERE USER__ID = 1
Record is deleted from DBUSER table!

リンク://タグ/削除/[削除]リンク://タグ/jdbc/[jdbc]リンク://タグ/ステートメント/[ステートメント]