我可以在使用保存点时将自动提交设置为 ON
can I set autocommit to ON while using savepoints
我是 Java 数据库编程的新手。
最近在做项目的时候,卡在了一个概念里,找了很多都没有找到解决方案来满足我的查询,这可能会帮助我摆脱问题。
其实问题是:
我有三个 table,假设一个主(包含实际数据的公共字段)table 和两个 child table(包含其他不同的字段)根据某些标准)。 Main table 包含部分信息,其余信息将根据某些条件仅保存在 child table.
中的一个中
现在的情况是这样的,我关闭了自动提交,然后触发一个插入查询。因此,当插入查询被触发时,数据库将在 mysql 中为其提供一个唯一 ID,因为 ID 字段是 autoIncrement。现在触发 Select 查询,我想从主 table 中提取该 ID。所以,这是我的问题,SELECT QUERY 能够提取我刚刚保存的那条特定记录的 ID 吗? 请记住自动提交设置为 false,我还没有提交。
我这样做是因为我想将该唯一 ID 插入 child table 之一,以便我可以关联 table 之间的信息。因此,在找到 ID 之后,我再次触发了一个新的 INSERT 查询,以将其余数据保存在 child table 之一中,现在具有唯一 ID 和其余数据。然后在成功插入后,我已经提交了连接。
此外,我希望信息保存在两个(主要和 child 之一)table 中,或者如果发生任何故障,细节不会完全保存,所以我不要丢失部分信息。
请帮帮我。如果你能解释一下自动提交和保存点之间的关系。什么时候用,哪些东西要记住。请提供一些真正的资源,如果可以的话,展示它们的性质,它们在不同情况下的工作方式等。我用谷歌搜索但没有得到任何这样有用的信息。我想深入了解它。
提前致谢:)
您似乎想在将记录添加到 table 时获取 ID。如果您使用 getGeneratedKeys(),则在插入记录时可用。自动提交不能用于 return this.
以下代码显示了如何完成此操作。
/**
* Insert to database using JDBC 3.0 + which will return the key of the new row.
*
* @param sql is the sql insert to send to the database
* @return the key for the inserted row
* @throws DBSQLException
*/
public static int insertAndReturnKey(Connection dbConnection, String sql, List<SqlField> params) throws DBSQLException
{
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
String paramList = null;
try {
preparedStatement = dbConnection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
// Setup your parameters
int result = preparedStatement.executeUpdate();
resultSet = preparedStatement.getGeneratedKeys();
if (resultSet.next()) {
return (resultSet.getInt(1));
} else {
// throw an exception from here
throw new SQLException("Failed to get GeneratedKey for [" + sql + "]");
}
} catch (SQLException ex) {
throw new DBSQLException(buildErrorMessage(ex, sql, params));
} finally {
DBConnector.closeQuietly(preparedStatement);
DBConnector.closeQuietly(resultSet);
}
}
我是 Java 数据库编程的新手。 最近在做项目的时候,卡在了一个概念里,找了很多都没有找到解决方案来满足我的查询,这可能会帮助我摆脱问题。
其实问题是:
我有三个 table,假设一个主(包含实际数据的公共字段)table 和两个 child table(包含其他不同的字段)根据某些标准)。 Main table 包含部分信息,其余信息将根据某些条件仅保存在 child table.
中的一个中现在的情况是这样的,我关闭了自动提交,然后触发一个插入查询。因此,当插入查询被触发时,数据库将在 mysql 中为其提供一个唯一 ID,因为 ID 字段是 autoIncrement。现在触发 Select 查询,我想从主 table 中提取该 ID。所以,这是我的问题,SELECT QUERY 能够提取我刚刚保存的那条特定记录的 ID 吗? 请记住自动提交设置为 false,我还没有提交。
我这样做是因为我想将该唯一 ID 插入 child table 之一,以便我可以关联 table 之间的信息。因此,在找到 ID 之后,我再次触发了一个新的 INSERT 查询,以将其余数据保存在 child table 之一中,现在具有唯一 ID 和其余数据。然后在成功插入后,我已经提交了连接。
此外,我希望信息保存在两个(主要和 child 之一)table 中,或者如果发生任何故障,细节不会完全保存,所以我不要丢失部分信息。
请帮帮我。如果你能解释一下自动提交和保存点之间的关系。什么时候用,哪些东西要记住。请提供一些真正的资源,如果可以的话,展示它们的性质,它们在不同情况下的工作方式等。我用谷歌搜索但没有得到任何这样有用的信息。我想深入了解它。
提前致谢:)
您似乎想在将记录添加到 table 时获取 ID。如果您使用 getGeneratedKeys(),则在插入记录时可用。自动提交不能用于 return this.
以下代码显示了如何完成此操作。
/**
* Insert to database using JDBC 3.0 + which will return the key of the new row.
*
* @param sql is the sql insert to send to the database
* @return the key for the inserted row
* @throws DBSQLException
*/
public static int insertAndReturnKey(Connection dbConnection, String sql, List<SqlField> params) throws DBSQLException
{
PreparedStatement preparedStatement = null;
ResultSet resultSet = null;
String paramList = null;
try {
preparedStatement = dbConnection.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
// Setup your parameters
int result = preparedStatement.executeUpdate();
resultSet = preparedStatement.getGeneratedKeys();
if (resultSet.next()) {
return (resultSet.getInt(1));
} else {
// throw an exception from here
throw new SQLException("Failed to get GeneratedKey for [" + sql + "]");
}
} catch (SQLException ex) {
throw new DBSQLException(buildErrorMessage(ex, sql, params));
} finally {
DBConnector.closeQuietly(preparedStatement);
DBConnector.closeQuietly(resultSet);
}
}