0

私人无效用户组合(){

    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() 发出数据操作语句;”

有人可以帮我吗?先感谢您

4

1 回答 1

1

使用PreparedStatement#executeUpdate

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

executeQuery()用于数据库 QUERY 语句(如 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);
         }
    }
于 2013-09-12T11:12:29.517 回答