使用twitter4j执行此查询时发生BatchUpdateException

时间:2016-07-25 07:53:11

标签: java mysql twitter4j

Exception in thread "main" java.sql.BatchUpdateException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 're talking election 2016 with @ForbesOpinion's @Avik ' 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.createBatchUpdateException(SQLError.java:1161)
    at com.mysql.jdbc.StatementImpl.executeBatchInternal(StatementImpl.java:1048)
    at com.mysql.jdbc.StatementImpl.executeBatch(StatementImpl.java:958)
    at FirstTwitterApp.main(FirstTwitterApp.java:76)
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 're talking election 2016 with @ForbesOpinion's @Avik ' 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:942)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3966)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3902)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2526)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2673)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2545)
    at com.mysql.jdbc.StatementImpl.executeUpdateInternal(StatementImpl.java:1540)
    at com.mysql.jdbc.StatementImpl.executeBatchInternal(StatementImpl.java:1022)
    ... 2 more

代码:

Date your_date =  tweet.getCreatedAt() ;
SimpleDateFormat formatter = new SimpleDateFormat("YYYY-MM-dd h:m:s");
String date = formatter.format(your_date);
String textStatus = tweet.getText();
System.out.println(textStatus);
String qry="insert into twitter_data(id,name,text_status,profile_picture,created_date)values(NULL,'"
        +tweet.getUser().getScreenName()+"','"
        +textStatus+"','"+tweet.getUser().getOriginalProfileImageURL()+"','"
        +date+"')";
connect.st.addBatch(qry);
connect.st.executeBatch();

1 个答案:

答案 0 :(得分:1)

您的状态文字包含单引号。因为在构建查询之前没有转义字符串,所以这会有效地关闭用于包装字符串的单引号。

从不通过连接用户提供的字符串来构建SQL查询。搜索“小鲍比掉落表”以找出原因。

Little Bobby Drop Tables

始终使用PreparedStatement来构建查询,因为这会自动为您处理转义。