我正在使用 - 从 java 文件向 Mysql 数据库中插入值
String query = "INSERT INTO genes (sent, title) VALUES ('"+sent+"','"+title+"')";
Statement stmt = con.createStatement();
int rs = stmt.executeUpdate(query);
其中sent
和title
是应用某种算法后提取的可变字符串。但是,当 sent
或 title
包含单个 qoutes 时,这会产生 sql 错误。
最佳答案
考虑使用带参数的预处理语句:
PreparedStatement pstmt = con.prepareStatement(
"INSERT INTO genes (sent, title) VALUES (?, ?)");
pstmt.setString(1, sent);
pstmt.setString(2, title);
pstmt.executeUpdate();
关于java - 由于插入查询中的单引号导致的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11322629/