我想在java gui中的数据显示为“product\u name”的地方插入数据,当我插入“product\u name”时,数据库会自动插入id key,因此我决定尝试在sql yog/mysql中创建嵌套查询—它在sql yog/mysql上运行得非常好。但是当我把查询放在netbean源代码上,并在insert all字段之后尝试按add按钮时,会显示一个错误“cannotissuedatamanipulationstatementswithexecutequery()”。我错过了什么?
查询:
INSERT INTO buy (id_sup,id_product,price,date)
VALUES ((SELECT id_sup FROM supplier WHERE name_sup='Wattshituya'), (SELECT id_product FROM barang WHERE name_product='Bucatti'), 200000,NOW());
netbean中的源代码:
private void add_buy() {
try {
Connection kon = koneksi.koneksiDb();
Statement st = kon.createStatement();
String sql = "INSERT INTO buy (id_sup,id_product,price,date) VALUES ((SELECT id_sup FROM supplier WHERE name_sup='"+comboboxSup.getSelectedItem()+"'),(SELECT id_product FROM product WHERE name_product='"+comboboxProd.getSelectedItem()+"'),"
+ "'"+txt_price.getText()+"','"+txt_date.getText()+"')";
ResultSet rs = st.executeQuery(sql);
JOptionPane.showMessageDialog(null, "Saved");
} catch (Exception e) {
JOptionPane.showMessageDialog(this, e.getMessage());
}
table_pembelian();
reset_formPem();
}
更新已解决
它需要对我的源代码进行一个简单的更改,从
ResultSet rs = st.executeQuery(sql);
应该是
ResultSet rs = st.executeUpdate(sql);
暂无答案!
目前还没有任何答案,快来回答吧!