Java在mysql中插入含有单引号的内容报错

使用java向mysql数据表中插入数据,若是String中含有单引号,则会出现以下报错html

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax;java

因此,单引号是须要转义的。mysql

解决方法以下:sql

//在java里可使用preparedstatement,你上面的状况能够用下面的方式:
    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();
//这样就算有特殊符号也能够执行了!!!

参考资料:debug

http://www.debugease.com/mysql/279755.htmlrest

http://stackoverflow.com/questions/15198580/how-to-escape-single-quote-in-string-literal-using-mysql-from-javacode

相关文章
相关标签/搜索