JDBCステートメント-行の削除
行を削除するJDBCステートメントの例。
RowDelete.java
package com.example.jdbc.statement.row;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.sql.Statement;
public class RowDelete {
public static void main(String[] args) {
try (Connection conn = DriverManager.getConnection(
"jdbc:postgresql://127.0.0.1:5432/test", "postgres", "password");
Statement statement = conn.createStatement()) {
int row = statement.executeUpdate(deleteByName("example"));
// rows affected
System.out.println(row);
} catch (SQLException e) {
System.err.format("SQL State: %s\n%s", e.getSQLState(), e.getMessage());
} catch (Exception e) {
e.printStackTrace();
}
}
private static String deleteByName(String name) {
return "DELETE FROM EMPLOYEE WHERE NAME='" + name + "'";
}
}
P.S Tested with PostgreSQL 11 and Java 8
pom.xml
org.postgresql postgresql 42.2.5
ソースコードをダウンロード
$ git clone https://github.com/example/java-jdbc.git