Instruction JDBC - Mettre à jour une ligne
Un exemple de déclaration JDBC pour mettre à jour une ligne.
RowUpdate.java
package com.example.jdbc.statement.row; import java.math.BigDecimal; import java.sql.Connection; import java.sql.DriverManager; import java.sql.SQLException; import java.sql.Statement; public class RowUpdate { 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(updateSalaryByName("example", new BigDecimal(1080))); // 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 updateSalaryByName(String name, BigDecimal salary) { return "UPDATE EMPLOYEE SET SALARY='" + salary + "' WHERE NAME='" + name + "'"; } }
P.S Tested with PostgreSQL 11 and Java 8
pom.xml
org.postgresql postgresql 42.2.5
Télécharger le code source
$ git clone https://github.com/example/java-jdbc.git