1

I'm getting this exception

java.sql.SQLException: Unknown column 'auyu' in 'where clause'

My Query and method in my database facade class.

db.save("delete from users where name = auyu");
public static void save(String sql) throws Exception {
 new DBFacade().connect();
 synchronized (c) {
 c.createStatement().executeUpdate(sql);
 }
}
skaffman
405k96 gold badges825 silver badges775 bronze badges
asked Aug 18, 2009 at 18:44
0

3 Answers 3

6

I suspect you meant:

delete from users where name = 'auyu'

This is still a pretty odd SQL command to give to a "save" method.

I'd also strongly suggest that you use parameterised SQL statements instead of embedding data directly into the SQL itself - particularly if the data has come from the user.

answered Aug 18, 2009 at 18:46
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks what if I need to use a String like this: String s = "auyu";
@Rocky: Use a preparedStatement (or string concatenation); see my answer for details on the PS option.
2

+1 to Jon Skeet's answer. Expanding and perhaps going OT, but it's best to parameterize these things and ensure escaping so that you aren't susceptible to SQL-injection attacks. E.g.:

public static void deleteUser(userName)
throws Exception
{
 PreparedStatement ps;
 new DBFacade().connect();
 // (Assuming 'c' is a connection that's in scope somehow)
 synchronized (c) {
 // (You'd want to cache the prepared statement in an appropriate
 // way related to how you're handling connections and pooling)
 ps = c.prepareStatement("delete from users where name = ?");
 ps.setString(1, userName);
 ps.executeUpdate();
 }
}

Otherwise, if a user provides a name like "anyu'; drop table users;", you could be for it.

answered Aug 18, 2009 at 18:54

Comments

2

You need single quotes around the auya ('auyu') and you'll need to escape them like so:

"delete from users where name = \'auyu\'"
answered Aug 18, 2009 at 18:48

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.