2017-02-27 5 views
0

mariadbのjavaetコードの結果セットで列の値を更新する際に問題があります。 mariadb JDBCコネクタではresultset.updateString()メソッドがサポートされていないようですが、誰も私にこのプロセスを実行する別の方法を教えてください。mariadbからjavaを更新していない列を更新しました

MariaDBコネクタのバージョン:mariadb-javaのクライアント-1.5.8.jar、 MariaDBのバージョン:mariadb-10.1.20-winx64

後は、コードスニペットです:スロー例外後 Java Code Snippet

Exception Trace

+0

更新行?すべての行?特定の行ですか?どの列を更新しますか? –

+0

テーブルの定義を投稿してください - updateString()で更新するカラムの種類は何ですか?コードスニペットと例外トレースをテキスト(写真ではなく)として投稿すると、ビッグGがこの投稿を見つけるのに役立ちます。 – chrisinmtown

答えて

1

代わりに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; 
    } 
+0

ご返信ありがとうございます。 @アレキサンダー – Rakesh

関連する問題