BANK
BANK

Reputation: 221

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

Upvotes: 0

Views: 57

Answers (1)

Prabhaker A
Prabhaker A

Reputation: 8473

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

Upvotes: 1

Related Questions