如何使用JDBC在MySQL DB中保存html文件?

use*_*521 2 java mysql sql jdbc

我有html文件String,我想使用更新查询将其插入MySQL数据库.我试过这个:

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

但我得到以下例外:

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
Run Code Online (Sandbox Code Playgroud)

这是在HTML内部 - 可能我只是引用html ""并插入它,因为它包含许多特殊字符和引号也 - 所以我如何插入它?我应该以某种方式编码吗?

Per*_*ror 7

我建议你使用PreparedStatement而不是Statement.

String query = "UPDATE orders SET status=?, html=? WHERE id=?";
PreparedStatement stmnt = conn.PreparedStatement(query);
stmnt.setString(1, yourtext);
....
int num = st.executeUpdate();
Run Code Online (Sandbox Code Playgroud)