0

PreparedStatementServelts( )とHTMLフォームを使用してMSAccessDBにデータを挿入しようとしています。

MSAccessの挿入クエリについて教えてください。私の要件は、フォームの値をMS Accessに挿入することです。また、フィールドの1つについてはinsert、別のテーブルから取得する必要があります。

だから私はそれをこのように持っています:

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

このように書くことはできますか?Col1に対して取得した入力に応じて、別のテーブルからcol3の値を取得する必要があります。

誰か助けてくれませんか。

4

1 に答える 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

2番目のオプションは、あるテーブルから別のテーブルに値を挿入するときに特定のシナリオを処理する必要がある場合に役立ちます。私はあなたがこれに従ってそしてあなた自身のコードを思い付くことができると思います

于 2012-07-19T23:00:22.570 に答える