尝试插入包含'的值时出现MySQLSyntaxErrorException

时间:2016-04-24 22:58:14

标签: java mysql jdbc jsoup

我知道MySQLSyntaxErrorException错误已经有无数条目,但是我还没有阅读任何解决了我的问题的帖子,我真的可以使用额外的一组眼睛试图指出我哪里出错了

我正在创建一个简单的Web scraper,它将维基百科页面上列出的啤酒厂名称存储到使用WAMP本地存储的基本MySQL表中。我的现有代码似乎正常工作,直到我遇到名称中包含'的啤酒厂名称。这也是我第一次使用JSoup进行HTML解析。

这是我目前的代码:

import java.io.IOException;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

public class Main {
public static DB db = new DB();

public static void main(String[] args) throws SQLException, IOException {
    db.runSql2("TRUNCATE Brewery;");
    processPage("myBrew");
    System.out.println("done parsing");
}


// recursive method to find brewery names by adding db entry for all <li><a>
// values on site. Might not need recursion for this.
public static void processPage(String bName) throws SQLException,
        IOException {
    // check if the given URL is already in database
    String sql = "select * from Brewery where name = '" + bName + "'";
    ResultSet rs = db.runSql(sql);
    if (rs.next()) {
        // do nothing because already exists
    } else {
        // store the brewery to database to avoid parsing again
        sql = "INSERT INTO  `Crawler`.`Brewery` " + "(`name`) VALUES "
                + "(?);";
        PreparedStatement stmt = db.conn.prepareStatement(sql, Statement.RETURN_GENERATED_KEYS);
        stmt.setString(1, bName);
        //stmt.execute();
        stmt.executeUpdate();

        // get useful information
        //research this further to better understand what it's doing
        Document doc = Jsoup.connect("https://en.wikipedia.org/wiki/List_of_microbreweries").get();

        //case-senstive
        if (doc.text().contains("Brewery")) {
            System.out.println(bName);
        }

        // get all links and recursively call the processPage method
        Elements breweries = doc.select("a[href]");
        for (Element link : breweries) {
            //System.out.println("element class: " + link.nodeName());
            //System.out.println("parent class: " + link.parent().nodeName());
            System.out.println("element title: " + link.attr("title"));

            //assumes that all brewery names will be listed in <li><a> html format
            if (link.nodeName() == "a" && link.parent().nodeName() == "li") {
                System.out.println("recursive call tripped");
                String tmp = link.attr("title");
                //String first = tmp.charAt(0) + "";
                if(tmp.contains("'")){
                    String brew = tmp.replaceAll("'", "\\\'");
                    /*System.out.println("new string: '" + brew +"'");
                    processPage("'" + brew + "'");*/
                    System.out.println("new string: " + brew);
                    processPage(brew);
                }
                else {
                    if (tmp.contains(" (page does not exist)")) {
                        String brew = tmp.replaceAll(" (page does not exist)", "");
                        System.out.println("shortened string: " + brew);
                        processPage(brew);
                    }
                    else {
                        //no ' exists in the name
                        processPage(tmp);
                    }
                }

            }
        }
    }
}
}

使用此语法时,出现以下错误:

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.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 All Natural Brewing Company'' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.Util.getInstance(Util.java:387)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:939)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2478)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2625)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2547)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2505)
at com.mysql.jdbc.StatementImpl.executeQuery(StatementImpl.java:1370)
at DB.runSql(DB.java:26)
at Main.processPage(Main.java:57)
at Main.processPage(Main.java:95)
at Main.processPage(Main.java:105)
at Main.processPage(Main.java:105)
at Main.processPage(Main.java:101)
at Main.processPage(Main.java:105)
at Main.processPage(Main.java:101)
at Main.processPage(Main.java:101)
at Main.processPage(Main.java:105)
at Main.processPage(Main.java:105)
at Main.processPage(Main.java:105)
at Main.processPage(Main.java:105)
at Main.processPage(Main.java:105)
at Main.processPage(Main.java:105)
at Main.processPage(Main.java:105)
at Main.processPage(Main.java:105)
at Main.processPage(Main.java:105)
at Main.processPage(Main.java:101)
at Main.processPage(Main.java:105)
at Main.processPage(Main.java:105)
at Main.processPage(Main.java:105)
at Main.processPage(Main.java:101)
at Main.processPage(Main.java:105)
at Main.processPage(Main.java:105)
at Main.main(Main.java:17)

使用此代码,包含'的啤酒厂名称不会存储在我的MySQL表中。但是,如果我替换以下行

String brew = tmp.replaceAll("'", "\\\'");

String brew = tmp.replaceAll("'", "\\\\'");

然后将啤酒厂名称存储到我的MySQL表中,并在下一个啤酒厂名称被删除之前得到以下错误:

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLIntegrityConstraintViolationException: Duplicate entry 'Beau\'s All Natural Brewing Company' for key 'brewery_name'
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
at java.lang.reflect.Constructor.newInstance(Unknown Source)
at com.mysql.jdbc.Util.handleNewInstance(Util.java:404)
at com.mysql.jdbc.Util.getInstance(Util.java:387)
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:932)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2478)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2625)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2551)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2073)
at com.mysql.jdbc.PreparedStatement.executeUpdateInternal(PreparedStatement.java:2009)
at com.mysql.jdbc.PreparedStatement.executeLargeUpdate(PreparedStatement.java:5094)
at com.mysql.jdbc.PreparedStatement.executeUpdate(PreparedStatement.java:1994)
at Main.processPage(Main.java:67)
at Main.processPage(Main.java:95)
at Main.processPage(Main.java:95)
at Main.processPage(Main.java:105)
at Main.processPage(Main.java:105)
at Main.processPage(Main.java:101)
at Main.processPage(Main.java:105)
at Main.processPage(Main.java:101)
at Main.processPage(Main.java:101)
at Main.processPage(Main.java:105)
at Main.processPage(Main.java:105)
at Main.processPage(Main.java:105)
at Main.processPage(Main.java:105)
at Main.processPage(Main.java:105)
at Main.processPage(Main.java:105)
at Main.processPage(Main.java:105)
at Main.processPage(Main.java:105)
at Main.processPage(Main.java:105)
at Main.processPage(Main.java:101)
at Main.processPage(Main.java:105)
at Main.processPage(Main.java:105)
at Main.processPage(Main.java:105)
at Main.processPage(Main.java:101)
at Main.processPage(Main.java:105)
at Main.processPage(Main.java:105)
at Main.main(Main.java:17)

使用

String brew = tmp.replaceAll("'", "''"); 

而不是

String brew = tmp.replaceAll("'", "\\\\'"); 

会导致同样的问题。

我的DB类中的runSql方法:

public ResultSet runSql(String sql) throws SQLException {
    Statement sta = conn.createStatement();
    return sta.executeQuery(sql);
}

如果有任何想法,请告诉我。我花了几个小时尝试不同版本的转义字符等,但我找不到任何解决这个问题的方法。另一双眼睛看着我的代码可能正是我需要的。在此先感谢您的帮助。

修改 我目前没有使用Spring或Hibernate。

2 个答案:

答案 0 :(得分:4)

如果您已经知道如何为PreparedStatement语句使用?值标记(INSERT),为什么不在SELECT语句中使用一个呢?< / p>

将以正确的方式修复您的错误。

答案 1 :(得分:1)

对两个查询使用PreparedStatement,您不必担心替换引号,并且您的选择将与插入一致。现在,似乎select与刚插入的brewery不匹配,但插入因名称重复而失败。