【问题标题】:Updating column not working in mariadb from java从java更新列在mariadb中不起作用
【发布时间】:2017-07-18 04:16:12
【问题描述】:

我在通过 java 代码的 mariadb 中的结果集更新列值时遇到问题。看起来 mariadb JDBC 连接器不支持 resultset.updateString() 方法,任何人都可以向我发送执行此过程的替代方法。

MariaDB 连接器版本:mariadb-java-client-1.5.8.jar, MariaDB 版本:mariadb-10.1.20-winx64

下面是sn-p的代码: Java Code Snippet

抛出以下异常: Exception Trace

【问题讨论】:

  • 更新哪一行?所有行?一些特定的行?更新哪些列?
  • 请发布表定义 - 您使用 updateString() 更新的列类型是什么?最好把代码sn-p和异常跟踪贴成文字(不是图片),帮助大G找到这个帖子。

标签: java mariadb mariadb-connect-engine


【解决方案1】:

您可以改用Statement.executeUpdate()。 当然,您还需要将 SELECT 语句更改为 UPDATE 语句。

缺点是您无法访问单行数据,因为您根本没有选择它。如果你需要这个,例如要计算更新的值(在您的情况下为 test@<localipaddress>),您可能必须首先像您一样触发选择,计算内存中的更新,然后使用 PreparedStatementBatch Update 执行相应的 UPDATE 语句。

Prepared Statement 示例:

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;
    }

【讨论】:

  • 感谢您的快速回复。 @亚历山大
猜你喜欢
  • 1970-01-01
  • 2016-12-17
  • 2021-11-18
  • 1970-01-01
  • 2016-08-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-11-26
相关资源
最近更新 更多