weird problem with JSP and postgresql

I'm running postgresql 8.1.3 on tomcat 5.5.7, and using the postgresql-8.1-404.jdbc3.jar JDBC driver, and I'm having a very weird exception that pretty much doesn't explain anything, and so I was hoping someone could help me with it.

I have certain values stored in a bean, and then I'm uploading a picture from my PC to the server and storing info in the database. The code is the following:

103: Connection con=DriverManager.getConnection("jdbc:postgresql://localhost:5432/db","user", "pass");

104: Statement st=con.createStatement();

105:

106: int rs = st.executeUpdate("INSERT INTO news (news_title, news_body, news_author_name, image1) values ('"+title+"', '"+body+"', '"+author+"', '"+filename+"')");

107:

108:

109: if (rs > 0)

And the exception is the following:

org.postgresql.util.PSQLException: ERROR: syntax error at or near "s"

This occurs at line 106.

Does anyone have any idea what the problem is and how to fix it?

Thank you.

[1040 byte] By [Hashimotoa] at [2007-10-2 20:27:27]
# 1

Ok, I discovered what the problem was, but it creates another problem for me.

Apparently, if the title or the body of the text contain a single quote, I will get a similar exception. This occured because there were several occurances of "aircraft's pilot", "plane's cockpit", etc...

When I deleted the single quotes, everything worked fine.

I guess I will have to use the String.replace method to escape all the characters. I hope that works.

Message was edited by:

Hashimoto

Hashimotoa at 2007-7-13 23:10:21 > top of Java-index,Enterprise & Remote Computing,Web Tier APIs...