Search code examples
javamysqltwitter

storing tweets in mysql from streaming api using java


I'm using twitter4j's streaming API to collect tweets. I'm doing this on java platform. I'm getting a stream of tweets at console but can't store.

public void onStatus(Status status) {
                              try
             {
               String myDriver = "org.gjt.mm.mysql.Driver";
               String myUrl = "jdbc:mysql://localhost/twitterapi";
               Class.forName(myDriver);
               Connection conn = DriverManager.getConnection(myUrl, "twitterapi", "");

               String query = " insert into tweets"
               + "(tweet_id,tweet_text,screen_name)" + " values" 
                       + "('" + status.getId() + "','" +
                          status.getText() + "', '" +
                          status.getUser().getScreenName() +"')";  

               Statement statement = conn.createStatement();
               statement.executeUpdate(query);
               conn.close();

             }
             catch (Exception e)
                              {
               System.err.println("Got an exception!");
               System.err.println(e.getMessage());
             }
             System.out.println("onStatus @" + status.getUser().getScreenName() + " - " + status.getText());
        }

Please consider every other thing to be correct since that's already working.


Solution

  • In your insert you have a different number of columns to values. You are not specifying the author_id.

    In addition, as DaveH noticed, you're not actually running the SQL command against the server.