Results 1 to 13 of 13
  1. #1
    Join Date
    Jun 2010
    Posts
    17
    Rep Power
    0

    Default Table is not updated ,java script is fine

    i have a following java script in which i want to change username of student in the table ,everything is fine in good even ps.executeUpdate() is working ,but when i go to table and write code select * from tablename,still the previous username is there ,
    The code is
    if((!t1.getText().equals("")) && (!t2.getText().equals("")) && (!t3.getText().equals("")) && (!t4.getText().equals("")))
    {
    try {
    enteruser = t4.getText();
    enterpass = t1.getText();
    newuser = t2.getText();
    confirm = t3.getText();
    PreparedStatement ps3 = con.prepareStatement("select * from sreg where username=?");
    ps3.setString(1, enteruser);
    ResultSet rs1 = ps3.executeQuery();
    rs1.next();
    getuser = rs1.getString(4);
    getuser=getuser.trim();
    getpass = rs1.getString(12);
    getpass=getpass.trim();
    rs1.close();
    ps3.close();
    System.out.println(getuser);
    System.out.println(getpass);

    if (enteruser.equals(getuser)) {
    if (getpass.equals(enterpass)) {
    PreparedStatement ps4 = con.prepareStatement("update sreg set username=? where username=?");
    ps4.setString(1, newuser);
    ps4.setString(2, enterpass);
    System.out.println("Before");
    ps4.executeUpdate();
    System.out.println("After");
    JOptionPane.showMessageDialog(this, "Successfully Updated");
    } else {
    JOptionPane.showMessageDialog(this, "Please Enter Correct Current Password");
    }
    } else {
    JOptionPane.showMessageDialog(this, "Please Enter Correct Current Username");
    }
    } catch (SQLException ex) {
    Logger.getLogger(password.class.getName()).log(Lev el.SEVERE, null, ex);
    }
    }

    else
    {
    JOptionPane.showMessageDialog(this,"Please Enter All the Filelds");
    }




    Note: System.out.println("Before");
    System.out.println("After"); are working ,means ps4.executeUpdate(); is working,,



    Somebody Please help to resolve it

    Thanks
    Last edited by amritpalpathak; 07-17-2010 at 04:25 PM.

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Properly close your PreparedStatment (ps4), your Connection(s) and entire database at the end.

    kind regards,

    Jos

  3. #3
    Join Date
    Jun 2010
    Posts
    17
    Rep Power
    0

    Default

    I have simple close prepared statement as
    ps4.close();
    still error is there

    @jos ,the messsage "Successfully Updated " is coming but when i go to table and check username ,still previous username is there

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by amritpalpathak View Post
    I have simple close prepared statement as
    ps4.close();
    still error is there

    @jos ,the messsage "Successfully Updated " is coming but when i go to table and check username ,still previous username is there
    Java Code:
    if (enteruser.equals(getuser)) {
    if (getpass.equals(enterpass)) {
    PreparedStatement ps4 = con.prepareStatement("update sreg set username=? where username=?");
    ps4.setString(1, newuser);
    ps4.setString(2, enterpass);
    I think your update statement is incorrect and I suspect you're filling in password for an old username).

    kind regards,

    Jos

  5. #5
    Join Date
    Jun 2010
    Posts
    17
    Rep Power
    0

    Default

    what do u mean by

    "I think your update statement is incorrect and I suspect you're filling in password for an old username)."

    I have a table sreg with column username at the back end,and in my code i just trying to update it from user,

    You can see there is no error in preparedstatement (Update);


    if preparedstatement is false,then why the message"Successfully updated" is coming, it means the prepared statement executed well thats why message is coming,,

    Regards
    Amrit pal pathak

  6. #6
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by amritpalpathak View Post
    what do u mean by

    "I think your update statement is incorrect and I suspect you're filling in password for an old username)."

    I have a table sreg with column username at the back end,and in my code i just trying to update it from user,

    You can see there is no error in preparedstatement (Update);


    if preparedstatement is false,then why the message"Successfully updated" is coming, it means the prepared statement executed well thats why message is coming,,

    Regards
    Amrit pal pathak
    Check your update statement: you want to update something where the existing username == enterpass. That doesn't make sense semantically; of course it isn't a syntax error and the update succeeds (by doing nothing probably), but no user is updated; in simple terms: your parameter values to the PreparedStatement are incorrect, i.e. username != password.

    kind regards,

    Jos

  7. #7
    Join Date
    Jun 2010
    Posts
    17
    Rep Power
    0

    Default

    You was right josAh, i was passing (enterpass) instead to (enteruser),thats the mistake
    ps4.setString(2, enterpass); /* This was wrong
    ps4.setString(2, enteruser); /*This is write


    Thanks a lot Jos
    Last edited by amritpalpathak; 07-18-2010 at 11:27 AM.

  8. #8
    Join Date
    Jun 2010
    Posts
    17
    Rep Power
    0

    Default

    Now help me to solve other problem in this script
    check out the following Statement from below,

    PreparedStatement ps3 = con.prepareStatement("select * from sreg where username=?");

    This is the modified form of above code


    Problem is that,If i pass correct username(that already exist in database ),script is going well, rs1 and retrieve the column from table with respect to enter username that match with the username exist in backend and finally password is updated susssfully

    Now the thing is that, if i pass wrong username(that is not exist in table ) then the error comes
    "Invalid cusror state" ... I have to Print a message(JOptionPane.showMessageDialog(this, "Please Enter Correc username") when user enter a wrong username

    what should be the code(condition) to print the message if entered username is wrong

    The Code is


    if((!t1.getText().equals("")) && (!t2.getText().equals("")) && (!t3.getText().equals("")) && (!t4.getText().equals("")))
    {
    try {
    enteruser = t4.getText();
    enterpass = t1.getText();
    newpass = t2.getText();
    confirm = t3.getText();
    PreparedStatement ps3 = con.prepareStatement("select * from sreg where username=?");
    ps3.setString(1, enteruser);
    ResultSet rs1 = ps3.executeQuery();
    rs1.next();
    getuser = rs1.getString(4);
    getuser=getuser.trim();
    getpass = rs1.getString(12);
    getpass=getpass.trim();
    rs1.close();
    ps3.close();




    if (enteruser.equals(getuser)) {
    System.out.println(enterpass);


    if (enterpass.equals(getpass)) {
    if(newpass.equals(confirm))

    {

    PreparedStatement ps4 = con.prepareStatement("update sreg set choosepassword=?,repeatpassword=? where username=?");

    ps4.setString(3, enteruser);
    ps4.setString(1, newpass);
    ps4.setString(2, confirm);

    ps4.executeUpdate();

    JOptionPane.showMessageDialog(this, "Successfully Updated");
    }
    else
    {
    JOptionPane.showMessageDialog(this,"Please Enter Same Password Again To Confirm");
    }
    }
    else {
    JOptionPane.showMessageDialog(this, "Please Enter Correct Current Password");
    }
    } else {
    JOptionPane.showMessageDialog(this, "Please Enter Correct Username");
    }
    } catch (SQLException ex) {
    Logger.getLogger(password.class.getName()).log(Lev el.SEVERE, null, ex);
    }
    }
    else
    {
    JOptionPane.showMessageDialog(this,"Please Enter All Fields");
    }



    Thanks

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by amritpalpathak View Post
    You was write josAh, i was passing (enterpass) instead to (enteruser),thats the mistake
    ps4.setString(2, enterpass); /* This was wrong
    ps4.setString(2, enteruser); /*This is write


    Thanks a lot Jos
    You're welcome; it can be hard to scrutinize your own code and spot the bug; when that happens to me I move away from the code and look again a half an hour later or so; most of the time I can see my mistake immediately.

    <pedantic mode>
    Of course I was right, I'm always right ... except when I'm wrong but those occasions don't count ;-)
    </pedantic mode>

    kind regards,

    Jos

  10. #10
    Join Date
    Jun 2010
    Posts
    17
    Rep Power
    0

    Default

    please reply for above code?


    i mean ,can we search the entered username within table(whole fields) so that if it match ,i will be the solution to my problem,further i can proceed ..

    how to search a table for values enter by user?
    what will be code?

    Help
    Last edited by amritpalpathak; 07-18-2010 at 11:48 AM.

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by amritpalpathak View Post
    please reply for above code?


    i mean ,can we search the entered username within table(whole fields) so that if it match ,i will be the solution to my problem,further i can proceed ..

    how to search a table for values enter by user?
    what will be code?

    Help
    You could've figured that out yourself by reading the API documentation for the ResultSet interface. If you query for a non-existing user (the user name is not in your table) the ResultSet will be empty, so your first resultset.next() call will return false, indicating that there is no 'next' row in the empty resultset.

    kind regards,

    Jos

  12. #12
    Join Date
    Jun 2010
    Posts
    17
    Rep Power
    0

    Default

    i hope u didnt get my mean
    when user enter a existing username suppose he/she enter it wrong ..Now the whole have no meaning because it fails in its first condition (false username)
    i have a table name "sreg" with column of name "username"
    now what i want,,, the entered username by user should be first search with all the entries in column "username", if it match,then we have to detect it in someway(e.g. we can update a variable like a++,, or can make a flag true or false), this is simple, but tell me how to make a search on table or column with value enter by user from front end... i hope u will get my mean now..




    Thanks

  13. #13
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,651
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by amritpalpathak View Post
    i hope u didnt get my mean
    when user enter a existing username suppose he/she enter it wrong ..Now the whole have no meaning because it fails in its first condition (false username)
    i have a table name "sreg" with column of name "username"
    now what i want,,, the entered username by user should be first search with all the entries in column "username", if it match,then we have to detect it in someway(e.g. we can update a variable like a++,, or can make a flag true or false), this is simple, but tell me how to make a search on table or column with value enter by user from front end... i hope u will get my mean now..
    You should make the column 'username' a primary key (unique) in your database so you'll have only one row per existing user.

    kind regards,

    Jos

Similar Threads

  1. Replies: 1
    Last Post: 01-27-2010, 09:20 AM
  2. java script
    By satti in forum Web Frameworks
    Replies: 0
    Last Post: 06-09-2009, 08:53 AM
  3. Replies: 2
    Last Post: 03-16-2009, 07:52 PM
  4. application is not running please fine the solution for it.
    By jagadeeshchinni in forum New To Java
    Replies: 4
    Last Post: 09-28-2008, 10:14 AM
  5. Errors driving me crazy! although compiles fine
    By irishsea2828 in forum New To Java
    Replies: 1
    Last Post: 04-08-2008, 03:23 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •