公司局域网中有个网站,希望网站上能够显示本公司股票信息,但是局域网内大部分人都不能上外网,所以就不能考虑内嵌股票网页。
没有股票数据库,那么只能读取某个财经网站的股票信息了,找来找去,找到东方财富网,数据结构比较简单。
比如输入网址http://hqdigi2.eastmoney.com/EM_Quote2010NumericApplication/CompatiblePage.aspx?Type=ZT&jsName=js_fav&fav=0000011,其中000001表示上证指数,1表示上证,
返回结果
var js_fav={favif:["1,000001,上证指数,2765.92,-1.31%,1003.01"]};
这不就是需要的股票信息吗?还有涨幅呢,稍加处理就可以放到自己的网站了。
实现目标需求,有以下几步:
1、创建一个定时器,每隔10秒钟读取一次最新股票信息;
2、给定股票代码,访问股票网址,获得股票信息;
3、处理得到符合要求的股票信息;
4、将股票信息存储在数据库中,供网站读取。
完整代码
没有股票数据库,那么只能读取某个财经网站的股票信息了,找来找去,找到东方财富网,数据结构比较简单。
比如输入网址http://hqdigi2.eastmoney.com/EM_Quote2010NumericApplication/CompatiblePage.aspx?Type=ZT&jsName=js_fav&fav=0000011,其中000001表示上证指数,1表示上证,
返回结果
var js_fav={favif:["1,000001,上证指数,2765.92,-1.31%,1003.01"]};
这不就是需要的股票信息吗?还有涨幅呢,稍加处理就可以放到自己的网站了。
实现目标需求,有以下几步:
1、创建一个定时器,每隔10秒钟读取一次最新股票信息;
2、给定股票代码,访问股票网址,获得股票信息;
3、处理得到符合要求的股票信息;
4、将股票信息存储在数据库中,供网站读取。
完整代码
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
public class TimerTask {
static Connection conn;
static Statement st;
static SimpleDateFormat dtf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static void main(String[] args) {
java.util.Timer timer = new Timer(true);
timer.schedule(new MyTimerTask(), 0, 10*1000);//创建定时器,每隔10秒钟执行一次
while(true)
try {
Thread.currentThread().sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static class MyTimerTask extends java.util.TimerTask{
public void run() {
try
{
//连接数据库
if(conn==null || conn.isClosed()){
String dburl = "jdbc:mysql://localhost:3306/oa";
Class.forName( "org.gjt.mm.mysql.Driver" );
conn = DriverManager.getConnection(dburl, "root", "password" );
st = conn.createStatement();
}
mGetStockPrice(st,"0000011");//取得指定股票的价格,注意股票代码7为,最后一位表示,1表示上证,2表示深证
mGetStockPrice(st,"3990012");
mGetStockPrice(st,"0000392");
mGetStockPrice(st,"2000392");
mGetStockPrice(st,"6000361");
mGetStockPrice(st,"6008371");
mGetStockPrice(st,"6018571");
System.out.println();
}catch(Exception ex){ex.printStackTrace();}
}
public void mGetStockPrice(Statement st,String stockCode){
String str = "";
try
{
System.out.print("+");
str = mGetUrlHtml("http://hqdigi2.eastmoney.com/EM_Quote2010NumericApplication/CompatiblePage.aspx?Type=ZT&jsName=js_fav&fav=" + stockCode,"UTF-8");
//网址返回的数据格式
//var js_fav={favif:["1,000001,上证指数,2765.92,-1.31%,1003.01"]};
//如果股票信息不为空
if(str.indexOf(",")>-1){
String[] arr = str.split(",");
//如果股票信息是有效的
if(arr.length==6){
//如果上涨就是红色,如果下跌就是绿色
if(arr[4].startsWith("-"))
str = "<span style=\"color:green;font-size:10pt;\">" + arr[2] + " " + arr[3] + " " + arr[4] + "</span>";
else
str = "<span style=\"color:red;font-size:10pt;\">" + arr[2] + " " + arr[3] + " " + arr[4] +"</span>";
str = str.replaceAll("'", "\'");
//判断数据库中是否已经存在股票代码,存在即新增,如果不存在就更新价格
ResultSet rs = st.executeQuery("select count(*) from stock_prop where propType='stockPrice' and propName='"+ stockCode +"'");
rs.next();
if("0".equals(String.valueOf(rs.getObject(1))))
st.executeUpdate("insert into stock_prop(propType,propName,propValue) values('stockPrice','"+ stockCode +"','"+ str +"')");
else
st.executeUpdate("update stock_prop set propValue='"+ str +"' where propType='stockPrice' and propName='"+ stockCode +"'");
rs.close();
}
}
System.out.print("*");
}
catch(Exception ex){ex.printStackTrace();}
}
public String mGetUrlHtml(String urlStr,String charSet){
String html= "";
try
{
//读取网址中的信息
URL url = new URL(urlStr);
URLConnection connection = url.openConnection();
String sCurrentLine = "";
StringBuffer sTotalString = new StringBuffer();
BufferedReader l_reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),charSet));
while ((sCurrentLine = l_reader.readLine()) != null)
sTotalString.append(sCurrentLine + "\r\n");
html = sTotalString.toString();
}catch(Exception ex){ex.printStackTrace();}
return html;
}
}
}
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Timer;
public class TimerTask {
static Connection conn;
static Statement st;
static SimpleDateFormat dtf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
public static void main(String[] args) {
java.util.Timer timer = new Timer(true);
timer.schedule(new MyTimerTask(), 0, 10*1000);//创建定时器,每隔10秒钟执行一次
while(true)
try {
Thread.currentThread().sleep(5000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
static class MyTimerTask extends java.util.TimerTask{
public void run() {
try
{
//连接数据库
if(conn==null || conn.isClosed()){
String dburl = "jdbc:mysql://localhost:3306/oa";
Class.forName( "org.gjt.mm.mysql.Driver" );
conn = DriverManager.getConnection(dburl, "root", "password" );
st = conn.createStatement();
}
mGetStockPrice(st,"0000011");//取得指定股票的价格,注意股票代码7为,最后一位表示,1表示上证,2表示深证
mGetStockPrice(st,"3990012");
mGetStockPrice(st,"0000392");
mGetStockPrice(st,"2000392");
mGetStockPrice(st,"6000361");
mGetStockPrice(st,"6008371");
mGetStockPrice(st,"6018571");
System.out.println();
}catch(Exception ex){ex.printStackTrace();}
}
public void mGetStockPrice(Statement st,String stockCode){
String str = "";
try
{
System.out.print("+");
str = mGetUrlHtml("http://hqdigi2.eastmoney.com/EM_Quote2010NumericApplication/CompatiblePage.aspx?Type=ZT&jsName=js_fav&fav=" + stockCode,"UTF-8");
//网址返回的数据格式
//var js_fav={favif:["1,000001,上证指数,2765.92,-1.31%,1003.01"]};
//如果股票信息不为空
if(str.indexOf(",")>-1){
String[] arr = str.split(",");
//如果股票信息是有效的
if(arr.length==6){
//如果上涨就是红色,如果下跌就是绿色
if(arr[4].startsWith("-"))
str = "<span style=\"color:green;font-size:10pt;\">" + arr[2] + " " + arr[3] + " " + arr[4] + "</span>";
else
str = "<span style=\"color:red;font-size:10pt;\">" + arr[2] + " " + arr[3] + " " + arr[4] +"</span>";
str = str.replaceAll("'", "\'");
//判断数据库中是否已经存在股票代码,存在即新增,如果不存在就更新价格
ResultSet rs = st.executeQuery("select count(*) from stock_prop where propType='stockPrice' and propName='"+ stockCode +"'");
rs.next();
if("0".equals(String.valueOf(rs.getObject(1))))
st.executeUpdate("insert into stock_prop(propType,propName,propValue) values('stockPrice','"+ stockCode +"','"+ str +"')");
else
st.executeUpdate("update stock_prop set propValue='"+ str +"' where propType='stockPrice' and propName='"+ stockCode +"'");
rs.close();
}
}
System.out.print("*");
}
catch(Exception ex){ex.printStackTrace();}
}
public String mGetUrlHtml(String urlStr,String charSet){
String html= "";
try
{
//读取网址中的信息
URL url = new URL(urlStr);
URLConnection connection = url.openConnection();
String sCurrentLine = "";
StringBuffer sTotalString = new StringBuffer();
BufferedReader l_reader = new BufferedReader(new InputStreamReader(connection.getInputStream(),charSet));
while ((sCurrentLine = l_reader.readLine()) != null)
sTotalString.append(sCurrentLine + "\r\n");
html = sTotalString.toString();
}catch(Exception ex){ex.printStackTrace();}
return html;
}
}
}