/////////////////////////////////////////////////////////////////////// // BiffSocko // getArticles.java // gets an RSS Feed and populates an RDBMS (MySQL in this case). // // DATABASE SETUP: // // create table NEWS( // indx int AUTO_INCREMENT, // DATE date, // NEWS LongText, // PRIMARY KEY (indx)); // // REMEMBER TO SET UP DATABASE/USER/PASS INFO in the jdbc connect string /////////////////////////////////////////////////////////////////////// import java.io.*; import java.net.*; import java.sql.*; public class getArticles { public static void main(String[] args) { try { ///////////////////////////////////////////// // Some declarations ///////////////////////////////////////////// String nextLine=null; // String for reading in data String superString=new String(""); // String for concat URL url = null; // URL object for newsurl URLConnection urlConn = null; // URL Connection for newsurl InputStreamReader inStream = null; // inputstream for Buffered reader BufferedReader buff = null; // read data from URL String newsurl = "http://p.moreover.com/cgi-local/page?c=Top%20stories&o=xml"; // URL for news xml Statement stmt; // String for sql statement Connection conn; // connection for database String SQLStatement = null; // SQL Statement ///////////////////////////////////////////// // Make a connection to the database ///////////////////////////////////////////// Class.forName("com.mysql.jdbc.Driver").newInstance(); conn = DriverManager.getConnection("jdbc:mysql://database.server.com:3306/DB","username", "password"); ///////////////////////////////////////////// // open up the URL connection ///////////////////////////////////////////// url = new URL(newsurl); urlConn = url.openConnection(); inStream = new InputStreamReader(urlConn.getInputStream()); buff= new BufferedReader(inStream); /////////////////////////////////////////////// // Read the url and put it into one big string /////////////////////////////////////////////// while (true){ nextLine = buff.readLine(); if (nextLine !=null){ superString = superString.concat(nextLine); }else{ break; } } /////////////////////////////////////////////// // INSERT the article into the database /////////////////////////////////////////////// superString = superString.replaceAll("'", "\\\\'"); SQLStatement = "INSERT INTO NEWS (DATE,NEWS) VALUES (NOW(),'" +superString+"')"; stmt = conn.createStatement (); stmt.executeUpdate(SQLStatement); stmt.close(); conn.close(); /////////////////////////////////////////// // now update the database with the // XML /////////////////////////////////////////// //System.out.println(superString); }catch (Exception e2) { e2.printStackTrace(); } } }