如何在MySql中使用java在不同表的不同列中进行插入时自动更新另一个表中的列
私有void用户_combo(){ 它给了我一个错误“不能用executeQuery()发出数据操作语句” 有人能帮我吗?提前谢谢你 执行此PreparedStatement对象中的SQL语句,该对象必须是SQL数据操作语言(DML)语句,例如INSERT、UPDATE或DELETE如何在MySql中使用java在不同表的不同列中进行插入时自动更新另一个表中的列,java,mysql,Java,Mysql,私有void用户_combo(){ 它给了我一个错误“不能用executeQuery()发出数据操作语句” 有人能帮我吗?提前谢谢你 执行此PreparedStatement对象中的SQL语句,该对象必须是SQL数据操作语言(DML)语句,例如INSERT、UPDATE或DELETE executeQuery()用于数据库查询语句(如select) 数据库更新语句的执行更新() 更新 请尝试executeUpdate()这样我就应该使用executeUpdate()而不是executeQuery
executeQuery()
用于数据库查询语句(如select)数据库更新语句的执行更新() 更新
请尝试
executeUpdate()
这样我就应该使用executeUpdate()而不是executeQuery()?@bagyei37是的。您必须使用executeUpdate()
而不是executeQuery()
我尝试了这一次,它没有给出任何错误,但在我的jcomboBox@bagyei37要在jcomboBox中更新,您需要再次执行select查询。我已经更新了相应的代码。
try {
String sql = "insert into asset_update(User) select (Concat(first_name), ' ', (last_name)) from user";
pst = conn.prepareStatement(sql);
rs = pst.executeQuery();
while (rs.next()) {
String name = rs.getString("User");
jComboBox_Users.addItem(name);
}
} catch (Exception e) {
JOptionPane.showMessageDialog(null, e);
}
}
String sql = "insert into asset_update(User) select (Concat(first_name), ' ', (last_name)) from user";
pst = conn.prepareStatement(sql);
int i = pst.executeUpdate();//since it is insert statement use executeUpdate()
if(i>0){
pst = conn.prepareStatement("Select User from asset_update");
rs = pst.executeQuery();//since it is select statement use executeQuery()
while (rs.next()) {
String name = rs.getString("User");
jComboBox_Users.addItem(name);
}
}