11 Replies Latest reply on May 12, 2010 5:41 AM by Sree_D

    sending data from datagrid to database

    dhifallah Level 1

      hello ,

       

      i'm trying to update chages done in my datagrid to my database ,but it doens't work ,

      in my mxml i'm calli,g a servlet java trough an HTTPService

       

      have you any suggestion please.

        • 1. Re: sending data from datagrid to database
          dhifallah Level 1

          my HTTPService is as the following:

          <mx:HTTPService

           

          id="MiseAJourTable" url="http://localhost:8080/SupportWEBJ2EE/ServletMiseAJourDataBase"

          useProxy=false" method="POST" resultFormat="text" result="updatedDataResult(event)">

           

          </mx:HTTPService>

          and my function sendDtata() is like that:

           

          private function sendData():void

          {

           

          var objSend:Object = new Object();

          MiseAJourTable.send(objSend);

           

          }

           

          I know that in my function sendData() I have to add other things, but i don't know what are these things exactly ,may be I have to add arguments to objSend like my datagrid for exemple

           

          please help

          • 2. Re: sending data from datagrid to database
            Sree_D

            The issue is we can not send an Object directly over httpservice as it in AS3 and the backend is java. The data should be converted to XML and then sent as request parameter to backend. Try something like below

             

             

             

            id="MiseAJourTable" url="http://localhost:8080/SupportWEBJ2EE/ServletMiseAJourDataBase"

             

             

             

            </mx:HTTPService>

             

            In code put something like below.

             

             

             

            var xmlObj:XML= xml format of the data to be sent to backend.

            var params:Object = new Object();

            MiseAJourTable.url=url to be called

             

             

            params.inputXmlString=xmlObj.toXMLString();

            MiseAJourTable.send(params);

             

            In the backend once the HTTPRequest object is available

             

            update (HTTPRequest req,HTTPResponse res)

            {

                 String inXmlString = req.getParameter("inputXmlString")

            }

            useProxy=false" method="POST" resultFormat="Object" result="updatedDataResult(event)">

            <mx:HTTPService

            • 3. Re: sending data from datagrid to database
              dhifallah Level 1

              thank you for your reply, but I didn't understand where can I put the

              update (HTTPRequest req,HTTPResponse res)

              {

                   String inXmlString = req.getParameter("inputXmlString")

              }

               

              I have changed my code as the following:

               

              private function sendData():void

              {

               

              var xmlObj:XML="<?xml version=\"1.0\" encoding=\"utf-8\"?>";

               

              var params:Object = new Object();

               

              MiseAJourTable.url=http://localhost:8080/SupportWEBJ2EE/ServletMiseAJourDataBase;

              params.inputXmlString=xmlObj.toXMLString();

               

              MiseAJourTable.send(params);

               

              }

               

               

               

              <mx:HTTPService id="MiseAJourTable" url="http://localhost:8080/SupportWEBJ2EE/ServletMiseAJourDataBase"

              useProxy="false" method="POST" resultFormat="Object" result="updatedDataResult(event)">

               

               

              </mx:HTTPService>

               

               

              but I have an error in the first line of (var xmlObj:XML="<?xml version=\"1.0\" encoding=\"utf-8\"?>";)

               

              1067: Contrainte implicite d'une valeur du type String vers un type sans rapport XML.

               

              How can I correct that please!!

               

              • 4. Re: sending data from datagrid to database
                Sree_D Level 2

                The method to use to get the data in backend depends on your J2EE frame work for example if you are using spring onSubmit method gets called when you send the http service from flex. Try to find such method in you frame work and check the request parameters for the input value.

                 

                The error u are getting is because A string value is assigned to XML which takes an object. Try to find some examples to use xml in flex.

                • 5. Re: sending data from datagrid to database
                  dhifallah Level 1

                  I'm still blocked and still didn't find a solution , here is what i have modifyed:

                   

                  <mx:HTTPService

                   

                   

                  id="MiseAJourTable" url="http://localhost:8080/SupportWEBJ2EE/ServletMiseAJourDataBase"

                  useProxy="

                  false" method="POST" resultFormat="e4x" result="updatedDataResult(event)">

                   

                   

                  <mx:request>

                   

                   

                  <branche_SVN>{choix.branche_SVN}</branche_SVN>

                   

                   

                  <environnement>{choix.environnement}</environnement>

                   

                   

                  <tag_version>{choix.tag_version}</tag_version>

                   

                   

                  <revision_ihm>{choix.revision_ihm}</revision_ihm>

                   

                   

                  <revision_metier>{choix.revision_metier}</revision_metier>

                   

                   

                  <date_initialisation>{choix.date_initialisation}</date_initialisation>

                   

                   

                  <version_cible>{choix.version_cible}</version_cible>

                   

                   

                  <lot_cible>{choix.lot_cible}</lot_cible>

                   

                   

                  <liste_organisme>{choix.liste_organisme}</liste_organisme>

                   

                   

                  </mx:request>

                   

                   

                   

                  </mx:HTTPService>

                   

                   

                  my fuction is as the following:

                   

                  private function updatedDataResult(event:ResultEvent):void

                  {

                   

                   

                   

                  xmldata=event.result as XML;

                   

                  for each (var thexml:XML in xmldata.Base){

                   

                  var obj:ChoixVar= new ChoixVar();

                   

                  obj.branche_SVN=thexml.branche_SVN;

                  obj.environnement=thexml.environnement;

                  obj.tag_version=thexml.tag_version;

                  obj.revision_ihm=thexml.revision_ihm;

                  obj.revision_metier=thexml.revision_metier;

                  obj.date_initialisation=thexml.date_initialisation;

                  obj.version_cible=thexml.version_cible;

                  obj.lot_cible=thexml.lot_cible;

                  obj.liste_organisme=thexml.liste_organisme;

                   

                  Alert.show(

                  "branche_SVN "+obj.branche_SVN+" environnement"+obj.environnement+"tag_version "+obj.tag_version+" revision_ihm"+obj.revision_ihm+"revision_metier "+obj.revision_metier+" version_cible"+obj.version_cible+"lot_cible "+obj.lot_cible+" liste_organisme"+obj.liste_organisme, "Information")

                   

                  }

                   

                   

                   

                  var resultObj:Object = event.result;

                   

                  var result:String = resultObj.message;

                   

                  if (result=="true") {

                  Alert.show(

                  'la base de donnée a été bien mis à jour');

                  }

                  else {

                  Alert.show(

                  'erreur, la base de donnée n\'est pas mis à jour');

                  }

                   

                  }

                   

                   

                   

                  <mx:Button

                   

                  label="valider" cornerRadius="20" fontFamily="Times New Roman" fontSize="14" fontWeight="bold" color="#1D444C" id="myButton" click="sendData()" />

                   

                   

                   

                  private function sendData():void

                  {

                   

                  var xmlObj:XML=new XML("<?xml version=\"1.0\" encoding=\"utf-8\"?>");

                   

                   

                  var params:Object = new Object();

                   

                  MiseAJourTable.url="http://localhost:8080/SupportWEBJ2EE/ServletMiseAJourDataBase";

                   

                  params.inputXmlString = xmlObj.toXMLString();

                   

                  MiseAJourTable.send(params);

                   

                  }

                   

                   

                   

                   

                  in my servlet I have the following code:

                  in my servlet I have the following code:

                   

                   

                   

                   

                   

                   

                   

                   

                  import java.io.*;
                  import java.sql.*;

                  import javax.servlet.*;
                  import javax.servlet.http.*;
                  import java.io.IOException;
                  import java.io.PrintWriter;

                  import javax.servlet.ServletException;
                  import javax.servlet.http.HttpServlet;
                  import javax.servlet.http.HttpServletRequest;
                  import javax.servlet.http.HttpServletResponse;


                  public class ServletMiseAJourDataBase extends HttpServlet {
                  public void init(ServletConfig config) throws ServletException{
                       super.init(config);
                     }

                     // Driver JDBC ORACLE
                     String driverName = "com.mysql.jdbc.Driver";
                   
                  String user ="*****"
                  pwd="******"
                  url="jdbc:mysql://10.0.10.33:3306/Supportweb";
                    

                    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                     
                    doPost(request, response);
                  }

                  protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
                   
                    String connectionURL = "jdbc:mysql://10.0.10.33:3306/Supportweb";
                    System.out.println("connexion ok ");
                    Connection connection=null;
                       ResultSet rs;
                       response.setContentType("text/html");
                     
                       //get the variables entered in the datagrid
                        
                       String branche_SVN = request.getParameter("branche_SVN");
                       String environnement = request.getParameter("environnement");
                       String tag_version = request.getParameter("tag_version");
                       String revision_ihm = request.getParameter("revision_ihm");
                       String revision_metier = request.getParameter("revision_metier");
                       String date_initialisation = request.getParameter("date_initialisation");
                       String version_cible = request.getParameter("version_cible");
                       String lot_cible = request.getParameter("lot_cible");
                       String liste_organisme = request.getParameter("liste_organisme");
                    
                     System.out.println("branche_SVN: "+branche_SVN);
                    System.out.println("environnement: "+environnement);
                    System.out.println("tag_version: "+tag_version);
                    System.out.println("revision_ihm: "+revision_ihm);
                    System.out.println("date_initialisation: "+date_initialisation);
                    System.out.println("version_cible: "+version_cible);
                    System.out.println("lot_cible: "+lot_cible);
                    System.out.println("liste_organisme: "+liste_organisme);
                      
                     PrintWriter out = response.getWriter();
                       try {
                         // Load the database driver
                         Class.forName("com.mysql.jdbc.Driver");
                         // Get a Connection to the database
                         connection = DriverManager.getConnection(connectionURL, "supportweb", "supportweb");
                         //Add the data into the database
                        String sql ="UPDATE `Supportweb`.`gestion_environnement_bis`SET branche_SVN = "+ branche_SVN +", environnement = "+ environnement +", tag_version = "+tag_version+",revision_ihm = " + revision_ihm+",revision_metier = " + revision_metier+",date_initialisation = " + date_initialisation+",version_cible = " + version_cible+",lot_cible = " + lot_cible+",liste_organisme = " + liste_organisme+"";
                         PreparedStatement pst = connection.prepareStatement(sql);
                         pst.setString(1, branche_SVN);
                         pst.setString(2, environnement);
                         pst.setString(3, tag_version);
                         pst.setString(4, revision_ihm);
                         pst.setString(5, revision_metier);
                         pst.setString(6, date_initialisation);
                         pst.setString(7, version_cible);
                         pst.setString(8, lot_cible);
                         pst.setString(9, liste_organisme);
                         int numRowsChanged = pst.executeUpdate();
                         // show that the new account has been created
                         out.println(" la base de donnée a était mise à jour ");
                      
                         pst.close();
                       }
                       catch(ClassNotFoundException e){
                         out.println("Couldn't load database driver: " + e.getMessage());
                       }
                       catch(SQLException e){
                         out.println("SQLException caught: " + e.getMessage());
                       }
                       catch (Exception e){
                         out.println(e);
                       }
                       finally {
                         // Always close the database connection.
                         try {
                           if (connection != null) connection.close();
                         }
                         catch (SQLException ignored){
                           out.println(ignored);
                         }
                       }
                     }
                  }

                   

                   

                   

                   

                  when i excute the servlet I have this message:

                   

                  SQLException caught: Parameter index out of range (1 > number of parameters, which is 0).

                   

                   

                  else, there is update of the database , i have the following alertwhen i click on 'valider':

                  erreur, la base de donnée n\'est pas mis à jour

                   

                  and I have:

                   

                  connexion ok

                  branche_SVN: null

                  environnement: null

                  tag_version: null

                  revision_ihm: null

                  date_initialisation: null

                  version_cible: null

                  lot_cible: null

                  liste_organisme: null

                   

                  please i nead your help because i'm realy blocked and do not what to do , in addition i found help only here in this forum , please please please

                  • 6. Re: sending data from datagrid to database
                    Sree_D Level 2

                    Looking at high level the code looks ok and its good that you are recieving the request in the Java code. The code currently is not sending any variables which you are trying to get from request. We are only setting one parameter inputXmlString only.

                     

                    Try    printing the value of inputXmlString  String inputXmlString  = request.getParameter("inputXmlString");

                     

                    Let me know if this works and you recieve <?xml version=\"1.0\" encoding=\"utf-8\"?> which we are setting in flex.

                    • 7. Re: sending data from datagrid to database
                      dhifallah Level 1

                      no it doesn't work:

                      String inputXmlString = request.getParameter("inputXmlString");

                      System.out.println("inputXmlString: "+inputXmlString);

                       

                       

                      in the screan i have  String inputXmlString: null  as result

                       

                       

                       

                      • 8. Re: sending data from datagrid to database
                        Sree_D Level 2

                        check again by replacing params.inputXmlString = xmlObj.toXMLString(); with params.inputXmlString = "hello"

                         

                        It should work if you are recieving the same request object that you are sending from flex.

                        • 9. Re: sending data from datagrid to database
                          dhifallah Level 1

                          i replaced it as you said but I'm still having the same ansaw :

                          inputXmlString: null

                          • 10. Re: sending data from datagrid to database
                            dhifallah Level 1

                            now i trayed once again and here i get

                             

                            inputXmlString: hello

                            • 11. Re: sending data from datagrid to database
                              Sree_D Level 2

                              Good to see that. Now if you want to send each element seperately then do it similar to what you tried now

                               

                              params.parameter1=value1;

                              params.parameter2=value2;

                              params.parameter3=value3;

                              params.parameter4=value4;

                               

                              and at the backend get these values

                              String parameter1= req.getParameter("parameter1");

                              String parameter2= req.getParameter("parameter2");

                              String parameter4= req.getParameter("parameter4");

                               

                              OR send all the data in single value and parse it at the backend like below

                               

                              params.inputXmlString='<data><parameter1>value1></parameter1><parameter2>value2></parameter2><parameter3>value3></parameter3><parameter4>value4></parameter4></data>';

                               

                               

                               

                              Hope this solves your problem.

                              String parameter3= req.getParameter("parameter3");