一种方法是:
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
如果您在将值从一个表插入到另一个表时需要处理特定场景,第二个选项将有所帮助。我想你可以按照这个并想出你自己的代码