以大秦鐵路(股票代碼:601006)為例。如果想獲取其最新行情,只需訪問新浪的股票數據接口:/list=sh601006,這個url會返回壹串文本,例如:
Var hq_str_sh601006= "大秦鐵路,27.55,27.25,26.91,27.55,26.20,26.438+0,26.92,
22114263, 589824680, 4695, 26.91, 57590, 26.90, 14700, 26.89, 14300,
26.88, 15100, 26.87, 3100, 26.92, 8900, 26.93, 14230, 26.94, 25150, 26.95, 15220, 26.96, 2008-01-11, 15:05:32";
這個字符串是由很多數據拼接而成的,不同含義的數據之間用逗號隔開。按照程序員的想法,序號從0開始。
0:“大秦鐵路”,股票名稱;
1:“27.55”,今日開盤價;
2:“27.25”,昨日收盤價;
3:“26.91”,現價;
4:“27.55”,今日最高價;
5:“26.20”,今日最低價;
6:“26.91”,投標價,即“買壹”報價;
7:“26.92”,拍賣價,即“賣壹”報價;
8:“22114263”,交易的股數,因為股票交易的基本單位是100股,所以在使用的時候,這個值通常要除以100;
9:“589824680”,交易金額,單位是“元”,為了壹目了然,“萬元”通常是交易金額的單位,所以這個值通常除以萬;
10:“4695”,“買壹”申請4695股,即47手;
11:“26.91”,“買壹個”報價;
12:“57590”,“買兩個”
13:“26.90”,“買二”
14:“14700”,“買三”
15:“26.89”,“買三”
16:“14300”,“買四”
17:“26.88”,“買四”
18:“15100”,“買五”
19:“26.87”,“買五”
20:“3100”,“賣壹”申報3100股,即31手;
21:“26.92”,“賣壹個”優惠
(22,23),(24,25),(26,27),(28,29)分別是“賣二”到“賣四”。
30:“2008-01-11”,日期;
31:“15:05:32”,時間;
相應的,妳也可以得到深圳股市的信息,但是這種方法的缺點是只能得到最新或者當前的股票數據,不能將歷史數據導入數據庫。當然,妳也可以以某壹天為起點,自己重新創建歷史數據。於是繼續尋找其他網站接口,最後找到了雅虎財經網站,它提供了壹個可以直接將股票歷史數據導入Excel的接口,真的很方便!直接在瀏覽器address /table.csv中數據化URL就可以了?S=股票代碼,但是手工輸入然後壹個壹個下載保存太麻煩了。光是上交所的股票就有800多只,手動下載後估計還得重新下載。所以我的想法是,1,多線程下載股票文件。2.將這些文件導入數據庫。
1.1文件下載類:
導入Java . io . *;
導入Java . net . *;
導入Java . util . list;
導入fatowen . stock system . sys config . data . download VO;
公共類HttpDownFile {
private static int BUFFER _ SIZE = 8096;
/* *根據URL下載文件並保存。
* @param destUrl字符串
* @param文件名字符串
* @拋出異常
*/
公共void saveToFile(String destUrl,String fileName)引發IOException {
FileOutputStream fos = null
BufferedInputStream bis = null
HttpURLConnection /table.csv?s = ";
字符串結果= " ";
字符串保存路徑= " ";
public HisDataAddThread(List paramList,String savePath){
this . my param list = param list;
this . save path = save path;
}
公共無效運行(){
while(runFlag){
download data = public data util . getdownloaddata(myParamList);
如果(!Lib.isEmpty(downLoadData)){
HttpDownFile oInstance = new HttpDownFile();
嘗試{
oin instance . savetofile(base URL+download data,savePath + downLoadData +"。CSV”);
}catch(異常錯誤){
system . out . println(err . tostring());
}
}否則{
runFlag = false
}
嘗試{
thread . sleep(1000);
} catch (InterruptedException e) {
e . printstacktrace();
}
}
}
公共列表getFailureList() {
返回失敗列表;
}
public void setfailurlist(List failurlist){
this . failure list = failure list;
}
公共列表getSuccessList() {
返回成功列表;
}
public void setSuccessList(列出成功列表){
this . success list = success list;
}
}
2.將下載的文件保存到數據庫工具類。
導入Java . io . buffered reader;
導入Java . io . file;
導入Java . io . filereader;
導入Java . io . io exception;
導入Java . util . ArrayList;
導入Java . util . iterator;
導入Java . util . list;
公共類CSVUtitl {
private buffered reader buffered reader = null;
private List List = new ArrayList();
public CSVUtitl(){
}
公共CSVUtitl(字符串文件名)引發IOException{
buffered reader = new buffered reader(新文件閱讀器(文件名));
String stemp
while((stemp = buffered reader . readline())!= null){
list . add(stemp);
}
}
公共列表getList()引發IOException {
退貨單;
}
//獲取csv文件中的行數
public int getRowNum(){
返回list . size();
}
//獲取csv文件中的列數
public int getColNum(){
如果(!list.toString()。等於("[]"){
//在//csv文件中,列由“,”分隔。
if(list.get(0)。toString()。包含(","){
返回list.get(0)。toString()。拆分(“,”)。長度;
}else if(list.get(0)。toString()。修剪()。長度()!= 0) {
返回1;
}否則{
返回0;
}
}否則{
返回0;
}
}
//獲取指定行的值
公共字符串getRow(int index) {
if (this.list.size()!= 0)
return(字符串)list.get(索引);
其他
返回null
}
//獲取指定行的值
公共字符串getCol(int index){
if (this.getColNum() == 0){
返回null
}
string buffer scol = new string buffer();
String temp = null
int colnum = this . get colnum();
if(colnum & gt;1){
for(Iterator it = list . Iterator();it . has next();) {
temp = it.next()。toString();
scol = scol.append(temp.split(","[index] +",");
}
}否則{
for(Iterator it = list . Iterator();it . has next();) {
temp = it.next()。toString();
scol = scol.append(temp +",");
}
}
String str = new String(scol . tostring());
str = str.substring(0,str . length()-1);
返回字符串;
}
//獲取指定行和列的值。
公共字符串getString(int row,int col) {
String temp = null
int colnum = this . get colnum();
if(colnum & gt;1){
temp = list.get(row)。toString()。split(","[col];
}else if(colnum == 1) {
temp = list.get(row)。toString();
}否則{
temp = null
}
返回溫度;
}
public void CsvClose()拋出IOException {
this . buffered reader . close();
}
公共void運行(字符串文件名)引發IOException {
CSVUtitl cu =新的CSVUtitl(文件名);
for(int I = 0;我& ltCu . getrownum();i++){
string sscc tag = format data(Cu . getstring(I,1));//獲取第壹行第壹列的數據。
string SiteName = format data(Cu . getstring(I,2));//獲取第壹行第二列的數據。
string station id = format data(Cu . getstring(I,3));
//將數據保存到數據庫
......
......
......
}
以立方計量的CsvClose();
}
公共字符串格式數據(字符串基礎數據){
字符串結果= null
如果(!"".等於(基礎數據)和。& ampbaseData!= null){
if(base data . length()& gt;1){
result = base data . substring(1,base data . length());
result = result.substring(0,result . length()-1);
}否則{
result = baseData
}
}否則{
結果= " ";
}
返回result . trim();
}
公共靜態void main(String[] args)引發IOException {
CSV utitl test = new CSV utitl();
嘗試{
文件路徑=新文件(" e:\ \ data ");
file[]f = path . list files();
list l = new ArrayList();
for(int I = 0;我& ltf .長度;i++){
if(f[i]。getName()。endsWith("。csv "))
l . add(f[I]);www.2cto.com
}
叠代器it = l . iterator();
while(it.hasNext()){
File ff =(File)it . next();
test . run(path . tostring()+file . separator+ff . getname());
}
}catch(異常e){
}
}
}