0

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

可以在 MS Acces Insert Query 上帮助我吗?我的要求是将表单值插入 MS Access,并且其中一个字段insert必须来自不同的表。

所以我有这种方式:

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

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

有人可以帮忙吗。

4

1 回答 1

1

一种方法是:

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

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

于 2012-07-19T23:00:22.570 回答