2

I have html file as String and I want to insert it into MySQL DB, using update query. I tried this:

Statement st = connection.createStatement();
String query = "UPDATE orders SET status='ready', html='"+html+"' WHERE id='123'";
int num = st.executeUpdate(query);

But I get following exception:

MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'zglosBladWindow').ck_window.showCenter('this');" href="#">zg?o? b??d na stronie<' at line 1

This is somwhere inside HTML - probably I cant just quote html with "" and insert it as it contains many special characters and quotes also - so how I can insert it? Should I encode it somehow?

1
  • You'll have to escape the variable html. Commented Jan 17, 2013 at 11:24

4 Answers 4

7

I'd advice you to use PreparedStatement rather than Statement.

String query = "UPDATE orders SET status=?, html=? WHERE id=?";
PreparedStatement stmnt = conn.PreparedStatement(query);
stmnt.setString(1, yourtext);
....
int num = st.executeUpdate();
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, this is what I was looking for but you have little mistake in your code: instead of int num = st.executeUpdate(query); there should be int num = st.executeUpdate();
@user606521 you are welcome, and thanks for pointing it out, it was basically copy/paste thingy .. :)
0

You should use PreparedStatement construct, as your HTML string may contain quotes or double qoutes. Read more here

Comments

0

You should use PreparedStatement, but I'd rather save the path in MySQL instead of saving the file.

Comments

0

You should probably use something like this,

StringBuilder contentBuilder = new StringBuilder();
try {
BufferedReader in = new BufferedReader(new FileReader("mypage.html"));
String str;
while ((str = in.readLine()) != null) {
    contentBuilder.append(str);
}
in.close();
} catch (IOException e) {
}
String content = contentBuilder.toString();

Comments

Your Answer

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.