头一次用mysql,插入括号报错。如
sql = "insert into metainfo (textId, gkeywords, gdescription)" +
"values (" + textId + ",'" + gkeywords + "','" + gdescription + "')";
stmt.execute(sql);主要是由于gkeywords或gdescription里包含有单引号、括号等特殊字符。
例如,gkeywords为 “你好you'are(大家好)”,则异常为:
com.mysql.jdbc.exceptions.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 's??????????°? ??±?????°? ????????°?','????Tenon?§s??????????°??????????ù???ù±??ú' at line 1即插入语句里的变量里(如gkeywords或gdescription)有引号等特殊字符时会出错。
请问如何处理这种情况?PS:mysql的编码已经设置为gb2312了。
sql = "insert into metainfo (textId, gkeywords, gdescription)" +
"values (" + textId + ",'" + gkeywords + "','" + gdescription + "')";
stmt.execute(sql);主要是由于gkeywords或gdescription里包含有单引号、括号等特殊字符。
例如,gkeywords为 “你好you'are(大家好)”,则异常为:
com.mysql.jdbc.exceptions.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 's??????????°? ??±?????°? ????????°?','????Tenon?§s??????????°??????????ù???ù±??ú' at line 1即插入语句里的变量里(如gkeywords或gdescription)有引号等特殊字符时会出错。
请问如何处理这种情况?PS:mysql的编码已经设置为gb2312了。
这句话values前面少个空格
"values (" + textId + ",\'" + gkeywords + "\',\'" + gdescription + "\')";
values前面加了个空格还是一样的错误。
另外,由于涉及到大量的中文文本,在每一个单引号前加个\比较麻烦且耗时间。gkeywords、gdescription都是临时读取的。请问在java里还有什么其它的方法吗?
2、如果这些号码也要插入数据库,为了避免这种是可以用参数的方法加进去的~
string strsql = "insert into metainfo (textId, gkeywords, gdescription) values(?, ?, ?)";
preparedstatement pstmt = conn.preparestatement(strsql);
pstmt.setstring(1, textId);
pstmt.setstring(2, gkeywords);
pstmt.setstring(3, gdescription);
resultset rs = pstmt.execute();
这样就算有特殊符号也可以执行了!!!
"values (" + textId + ",'" + gkeywords + "','" + gdescription + "')";
stmt.execute(sql);