代わりにStatement.executeUpdate()を使用できます。 SELECT
ステートメントをUPDATEステートメントに変更する必要があるためです。
単なる行データを選択しないため、単一行データへのアクセスがゆるいという欠点があります。これが必要な場合、たとえば(あなたのケースでは[email protected]<localipaddress>
)更新値を計算するためには、選択したものを最初に実行し、メモリ内の更新を計算してから、PreparedStatementまたはBatch Updateを使用してUPDATE
文を実行する必要があります。
準備ステートメント例:
public static int preparedUpdate(Connection conn, String localIPAddress) throws SQLException {
int numChangedRows = 0;
try (Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery("SELECT * FROM table1");
while (rs.next()) {
// id => something unique for this row within the table,
// typically the primary key
String id = rs.getString("id");
String jid = rs.getString("column1");
if("abc".equals(jid)) { // just some nonsense condition
try (PreparedStatement batchUpdate = conn.prepareStatement("UPDATE table1 SET column1 = ? where id = ?")) {
batchUpdate.setString(1, localIPAddress);
batchUpdate.setString(2, id);
numChangedRows = batchUpdate.executeUpdate();
}
}
}
}
return numChangedRows;
}
バッチ更新例:
public static int[] batchUpdate(Connection conn, String localIPAddress) throws SQLException {
int[] changedRows = null;
try (PreparedStatement batchUpdate = conn.prepareStatement("UPDATE table1 SET column1 = ? where id = ?")) {
try (Statement stmt = conn.createStatement()) {
ResultSet rs = stmt.executeQuery("SELECT * FROM table1");
while (rs.next()) {
// id => something unique for this row within the table,
// typically the primary key
String id = rs.getString("id");
String jid = rs.getString("column1");
if("abc".equals(jid)) { // just some nonsense condition
batchUpdate.setString(1, localIPAddress);
batchUpdate.setString(2, id);
batchUpdate.addBatch();
}
}
}
changedRows = batchUpdate.executeBatch();
}
return changedRows;
}
更新行?すべての行?特定の行ですか?どの列を更新しますか? –
テーブルの定義を投稿してください - updateString()で更新するカラムの種類は何ですか?コードスニペットと例外トレースをテキスト(写真ではなく)として投稿すると、ビッグGがこの投稿を見つけるのに役立ちます。 – chrisinmtown