アットウィキロゴ

ごおgぇふぁいなんせ

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

class game0505{

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

String data[]=new String[1000];
String date[]=new String[100];
String ddate[]=new String[10000];
int dcomp[]=new int[10000];
double oprice[]=new double[100];
double hprice[]=new double[100];
double lprice[]=new double[100];
double cprice[]=new double[100];
double volume[]=new double[100];
int dcheck[]=new int[100];
int num;
int dbnumber;
int s,sx,ds;
int z,h;


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

game0505(){

String urlx;

urlx="http://www.google.com/finance/historical?q=NYSE:HMC";

getdata(urlx);

makedata();

checkdata();


for(s=0;s<num;s++){
z=0;
for(sx=1;sx<dbnumber+1;sx++){
h=0;
if(date[s].equals(ddate[sx]))h=h+1;
if(dcomp[sx]==50)h=h+1;
if(h==2)z=100;
}
if(z>50)dcheck[s]=100;
}

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










}

void insertdata(int s,int ds){

Connection dbx;    

try {
dbx = DriverManager.getConnection(url, usr, pwd);
String sql = "insert into zgoogle(keyword,company,date,oprice,hprice,lprice,cprice,volume) values (?, ?,?,?,?,?,?,?)";
PreparedStatement pstmt = dbx.prepareStatement(sql);
pstmt.setInt(1, ds);
pstmt.setInt(2, 50);  
pstmt.setString(3, date[s]);
pstmt.setDouble(4, oprice[s]);
pstmt.setDouble(5, hprice[s]);
pstmt.setDouble(6, lprice[s]);
pstmt.setDouble(7, cprice[s]);
pstmt.setDouble(8, volume[s]);
int nu = pstmt.executeUpdate();

dbx.close();

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

}

void checkdata(){

Connection dbx;    
Statement  stx;
ResultSet rsx;
int s,comp;
String item;

s=0;

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

      rsx = stx.executeQuery("select * from zgoogle");
      if (rsx != null) {
        while (rsx.next()) {
        s = rsx.getInt("keyword");        
        comp = rsx.getInt("company");
        item = rsx.getString("date");
dcomp[s]=comp;
ddate[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 zgoogle(";
sqx=sqx+"keyword integer,";
sqx=sqx+"company integer,";
sqx=sqx+"date varchar(50),";  
sqx=sqx+"oprice double,";
sqx=sqx+"hprice double,";
sqx=sqx+"lprice double,";
sqx=sqx+"cprice double,";
sqx=sqx+"volume double)";

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

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

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






void makedata(){

String data1[]=new String[1000];
int point[]=new int[1000];

String str1;
str1="<td class=";
int n,m;


m=0;
for(n=0;n<num;n++){
if(data[n].indexOf(str1)>-1)data1[m]=data[n];
if(data[n].indexOf(str1)>-1)m=m+1;
}

num=m;

m=0;
for(n=0;n<num;n++){
if(data1[n].indexOf("lm")>-1)point[m]=n;
if(data1[n].indexOf("lm")>-1)m=m+1;
}

num=m;
for (n=0;n<num;n++){
m=point[n];
String[] x1=data1[m].split(">");
date[n]=x1[1];
String[] x2=data1[m+1].split(">");
oprice[n]=makeprice(x2[1]);
String[] x3=data1[m+2].split(">");
hprice[n]=makeprice(x3[1]);
String[] x4=data1[m+3].split(">");
lprice[n]=makeprice(x4[1]);
String[] x5=data1[m+4].split(">");
cprice[n]=makeprice(x5[1]);
String[] x6=data1[m+5].split(">");
volume[n]=makeprice(x6[1]);
}

}

double makeprice(String str){
int p;
double mx;
p=ckc(str);
mx=0;
if(p==1)mx=make1(str);
if(p==2)mx=make2(str);
if(p==0)mx=make0(str);
return mx;
}

int ckc(String str){
int px;
String[] z=str.split(",");
px=z.length;
return px-1;
}

double make0(String str){
double a;
a=Double.parseDouble(str);
return a;
}


double make2(String str){
double a,b,c,d;
String[] y=str.split(",");
a=Double.parseDouble(y[0]);
b=Double.parseDouble(y[1]);
c=Double.parseDouble(y[2]);;
d=1000*1000*a+1000*b+c;
return d;
}

double make1(String str){
double a,b,c;
String[] y=str.split(",");
a=Double.parseDouble(y[0]);
b=Double.parseDouble(y[1]);
c=1000*a+b;
return c;
}




void getdata(String name){  
int n;

num=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;
num=n;
        }
        reader.close();
        con.disconnect();
}
catch(Exception e) { System.err.println(e); }  
}

}
最終更新:2011年03月28日 15:25