X

A blog about JSON in the Oracle database Technology

  • May 18, 2015

Little Java helper to load Facebook postings into Oracle RDBMS

Beda Hammerschmidt
Consulting (Coding) Member of Technical Staff

In the last two blog postings I have referred to the Facebook Graph API.  In case you want to play with the examples, but also want different data, you might find this little Java program handy: it makes https request to a Facebook page and pulls down feeds and stores them in the Oracle database.

You need to obtain a current Access Token from Facebook (https://developers.facebook.com/tools/explorer)
The code is something I hacked together quickly, and I probably missed a couple of things, like closing all resources properly, so use it at you own risk!

I use GSON (https://code.google.com/p/google-gson/) to process the JSON result in Java (to extract the link to the next page in the pagination mechanism).

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import com.google.gson.*;
import javax.net.ssl.HttpsURLConnection;


public class FacebookLoader {

/* Facebook info: use you own FRESH accessToken */
private
static String accessToken =                
 "CAACEdEose0cBAPA9dsaZAT9p6irZB4ibzAgOQLMmaNBqeo1xfclZB9Y85W3jC6ZA8ZCFoAYUypQjVJdh9mCAZAL6TrKkKMBPD5TOo7HQvB8Jf3X5ZBf13E9H5yaMTpLvfx2AJU38GIzHuMwVSvYPoKoYiOB5HKulpovjHxUWl8ZBkUIWsmLE89p3rkw6pgrZAZCGpoVfwVGZCNoRxBYi3RuINuan0nh7lJfJIZD";

private static String id = "Oracle"; // you can use other ids here
private static int numPages = 100;  
        private URL url;

/* oracle connect string info. used to build the connect string. */
        private String host = "localhost";
private int port =  4539;
private String service = "v16.regress.rdbms.dev.us.oracle.com"; 
private String password = "a";
private String user = "a";
private String connectString = "jdbc:oracle:thin:" + user + "/" + password + "@//" + host + ":" + port + "/" + service;
private String tableName = "FB_TAB";
private Connection conn;

public static void main(String[] args) {
       // in case Java needs a proxy to call outside world. May not be needed.
//System.setProperty("http.proxyHost", "yourProxyUrl");
//System.setProperty("http.proxyPort", "80");
//System.setProperty("https.proxyHost", "yourProxyUrl");
//System.setProperty("https.proxyPort", "80");

try {
new FacebookLoader().load();
} catch (Exception e) {
e.printStackTrace();
}
System.out.println("Done");
}

private void load() throws Exception {
JsonParser jsonParser = new JsonParser();
String urlString = "https://graph.facebook.com/" + id + "/feed?access_token=" + accessToken;

for (int i=0; i<numPages; i++){
System.out.println(i);
String content = loadPage(urlString);
saveJsonToDb(content);

/* parse JSON to extract link to next page */
JsonObject jo = (JsonObject)jsonParser.parse(content);
urlString = jo.get("paging").getAsJsonObject().get("next").getAsString();
}
}

private String loadPage(String urlString) throws Exception {
url = new URL(urlString);
HttpsURLConnection con = (HttpsURLConnection) url.openConnection();
BufferedReader br = new BufferedReader(new InputStreamReader(
con.getInputStream()));
String input = br.readLine();
br.close();
return input;
}

private void saveJsonToDb(String content) throws Exception{
System.out.println(content);
DriverManager.registerDriver(new oracle.jdbc.OracleDriver());
if (conn == null)
conn = DriverManager.getConnection(connectString);
String sql = "INSERT INTO " + tableName + " VALUES(?)";
PreparedStatement stmt = conn.prepareStatement(sql);
stmt.setString(1, content);
stmt.executeUpdate();
}
}

Be the first to comment

Comments ( 0 )
Please enter your name.Please provide a valid email address.Please enter a comment.CAPTCHA challenge response provided was incorrect. Please try again.