繁体   English   中英

当使用Java在MySql的不同表的不同列中进行插入时,如何自动更新另一个表中的列

[英]how to automatically update a column in another table when an insertion is made in a different column in a different table in MySql using java

私人void 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

在此PreparedStatement对象中执行SQL语句,该对象必须是SQL数据操作语言(DML)语句,例如INSERT,UPDATE或DELETE

用于数据库QUERY语句的executeQuery() (例如select)
用于数据库UPDATE语句的executeUpdate()
更新

  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);
         }
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM