【发布时间】:2014-07-24 11:10:49
【问题描述】:
我在 Java 应用程序中有一个奇怪的行为。
它向远程 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
“DAO”:
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)";
我怀疑这可能与我对Statement 和Connection 的使用有关。
这是一个轻量级的轻量级应用程序,我很简单,所以没有框架,也没有关于事务或提交的具体说明。
【问题讨论】:
-
我们先看看你的一些代码怎么样
-
"[...] 插入或删除数据库 [...] 将失败。"那是什么意思?这个问题描述太模糊了。
-
您在运行 executeUpdate() 时是否收到任何错误消息?请在这里发帖。如果您维护任何事务,请检查该配置两次。