私有(private)无效User_combo(){
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);
}
}
它给了我错误“无法使用executeQuery()发出数据操作语句;”
有人可以帮我解决这个问题吗?预先感谢您
最佳答案
使用PreparedStatement#executeUpdate
Executes the SQL statement in this PreparedStatement object, which must be an SQL Data Manipulation Language (DML) statement, such as INSERT, UPDATE or DELETE
executeQuery()
用于数据库查询语句(如 select)
executeUpdate()
用于数据库 UPDATE 语句
更新
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);
}
}
关于java - 当使用java在MySql中的不同表中的不同列中进行插入时,如何自动更新另一个表中的列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18762597/