我正在尝试使用 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/