Failed update

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?

Who is Participating?
ee_ai_constructConnect With a Mentor Commented:
Please read entire thread.  Asker solved or content important.
Closed, 500 points refunded.
ee_ai_construct (replacement part #xm34)
Community Support Moderator
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 :


k41d3nAuthor Commented:
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.....
Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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.

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.

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

And it's a local database.
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
Are you seeing any exception coming out? I think your time stamp is doing something funny...
what is the data type of timestamp field?
Try this statement:
UPDATE news SET slug=?, username=?, content=?, timestamp=?
WHERE id = ?

The update statement you are using is not standard SQL
k41d3nAuthor Commented:
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.
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.