java - 使用 MS Access 的 PepareStatement,其中某些值来自不同的表

标签 java mysql ms-access servlets

我正在尝试使用 Servelts (PreparedStatement) 和 HTML 表单将数据插入 MS Access DB。

有人可以帮我解决 MS Access 插入查询吗?我的要求是将表单值插入到 MS Access 中,并且插入期间的字段之一必须来自不同的表。

所以我是这样的:

insert into tablename(Col1, col2, col3)
values(?,?, select col3 from diffferent_table where name=col1))

可以这样写吗?我必须根据 Col1 的输入从不同的表中获取 col3 的值

有人可以帮忙吗?

最佳答案

这样做的一种方法是:

PreparedStatement insertStatement= connection.prepareStatement("insert into tablename(col1, col2, col3) values(?,?, select col3 from different_table where name = ?)");
//Then set your parameters accordingly. As per your requirement, the 1st and last paramter should've the same value

如果我是你,我会做以下事情:

PreparedStatement retrieveStatement= connection.prepareStatement("select col3 from diffferent_table where name=?");
PreparedStatement insertStatement= connection.prepareStatement("insert into tablename(col1, col2, col3) values(?,?, ?)");

//set the value
retrieveStatement.setXX(1,Col1);

然后,我将通过执行 retrieveStatement 从结果集中检索值,并在 insertStatement 中设置该值

如果您需要在将一个表中的值插入另一个表中时处理特定场景,第二个选项将有所帮助。 我想你可以按照这个并提出你自己的代码

关于java - 使用 MS Access 的 PepareStatement,其中某些值来自不同的表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11570453/

相关文章:

java - 如何使用 Optional.ifPresent 返回 boolean 值

php - 函数删除测试

javascript - 从 HTML 和 Javascript 文件读取 Access 数据库记录

用于显示电子邮件列中域名总数的 SQL

ms-access - 使用 Access 2007 中的 VBA 在 AutoCAD (dwg) 文件中搜索

java - 将gradle项目导入eclipse出错

Java 使用转换器在 XML header 中生成版本 "1.1"

javascript - 将多个HTML表单结果添加到mysql数据库

java - Spring boot通用字符串修剪序列化

mysql - 多次查询同一个表但在不同的列中mysql