我有 String
格式的 html 文件,我想使用更新查询将它插入 MySQL 数据库。我试过这个:
Statement st = connection.createStatement();
String query = "UPDATE orders SET status='ready', html='"+html+"' WHERE id='123'";
int num = st.executeUpdate(query);
但我得到以下异常:
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
这是 HTML 内部的某个地方 - 可能我不能只用 ""
引用 html 并插入它,因为它也包含许多特殊字符和引号 - 那么我如何插入它?我应该以某种方式对其进行编码吗?
最佳答案
我建议您使用 PreparedStatement而不是声明。
String query = "UPDATE orders SET status=?, html=? WHERE id=?";
PreparedStatement stmnt = conn.PreparedStatement(query);
stmnt.setString(1, yourtext);
....
int num = st.executeUpdate();
关于java - 如何使用 JDBC 在 MySQL 数据库中保存 html 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14377945/