Problem with updating a record in database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • veerapureddy
    New Member
    • Jun 2007
    • 21

    Problem with updating a record in database

    Hai everybody,

    I am facing problem with updating a record .actullay im getting some data from database(Access ) and populating it on a html form.im trying to edit some texts and update, its not reflecting in database. here is the code.
    (the request parameters from html form, there is no mismatch between variable names)
    *************** *************** *************** *************** *************** *************** **private void updateEmployee( HttpServletRequ est request, HttpServletResp onse response) throws IOException{
    PrintWriter out = response.getWri ter();
    String firstname=reque st.getParameter ("firstname" );
    String lastname=reques t.getParameter( "lastname") ;
    String empid=request.g etParameter("em pid");
    String extnnum = request.getPara meter("extnno") ;
    String lotusmailid=req uest.getParamet er("lotusmailid ");
    String personalmailid= request.getPara meter("personal mailid");
    String pplmanager=requ est.getParamete r("pplmanager") ;
    String projectmanager= request.getPara meter("projectm anager");
    String hrid=request.ge tParameter("hri d");
    String unixlogin=reque st.getParameter ("unixlogin" );
    String attuid=request. getParameter("a ttuid");
    String portnum=request .getParameter(" portno");
    String address=request .getParameter(" address");
    String city=request.ge tParameter("cit y");
    String state=request.g etParameter("st ate");
    String pincode=request .getParameter(" pincode");
    String mobnum=request. getParameter("m obno");
    String homenum=request .getParameter(" hometelno");
    String project=request .getParameter(" project");
    String teamleader=requ est.getParamete r("teamleader") ;
    out.println("EM P ID IS:"+empid);


    try
    {


    Class.forName(" sun.jdbc.odbc.J dbcOdbcDriver") ;
    Connection con =DriverManager. getConnection(" jdbc:odbc:giom" );
    System.out.prin tln("GOT CONNECTION FOR EMPLOYEE UPDATE HANDLER ");
    Statement s = con.createState ment();

    System.out.prin tln("GOT CONNECTION yaar");
    String sql = "UPDATE EMPLOYEE SET FNAME='"+ firstname +"', LNAME='"+ lastname +"', OFF_EMAIL='"+ lotusmailid +"',PRSNL_EMAIL ='"+ personalmailid +"',PPL_MANAGER ='"+ pplmanager +"',PROJECT_MAN AGER='"+ projectmanager +"',EXTN='"+ extnnum +"',PROJECT= '"+ project+"',TEAM _LEADER='"+ teamleader +"',HRID='"+ hrid +"',ATT_UID= '"+ attuid +"',UNIX_LOGIN= '"+ unixlogin+"',PO RT_NO='"+ portnum+"',MOBI LE_NO='"+ mobnum +"',HOME_NO= '"+ homenum +"',CONT_ADDR=' "+ address +"',CITY='"+ city +"',STATE='" + state +"',PINCODE= '"+ pincode +"' WHERE EMP_ID='"+ empid +"'" ;


    int i = s.executeUpdate (sql);
    System.out.prin tln("i value is :"+i);
    if (i!= 0)
    {
    String message = "Successful ly deleted"+i+" users.";
    System.out.prin tln(message);
    out.println(mes sage);
    }
    }
    catch (ClassNotFoundE xception e) {
    System.out.prin tln(e.toString( ));
    }
    catch (SQLException e) {
    e.printStackTra ce();
    }
    catch (Exception e) {
    System.out.prin tln(e.toString( ));
    }


    }
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by veerapureddy
    Hai everybody,

    I am facing problem with updating a record .actullay im getting some data from database(Access ) and populating it on a html form.im trying to edit some texts and update, its not reflecting in database. here is the code.
    (the request parameters from html form, there is no mismatch between variable names)
    *************** *************** *************** *************** *************** *************** **private void updateEmployee( HttpServletRequ est request, HttpServletResp onse response) throws IOException{
    PrintWriter out = response.getWri ter();
    String firstname=reque st.getParameter ("firstname" );
    String lastname=reques t.getParameter( "lastname") ;
    String empid=request.g etParameter("em pid");
    String extnnum = request.getPara meter("extnno") ;
    String lotusmailid=req uest.getParamet er("lotusmailid ");
    String personalmailid= request.getPara meter("personal mailid");
    String pplmanager=requ est.getParamete r("pplmanager") ;
    String projectmanager= request.getPara meter("projectm anager");
    String hrid=request.ge tParameter("hri d");
    String unixlogin=reque st.getParameter ("unixlogin" );
    String attuid=request. getParameter("a ttuid");
    String portnum=request .getParameter(" portno");
    String address=request .getParameter(" address");
    String city=request.ge tParameter("cit y");
    String state=request.g etParameter("st ate");
    String pincode=request .getParameter(" pincode");
    String mobnum=request. getParameter("m obno");
    String homenum=request .getParameter(" hometelno");
    String project=request .getParameter(" project");
    String teamleader=requ est.getParamete r("teamleader") ;
    out.println("EM P ID IS:"+empid);


    try
    {


    Class.forName(" sun.jdbc.odbc.J dbcOdbcDriver") ;
    Connection con =DriverManager. getConnection(" jdbc:odbc:giom" );
    System.out.prin tln("GOT CONNECTION FOR EMPLOYEE UPDATE HANDLER ");
    Statement s = con.createState ment();

    System.out.prin tln("GOT CONNECTION yaar");
    String sql = "UPDATE EMPLOYEE SET FNAME='"+ firstname +"', LNAME='"+ lastname +"', OFF_EMAIL='"+ lotusmailid +"',PRSNL_EMAIL ='"+ personalmailid +"',PPL_MANAGER ='"+ pplmanager +"',PROJECT_MAN AGER='"+ projectmanager +"',EXTN='"+ extnnum +"',PROJECT= '"+ project+"',TEAM _LEADER='"+ teamleader +"',HRID='"+ hrid +"',ATT_UID= '"+ attuid +"',UNIX_LOGIN= '"+ unixlogin+"',PO RT_NO='"+ portnum+"',MOBI LE_NO='"+ mobnum +"',HOME_NO= '"+ homenum +"',CONT_ADDR=' "+ address +"',CITY='"+ city +"',STATE='" + state +"',PINCODE= '"+ pincode +"' WHERE EMP_ID='"+ empid +"'" ;


    int i = s.executeUpdate (sql);
    System.out.prin tln("i value is :"+i);
    if (i!= 0)
    {
    String message = "Successful ly deleted"+i+" users.";
    System.out.prin tln(message);
    out.println(mes sage);
    }
    }
    catch (ClassNotFoundE xception e) {
    System.out.prin tln(e.toString( ));
    }
    catch (SQLException e) {
    e.printStackTra ce();
    }
    catch (Exception e) {
    System.out.prin tln(e.toString( ));
    }


    }
    It is a good idea to always close one's connections.
    So what happens when you run it? Is anything printed to the console?

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      I think u didn't Connection.comm it() after executeUpdate() .
      Do the commit then the reflection ll happen.
      Best of luck.

      Kind regards,
      Dmjpro.

      Comment

      • veerapureddy
        New Member
        • Jun 2007
        • 21

        #4
        Originally posted by r035198x
        It is a good idea to always close one's connections.
        So what happens when you run it? Is anything printed to the console?
        hai r035198x,

        Thanks for replying.

        ouput on console:

        GOT CONNECTION FOR EMPLOYEE UPDATE HANDLER
        GOT CONNECTION YAAR
        i value is:0

        i dont know , where i am doing the mistake?

        Comment

        • veerapureddy
          New Member
          • Jun 2007
          • 21

          #5
          Originally posted by dmjpro
          I think u didn't Connection.comm it() after executeUpdate() .
          Do the commit then the reflection ll happen.
          Best of luck.

          Kind regards,
          Dmjpro.
          hai dmjpro,

          Thanks for your reply to this post.

          i got the connection for databse.
          the output on console is:
          GOT CONNECTION FOR EMPLOYEE UPDATE HANDLER
          GOT CONNECTION YAAR
          i values is :0
          i dont have idea about commit() function.

          can u tell me , where i am doing the mistake.

          Thanks
          veerapureddy

          Comment

          • dmjpro
            Top Contributor
            • Jan 2007
            • 2476

            #6
            Originally posted by veerapureddy
            hai dmjpro,

            Thanks for your reply to this post.

            i got the connection for databse.
            the output on console is:
            GOT CONNECTION FOR EMPLOYEE UPDATE HANDLER
            GOT CONNECTION YAAR
            i values is :0
            i dont have idea about commit() function.

            can u tell me , where i am doing the mistake.

            Thanks
            veerapureddy
            Ok.
            Do u know the meaning of return value is 0?
            Ok, Let's explain.
            It means the number of updated rows is 0.
            Check out the where clause u used in UPDATE Query.
            Print the update query string into console and try to run in Oracle Client (SQL* or Toad or SQL Navigator or anything else u have).

            Now let's explain what is commit.
            When a database connection is opened then a database session starts.
            Now a change made in a session only changes reflected in that session.
            The changes is not reflected to the atabase untill u do a commit to the database.
            As u r in JAVA then call the commit like this .....

            [code=java]
            Connection l_con = null;
            //Code for connection making
            l_con.commit(); //The changes u did in this session reflects to the database.
            [/code]

            For details see the SUN API Documentation for Connection interface.
            Have a good day.

            Kind regards,
            Dmjpro.

            Comment

            • veerapureddy
              New Member
              • Jun 2007
              • 21

              #7
              Originally posted by dmjpro
              Ok.
              Do u know the meaning of return value is 0?
              Ok, Let's explain.
              It means the number of updated rows is 0.
              Check out the where clause u used in UPDATE Query.
              Print the update query string into console and try to run in Oracle Client (SQL* or Toad or SQL Navigator or anything else u have).

              Now let's explain what is commit.
              When a database connection is opened then a database session starts.
              Now a change made in a session only changes reflected in that session.
              The changes is not reflected to the atabase untill u do a commit to the database.
              As u r in JAVA then call the commit like this .....

              [code=java]
              Connection l_con = null;
              //Code for connection making
              l_con.commit(); //The changes u did in this session reflects to the database.
              [/code]

              For details see the SUN API Documentation for Connection interface.
              Have a good day.

              Kind regards,
              Dmjpro.
              Hai Dmjpro,

              Thanks a lot , now its working. After printing sql query on console, i am able to detect the false and i corrected it.the problem is , ididnt use trim function for empid in where clause(sql query), so its taking white spaces and not matching the data with database data.thanks for your valuable time

              Thanks&regards
              veerapureddy

              Comment

              • dmjpro
                Top Contributor
                • Jan 2007
                • 2476

                #8
                Originally posted by veerapureddy
                Hai Dmjpro,

                Thanks a lot , now its working. After printing sql query on console, i am able to detect the false and i corrected it.the problem is , ididnt use trim function for empid in where clause(sql query), so its taking white spaces and not matching the data with database data.thanks for your valuable time

                Thanks&regards
                veerapureddy
                U welcome.
                So u came to know how to use commit and why it is?
                Right?
                Have a good day.

                Kind regards,
                DMjpro.

                Comment

                • veerapureddy
                  New Member
                  • Jun 2007
                  • 21

                  #9
                  Originally posted by dmjpro
                  U welcome.
                  So u came to know how to use commit and why it is?
                  Right?
                  Have a good day.

                  Kind regards,
                  DMjpro.

                  Yes DMjpro, i understand the concept of commit function.

                  thank you very much

                  Comment

                  Working...