2017-02-27 71 views
0

我正在面對java代碼中mariadb中resultset更新列值的問題。看起來像resultset.updateString()方法在mariadb JDBC連接器中不受支持,任何人都可以請發送給我另一種方法來執行此過程。更新列在java中的mariadb中不起作用

MariaDB的連接器版本:MariaDB的-Java的客戶機 - 1.5.8.jar, MariaDB的版本:MariaDB的-20年10月1日 - winx64

以下是代碼片段: Java Code Snippet

繼拋出異常: Exception Trace

+0

更新哪一行?所有的行?一些特定的行?更新哪些列? –

+0

請發表表格定義 - 什麼是你更新updateString()的列類型?最好將代碼片段和異常跟蹤作爲文本(而不是圖片)發佈,幫助大G找到這篇文章。 – chrisinmtown

回答

1

您可以改爲使用Statement.executeUpdate()。 因爲您需要將您的SELECT聲明更改爲UPDATE聲明。

缺點是您無法訪問單行數據,因爲您根本沒有選擇它。如果你需要這個,例如要計算更新後的值(在您的情況下爲[email protected]<localipaddress>),您可能必須首先按照您所做的操作選擇該選擇,然後在內存中計算您的更新,然後使用PreparedStatementBatch 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

感謝您的快速回復。 @Alexander – Rakesh