アットウィキロゴ

yahoofin

import java.net.*;  
import java.io.*;
import java.sql.*;

class game0504{

String bun;
int num,s,sx;
String article[]=new String[100];
String title[]=new String[100];
String des[]=new String[100];
String link[]=new String[100];
String pdate[]=new String[100];
String dbtitle[]=new String[1000];
int dcheck[]=new int[100];

String url = "jdbc:derby:db;create=true";  
String usr = "";  
String pwd = "";

int dbnumber,ds;
int z;

public static void main(String [] args) {
        game0504 test=new game0504();        
    }  

game0504(){

String urlx;

urlx="http://finance.yahoo.com/rss/headline?s=intc";

generatedata(urlx);

urlx="http://finance.yahoo.com/rss/headline?s=amzn";

generatedata(urlx);

urlx="http://finance.yahoo.com/rss/headline?s=csco";

generatedata(urlx);

}

void generatedata(String urlx){

getdata(urlx);
makearticle();
makeitem();

checkdata();

for(s=1;s<num+1;s++){
z=0;
for(sx=1;sx<dbnumber+1;sx++){
if(title[s].equals(dbtitle[sx]))z=100;
}
if(z>50)dcheck[s]=100;
}

ds=dbnumber;
for(s=1;s<num+1;s++){
if(dcheck[s]<50)ds=ds+1;
if(dcheck[s]<50)insertdata(s,ds);
}

}


void insertdata(int s,int ds){

Connection dbx;    

try {
dbx = DriverManager.getConnection(url, usr, pwd);
String sql = "insert into yfr2(keyword,title,pdate,des,link) values (?, ?,?,?,?)";
PreparedStatement pstmt = dbx.prepareStatement(sql);
pstmt.setInt(1, ds);
pstmt.setString(2, title[s]);  
pstmt.setString(3, pdate[s]);
pstmt.setString(4, des[s]);
pstmt.setString(5, link[s]);

int nu = pstmt.executeUpdate();

dbx.close();

}catch (SQLException e){System.out.println("SQLException:" + e.getMessage());}

}



void checkdata(){

Connection dbx;    
Statement  stx;
ResultSet rsx;

try {
      dbx = DriverManager.getConnection(url, usr, pwd);
      stx = dbx.createStatement();

      rsx = stx.executeQuery("select * from yfr2");
      if (rsx != null) {
        while (rsx.next()) {
          s = rsx.getInt("keyword");
          String item = rsx.getString("title");
dbtitle[s]=item;
}
}

dbnumber=s;

rsx.close();
stx.close();
dbx.close();

} catch (Exception ex) {ex.printStackTrace();}  



}


void maketable(){

Connection dbx;  
Statement stx;  
String sqx;

sqx="create table yfr2(";
sqx=sqx+"keyword integer,";
sqx=sqx+"title varchar(200),";  
sqx=sqx+"pdate varchar(100),";
sqx=sqx+"des varchar(1000),";
sqx=sqx+"link varchar(500))";

try {  
dbx = DriverManager.getConnection(url, usr, pwd);
stx = dbx.createStatement();  
stx.executeUpdate(sqx);

stx.close();
dbx.close();

}
catch (Exception ex) { ex.printStackTrace();}
}






void makeitem(){

for (s=1;s<num+1;s++){
title[s]=makedata(article[s],"title");
des[s]=makedata(article[s],"description");
link[s]=makedata(article[s],"link");    
pdate[s]=makedata(article[s],"pubDate");
}

}

String makedata(String doc,String you){
String sx1,sx2,sx;
int x1,x2;
int lp1;
int lp2;
sx1="<"+you+">";
sx2="</"+you+">";
lp1=sx1.length();
lp2=sx2.length();
x1=doc.indexOf(sx1);
x2=doc.indexOf(sx2);
sx="";
if(x1>-1)sx=doc.substring(x1+lp1,x2);
return sx;
}

void makearticle(){
int point,x,n;
int x2,lp2;
String sx;
point=0;
x=bun.indexOf("<item>");
n=0;
while(x>0){
n=n+1;
article[n]=makeart(bun,point);
sx="</item>";
lp2=sx.length();
x2=bun.indexOf(sx,point);
point=x2+lp2;
x=bun.indexOf("<item>",point);
}
num=n;
}

String makeart(String bun,int point){
String sx1,sx2;
int x1,x2;
int lp1;
int lp2;
sx1="<item>";
sx2="</item>";
lp1=sx1.length();
lp2=sx2.length();
x1=bun.indexOf(sx1,point);
x2=bun.indexOf(sx2,point);
return bun.substring(x1,x2+lp2);
}

void getdata(String name){
int nu;
int n;
String data[]=new String[1000];

nu=0;
try{
        URL url = new URL(name);

        HttpURLConnection con = (HttpURLConnection)url.openConnection();
        con.setRequestMethod("GET");
        con.connect();

        BufferedReader reader =
            new BufferedReader(new InputStreamReader(con.getInputStream(),"UTF-8"));
n=0;
        while (true){
            String line = reader.readLine();
            if ( line == null ){
                break;
            }
data[n]=line;
n=n+1;
nu=n;
        }
        reader.close();
        con.disconnect();
}
catch(Exception e) { System.err.println(e); }  
bun="";
for (n=0;n<nu;n++){
bun=bun+data[n];
}

}

}
最終更新:2011年03月28日 09:42