Solved

Failed update

Posted on 2004-09-09
11
224 Views
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();
        return(ret);
    }

The jsp:

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

<%
 if (session.getAttribute("logged")==null) {
            response.sendRedirect(response.encodeRedirectURL("login.jsp"));
  } 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) {
    nc.dbConnect();
    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!";
        }
    nc.dbDisconnect();
    %> <%= 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?

thanks!
0
Comment
Question by:k41d3n
  • 3
  • 2
  • 2
  • +3
11 Comments
 
LVL 7

Expert Comment

by:lhankins
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 :

   conn.commit();

When you're done with your update.

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

   c.setAutoCommit(true);

0
 
LVL 2

Author Comment

by:k41d3n
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.....
0
 
LVL 7

Expert Comment

by:lhankins
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.

0
 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
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.

0
 
LVL 2

Author Comment

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

And it's a local database.
0
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 
LVL 11

Expert Comment

by:sompol_kiatkamolchai
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
0
 
LVL 19

Expert Comment

by:Kuldeepchaturvedi
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?
0
 
LVL 21

Expert Comment

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

The update statement you are using is not standard SQL
0
 
LVL 2

Author Comment

by:k41d3n
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.
0
 

Accepted Solution

by:
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
0

Featured Post

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

In  today’s increasingly digital world, managed service providers (MSPs) fight for their customers’ attention, looking for ways to make them stay and purchase more services. One way to encourage that behavior is to develop a dependable brand of prod…
What is Backup? Backup software creates one or more copies of the data on your digital devices in case your original data is lost or damaged. Different backup solutions protect different kinds of data and different combinations of devices. For e…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

708 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now