Results 1 to 6 of 6
  1. #1
    Mikey is offline Member
    Join Date
    Mar 2011
    Posts
    15
    Rep Power
    0

    Question SQL Update with ? as values

    Hi, I am trying to get the password collumn in my database to update with a new password from which the user will enter into a Java GUI. The Java side is working fine, however my SQL is complaining saying that 'ERROR: null value in column "password" violates not-null constraint'. Even though I am actually specifying a value. Here is the UPDATE statement:

    public static void updatePassword(String password, int userID) {

    Connection connection = connectionDB.createConnection();
    try {
    PreparedStatement stmt = connection.prepareStatement("UPDATE login "
    + "SET password = ?"
    + "WHERE login_id = ?");

    stmt.setString(1, password);
    stmt.setInt(2,userID);
    stmt.execute();


    } catch (Exception e) {
    System.out.println(e.getMessage());
    }
    System.out.println("Password" +password);
    System.out.println("UserID"+ userID); // It is finding the correct userID but not updating the password for that user!
    }

    If anyone can help, thats great :)

  2. #2
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default

    Replace this
    Java Code:
    PreparedStatement stmt = connection.prepareStatement("UPDATE login "
    + "SET password = ?"
    + "WHERE login_id = ?");
    
    stmt.setString(1, password);
    stmt.setInt(2,userID);
    stmt.execute();
    by
    Java Code:
    PreparedStatement stmt = connection.prepareStatement("UPDATE login SET password = ' +"password"+ ' WHERE login_id = ' +"userID"+ ');
    stmt.execute();
    Don't Forget to try yourself before asking others help.....
    Press REP, if you find their advices/solutions effective.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,189
    Rep Power
    19

    Default

    Quote Originally Posted by UJJAL DHAR View Post
    Replace this
    Java Code:
    PreparedStatement stmt = connection.prepareStatement("UPDATE login "
    + "SET password = ?"
    + "WHERE login_id = ?");
    
    stmt.setString(1, password);
    stmt.setInt(2,userID);
    stmt.execute();
    by
    Java Code:
    PreparedStatement stmt = connection.prepareStatement("UPDATE login SET password = ' +"password"+ ' WHERE login_id = ' +"userID"+ ');
    stmt.execute();
    No, don't. That's susceptible to SQL injection.

    db

  4. #4
    UJJAL DHAR is offline Senior Member
    Join Date
    Apr 2010
    Location
    Dhaka,Bangladesh
    Posts
    180
    Rep Power
    0

    Default

    Quote Originally Posted by Darryl.Burke View Post
    No, don't. That's susceptible to SQL injection.

    db
    It will work.Then what is the problem? Can you explain?
    And please give the solution.
    Last edited by UJJAL DHAR; 04-02-2011 at 03:18 PM.
    Don't Forget to try yourself before asking others help.....
    Press REP, if you find their advices/solutions effective.

  5. #5
    Mikey is offline Member
    Join Date
    Mar 2011
    Posts
    15
    Rep Power
    0

    Default

    If thats subject to SQL Injection then I can't really use it. Thanks for the suggestion though. Any other idea's? Thanks

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default

    Quote Originally Posted by UJJAL DHAR View Post
    It will work.Then what is the problem? Can you explain?
    And please give the solution.
    Are you familiar with the SQL injection issue? If not, you should understand it first before giving SQL advice, and you can find all about it on Google and in fact in Wikipedia.

Similar Threads

  1. Replies: 14
    Last Post: 01-22-2011, 02:36 PM
  2. How to update JTextArea with JSlider Values
    By theen3my in forum AWT / Swing
    Replies: 7
    Last Post: 10-04-2009, 08:48 AM
  3. Replies: 0
    Last Post: 05-22-2008, 10:53 AM
  4. Replies: 4
    Last Post: 04-15-2008, 01:04 PM
  5. unable to update MYSQL with values from jsp page
    By koushika in forum JavaServer Pages (JSP) and JSTL
    Replies: 0
    Last Post: 03-27-2008, 01:35 AM

Tags for this Thread

Posting Permissions

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