Hi

I am running an jsp application thru which the values are saved in mysql. all values are saved twice.Pls help me how to avoid that

Dani AI

Generated

Cross-posting (as pointed out) is a separate etiquette issue and does not cause duplicate rows. The usual technical causes for “values saved twice” are that the same INSERT runs twice: a browser POST is resubmitted on refresh (or double-click), client-side code triggers two submits, or server-side logic executes the insert more than once (for example, a JSP with DB code being included/forwarded or an insert called twice inside a loop).

Quick diagnostic steps and fixes:

  • Add a short, timestamped server log immediately before the INSERT to tell whether one request produced two inserts or two requests hit the server (include thread id and key params).
  • Check the webserver access logs for duplicate POSTs.
  • Ensure the HTML has a single submit path (avoid both onclick and onsubmit handlers that submit).
  • Use Post/Redirect/Get so a refresh won’t re-run the INSERT. Example server-side redirect after processing:
    // after successful insert in a servlet
    response.sendRedirect("success.jsp");
  • Prevent double-clicks by disabling the submit control on submit:
    <form method="post" onsubmit="this.submitBtn.disabled=true;">
    <input type="submit" name="submitBtn" value="Send"/>
    </form>
  • Keep the insert atomic and executed once; use try-with-resources and a single executeUpdate() call:
    try (Connection conn = ds.getConnection();
       PreparedStatement ps = conn.prepareStatement("INSERT INTO users(name,email) VALUES(?,?)")) {
    ps.setString(1, name);
    ps.setString(2, email);
    ps.executeUpdate();
    }

Defensive DB measure:

  • Add a unique constraint on the columns that define a duplicate so the schema prevents accidental duplicates:
    ALTER TABLE users ADD CONSTRAINT uniq_user UNIQUE (name, email);

Best practice: move DB code out of JSPs into a servlet/DAO, log the insert call, apply PRG, and add a DB uniqueness constraint. These steps will reveal whether the problem is client-side, server-side, or purely data integrity related, and will stop repeat inserts while the root cause is fixed.

Recommended Answers

All 3 Replies

FYI Also posted here.

@OP If you are going to cross-post, please at least have the decency to say that and post the links to all in all of the threads, as well as providing summarys of what has happened from time to time. No one likes to waste their time making suggestions others have already made. You should be wasting yours keeping everybody else abreast of what you've already been advised. It's only common decency.

Now, add links to any other cross-post you may have, please.

Actually i thought that daniweb and java forums r 2 different forums. so i posted my query in both so that i can get reply in any of these two. and i know the decency. pls dont teach me.

Actually i thought that daniweb and java forums r 2 different forums. so i posted my query in both so that i can get reply in any of these two. and i know the decency. pls dont teach me.

They are two different forums, but how does that change things? It might make it harder for anyone to know that they have wasted their time, but they will still have wasted it. So maybe you don't "know the decency" and I do need to teach you.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.