Java Prepared statement not executing

后端 未结 4 510
佛祖请我去吃肉
佛祖请我去吃肉 2021-01-24 02:46

I have created a small 3 tier program, consisting of : front end -> servlet -> database.

Front end I enter some details into a form. They are passed to a servlet, which

相关标签:
4条回答
  • 2021-01-24 03:14

    You should invoke the method executeUpdate() on the statement object.

    Also, I don't see any call to commit the data, any transaction handling. It's fine if you skipped that piece of code for the purpose of this question; otherwise it's quite an important step ( commit if all goes well, rollback for exception scenarios)

    0 讨论(0)
  • 2021-01-24 03:17

    Use executeUpdate for database write operations:

    preparedStmt.executeUpdate();
    
    0 讨论(0)
  • 2021-01-24 03:26

    Answer: The database ID was not set to auto increment. For some reason this does not allow you to then insert data to table. Thanks to ChadNC for pointing this out.

    0 讨论(0)
  • 2021-01-24 03:28

    Also, why st = con.createStatement();?

    And why do you have a leading space in your query?

    String query = " INSERT INTO user_information (name, email, country, password)" 
                     + " VALUES (?, ?, ?, ?)";
    

    This leading space may or may not matter...

    Lastly, you should be closing your connection when you're through with it, using try-with-resources or a finally block.

    0 讨论(0)
提交回复
热议问题