将处理结果存储在 table 中

store the results of a treatment in table

我执行了一段代码,用于显示列中字段内容的指定信息 "update_action" a table "incident" a database "base_rapport_tt" on Wamp服务器。在我的 table 中,我添加了另一列,我将在其中存储显示的信息。我的目标是显示整个列的结果并将其存储在新的列顺序中。

try
    {   
        String Sql="Select Update_Action from   incident    where id_incident ='"+jTextField3.getText()+"' and Status like 'Closed'";
        con = getConnection("jdbc:mysql://localhost:3306/base_rapport_tt","root","");
        stmt=con.createStatement();
        rs=stmt.executeQuery(Sql);

        while(rs.next()) {
            str=rs.getString("update_Action");

            while(!"".equals(str)){
              int debut=str.indexOf('(')+1;
              int fin=str.indexOf(')',debut);
              nom += " "+str.substring(debut,fin);
              str=str.substring(fin+1,str.length()); 
              nom+=", "; 
            } 
         }
    } 
    catch (Exception e) {
       JOptionPane.showMessageDialog(this,e);
   }

我试过这个代码:

try
{ 
   String Sql="Select Update_Action,id_incident from   incident
where Status like 'Closed'";
  con = getConnection("jdbc:mysql://localhost:3306/base_rapport_tt","root","");
 stmt=con.createStatement();
 rs=stmt.executeQuery(Sql);

 while(rs.next()) {
     str=rs.getString("update_Action");
     nom="";
     while(!"".equals(str)){
        int debut=str.indexOf('(')+1;
        int fin=str.indexOf(')',debut);
        nom += " "+str.substring(debut,fin);
        str=str.substring(fin+1,str.length()); 
        nom+=", ";  

        Connection conn = DriverManager.getConnection("jdbc:mysql://localhost:3306/base_rapport_tt", "root", "");
        String query = "update incident set intervenants = ? where id_incident like '%'";
        java.sql.PreparedStatement preparedStmt = conn.prepareStatement(query);
        preparedStmt.setString(1,nom);
        preparedStmt.executeUpdate();
        conn.close();    
      }
   }
}
catch (Exception e) 
{
           //JOptionPane.showMessageDialog(this,e);
}

收件人字段第一列的结果很好。但是问题是其他字段都是前者的结果

谢谢。

问题出在

 String query = "update incident set intervenants = ? where id_incident like '%'";

此查询更新所有行id_incident如“%”)!你需要使用过滤器主键(我认为它 id_incident )!

您必须执行此查询

String query = "update incident set intervenants = ? where id_incident = ? ;
preparedStmt.setString(1,nom);
preparedStmt.setInt(2,rs.getInt("id_incident")); 

P.S。 1)并且使用单一更新并不好。最好的方法是使用 bulk update 例如 java - Multipile update statements in MySql
2) 为什么 select 和更新使用不同的连接?都一样!