0

I executed a code that is used to display the specified information from a content of a field in a column "update_action" a table "incident" a database "base_rapport_tt" on WampServer. and in my table, I added another column in which I will store the displayed information. My goal is to display the result of the whole column and store it in the new column order.

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

I tried with this code:

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

the result of the first column of the recipient field is well. But the problem is that the other fields is the result of the former.

thanks.

  • 1
    A smallt tipp, consider taking a look at [PreparedStatement](http://docs.oracle.com/javase/7/docs/api/java/sql/PreparedStatement.html). It´s better to save your self from sql injection – SomeJavaGuy Apr 14 '15 at 09:47

1 Answers1

0

Problem is in

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

This query updates all rows (id_incident like '%')! you need to use filter Primary Key (I think it id_incident )!

You have to execute this query

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

P.S. 1) And it's not good to use single update. The best way is using bulk update for exampe java - Multipile update statements in MySql
2) Why do you use different connections for select and update? It are the same!

Community
  • 1
  • 1
rpc1
  • 688
  • 10
  • 23
  • thanks you . I tried with your answer but nothing. the same problem – nabil123456 Apr 15 '15 at 08:41
  • I executed a small code that allows me to take some information from a field of a column and store it in another field in another column in the same table.pour the first field is done. But, for the other fields of the destination column, it stores the result of the first recipient fields. – nabil123456 Apr 15 '15 at 10:34
  • output exception from catch (Exception e) { //JOptionPane.showMessageDialog(this,e); } – rpc1 Apr 15 '15 at 11:31
  • this message appears : String index out of range: -1 – nabil123456 Apr 15 '15 at 13:37