简体   繁体   中英

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

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

    }
}

it gives me the error "can not issue data manipulation statements with executeQuery();"

can someone please help me with this? Thank you in advance

Use 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() for database QUERY statement(Like select)
executeUpdate() for database UPDATE statements

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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