有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

mysql Java语句。executeUpdate(sql)在executeQuery(sql)工作时不工作

我在Java应用程序中有一个wierd行为。 它向远程MySQL数据库发出简单的查询和修改。我发现由executeQuery()运行的查询工作正常,但通过executeUpdate()运行的数据库插入或删除操作将失败

排除出现在脑海中的第一件事:应用程序连接的用户设置了正确的权限,因为同一个INSERT在同一台机器上运行,但在DBeaver中,将产生所需的修改

一些代码:

连接创建

Class.forName("com.mysql.jdbc.Driver");
connection = DriverManager.getConnection(url, user, pass);

问题部分:

Statement parentIdStatement = connection.createStatement();
String parentQuery = String.format(ProcessDAO.GET_PARENT_ID, parentName);
if (DEBUG_SQL) {
      plugin.getLogger().log(Level.INFO, parentQuery);
}
ResultSet result = parentIdStatement.executeQuery(parentQuery);
result.first();
parentId = result.getInt(1);
if (DEBUG_SQL) {
      plugin.getLogger().log(Level.INFO, parentId.toString()); // works, expected value
}

Statement createContainerStatement = connection.createStatement();
String containerQuery = String.format(ContainerDAO.CREATE_CONTAINER, parentId, myName);
if (DEBUG_SQL) {
      plugin.getLogger().log(Level.INFO, containerQuery); // works when issued through DBeaver
}
createContainerStatement.executeUpdate(containerQuery); // does nothing

“DAOs”:

ProcessDAO.GET_PARENT_ID = "SELECT id FROM mon_process WHERE proc_name = '%1$s'";
ContainerDAO.CREATE_CONTAINER = "INSERT INTO mon_container (cont_name, proc_id, cont_expiry, cont_size) VALUES ('%2$s', %1$d, CURRENT_TIMESTAMP(), NULL)";

我怀疑这可能与我对StatementConnection的使用有关。 这是一个轻量级的应用程序,使用简单,因此没有框架,也没有关于事务或提交的具体说明


共 (1) 个答案

  1. # 1 楼答案

    所以,最终,这段代码还不错。今天起作用了

    回答以下问题:在类似情况下,首先看哪里(选择有效,但更新/插入/删除无效)

    如果权限不是问题,那么可能是您试图修改的表上有一个锁。在我的例子中,有人离开时打开了一个未限制的事务

    正确的SQL异常日志记录(在我的例子中,这是次优的)将帮助您解决这个问题