Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Failed update

Posted on 2004-09-09
Medium Priority
Last Modified: 2010-04-01
Is there any reason that this isn't working?

The call:

    public int updateRecord(String id, String slug, String username, String content) throws Exception {
        String query = "UPDATE news SET (slug, username, content, timestamp) VALUES (?, ?, ?, ?) WHERE slug = ?";
        stmt = conn.prepareStatement(query);
        java.util.Date now = new java.util.Date();
        stmt.setString( 1, slug);
        stmt.setString( 2, username);
        stmt.setString( 3, content);
        stmt.setTimestamp( 4, new java.sql.Timestamp( now.getTime() ) );
        stmt.setString( 5, id);
        int ret = stmt.executeUpdate();

The jsp:

<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@page import="java.sql.*" %>
<%@page import="news.*" %>

 if (session.getAttribute("logged")==null) {
  } else {
      String user = (String)session.getAttribute("logged");

<% NewsConnect nc = new NewsConnect();%>
String id = request.getParameter("id");
String slug = request.getParameter("slug");
String content = request.getParameter("content");
String contact = request.getParameter("username");
String submit = request.getParameter("submit");
String msg = "";
if (submit != null) {
    if ((nc.updateRecord(id, slug, contact, content)) > 0) {
        msg = "Your record for '"+ slug + "' has been changed!! <a href=records.jsp>Go back</a>";
        } else {
        msg = "Failed to update the record!";
    %> <%= msg %> <%

The only thing I can think of, is that I am not including every field in my update, just the ones I want to update. Though I don't know why this would be an issue.

Or, the field id in the database is an auto increment int and when I use string to reference that record for update it fails, I tried to int id = Integer.parseInt() but I got the same results. A blank page after I click submit and the record unchanged.

Any idea?

Question by:k41d3n
  • 3
  • 2
  • 2
  • +3

Expert Comment

ID: 12022181
On your WHERE clause, do you really mean to have "WHERE slug = ?" (seems like this should be "WHERE id = ?")

>   String query = "UPDATE news SET (slug, username, content, timestamp) VALUES (?, ?, ?, ?) WHERE slug = ?";

Also - Can't tell from what you've posted, but what is the autocommit set to for the JDBC Connection...?   If you have autocommit set to false, then your update won't get applied until you issue a commit (this is how you support transactions with JDBC).

If you need autocommit set to false, just be sure and call :


When you're done with your update.

If you want to change the value of the autocommit for the current connection, just do :



Author Comment

ID: 12022225
First, you are right, I forgot to switch the operator back to id from slug.

neither work.

and I can't see needing to use autocommit, as I can update and add records with the same function on another table.

which leads me to believe it's because of the id.....

Expert Comment

ID: 12022437
One thing which might help debug the problem is to switch your prepared statement to a normal SQL statement (all strings) and then do a System.out.println on it just before executing.

Then copy the output of your debug statement into your favorite SQL editor and see it works from there.   If it doesn't work in the SQL editor, you'll have an easier time debugging there.

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

LVL 11

Expert Comment

ID: 12022816
How do you know that database is not updated?
Is there a cache on web browser?
To make sure, you should check it by using SQL edit to make a query to check whether you update operatio success or not.


Author Comment

ID: 12023385
yeah, I know it isn't updating because I check the database physically.

And it's a local database.
LVL 11

Expert Comment

ID: 12023540
Have you tested simple update statement like this in SQL editor yet?

UPDATE news SET (slug, username, content, timestamp) VALUES ("some slug", "some name", "some content", "some timestamp") WHERE id = 123
LVL 19

Expert Comment

ID: 12023682
Are you seeing any exception coming out? I think your time stamp is doing something funny...
what is the data type of timestamp field?
LVL 21

Expert Comment

ID: 12025572
Try this statement:
UPDATE news SET slug=?, username=?, content=?, timestamp=?
WHERE id = ?

The update statement you are using is not standard SQL

Author Comment

ID: 12026769
I'm a bone head.

In my form I had the id field as hidden, but I left out the name="id" so the id was never getting passed to the edit action.

I figured it out by printing the variables with the error message.

Accepted Solution

ee_ai_construct earned 0 total points
ID: 12064310
Please read entire thread.  Asker solved or content important.
Closed, 500 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Moderator

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Use this step by step method when setting up QuickBooks Online. They will allow you to explore the various features of the advanced settings available to you.
How do you create a user-centered user experience on your website? And what are some things you should consider in the process?
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
Suggested Courses

581 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question