gpt4 book ai didi

java - 在 MySQL JDBC 中*在*数据库插入之前获取 AUTO_INCRMENT

转载 作者:行者123 更新时间:2023-11-29 06:29:17 24 4
gpt4 key购买 nike

我正在使用 JDBC 开发 MySQL 数据库项目。它使用与外键链接的父/子表。

TL;DR:我希望能够在 INSERT 语句之前获取表的 AUTO_INCRMENT id。我已经知道 JDBC 中的 getGenerateKeys() 方法可以在插入后执行此操作,但我的应用程序在插入之前需要 ID。也许对于这个特定应用程序的问题有更好的解决方案?详情如下:

<小时/>

在此应用程序的一部分中,用户可以通过表单或控制台输入来创建新项目以输入详细信息 - 其中一些详细信息采用新项目中“子项目”的形式。

这些输入存储在 Java 对象中,以便表的每一行对应于这些对象之一 - 以下是一些示例:

MainItem
- id (int)
- a bunch of other details...

MainItemTitle
- mainItemId (int)
- languageId (int)
- title (String)

ItemReference
- itemId (int) <-- this references MainItem id
- referenceId (int) <-- this references another MainItem id that is linked to the first

因此,本质上每个 Java 对象都代表 MySQL 数据库相关表中的一行。

当我将输入的值存储到对象中时,我使用一个虚拟 ID,如下所示:

private static final int DUMMY_ID = 0;

...

MainItem item = new MainItem(DUMMY_ID, ...);

// I read each of the titles and initialise them using the same dummy id - e.g.
MainItemTitle title = new MainItemTitle(DUMMY_ID, 2, "Here is a a title");

// I am having trouble with initialising ItemReference so I will explain this later

读取用户输入后,它们将存储在“holder”类中:

class MainItemValuesHolder {

MainItem item;
ArrayList<MainItemTitle> titles;
ArrayList<ItemReference> references;
// These get initialised and have getters and setters, omitted here for brevity's sake
}

...

MainItemValuesHolder values = new MainItemValuesHolder();
values.setMainItem(mainItem);
values.addTitle(englishTitle);
values.addTitle(germanTitle);
// etc...

在应用程序的最后一层(在值持有者作为参数传递的另一个类中),读取“持有者”类中的数据并将其插入数据库中:

// First insert the main item, belonging to the parent table

MainItem mainItem = values.getMainItem();
String insertStatement = mainItem.asInsertStatement(true); // true, ignore IDs
// this is an oversimplification of what actually happens, but basically constructs the SQL statement while *ignoring the ID*, because...

int actualId = DbConnection.insert(insertStatement);
// updates the database and returns the AUTO_INCREMENT id using the JDBC getGeneratedKeys() method

// Then do inserts on sub-items belonging to child tables
ArrayList<MainItemTitle> titles = values.getTitles();
for (MainItemTitle dummyTitle : titles) {
MainItemTitle actualTitle = dummyTitle.replaceForeignKey(actualId);
String insertStatement = actualTitle.asInsertStatement(false); // false, use the IDs in the object
DbConnection.insert(insertStatement);
}

现在,问题在于将此过程用于 ItemReference。因为它链接两个 MainItem,所以使用(或多个)虚拟 ID 预先构造对象会破坏这些关系。

最明显的解决方案似乎是能够预先获取AUTO_INCRMENT ID,这样我就不需要使用虚拟ID。

我认为另一种解决方案是在输入数据后立即插入数据,但我更愿意将应用程序的不同功能保留在单独的类中 - 因此一个类负责一个操作。而且,通过在输入数据后立即插入,那么如果用户在完成输入“主条目”、标题、引用文献等所有数据之前选择取消,则需要删除现在无效的数据。

<小时/>

总之,在插入之前,我如何才能获得AUTO_INCRMENT?对于这个特定的应用程序有更好的解决方案吗?

最佳答案

无法获取插入之前的值。您无法知道可能会采取哪些其他行动。 AUTO_INCRMENT 可能不会递增 1,您可能已设置但可以更改。

可以使用临时表来存储由您控制的键的数据。我建议使用 Uuid 而不是 Id,这样您就可以假设它始终是唯一的。然后您的其他类可以将数据复制到 Activity 表中,您仍然可以使用 Uuid 链接数据以在临时表中查找相关数据,但按照对数据库有意义的顺序写入数据(因此“根”) ' 首先记录以获取其 key ,然后在需要时使用它。

关于java - 在 MySQL JDBC 中*在*数据库插入之前获取 AUTO_INCRMENT,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57402471/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com