下载双色球数据到本地sqlite数据库,共236期,sqlite236期,1)将网站http://
分享于 点击 12115 次 点评:274
下载双色球数据到本地sqlite数据库,共236期,sqlite236期,1)将网站http://
1)将网站http://baidu.lehecai.com/lottery/draw/list/50?lottery_type=50&page=1&ds=2011-01-01&de=2012-07-18,8页双色球数据放入sqlite数据库。
2)用到的jar包
httpclient最新版本
jsoup最新版本
sqlite最新版本及jdbc驱动
这几个软件包oschina都有。文档也有。
3)最关键的一步是分析彩票数据,为买彩票提供保障。这个还没处理……能力有限啊,中个大奖多好……
import java.io.BufferedReader;import java.io.IOException;import java.io.InputStream;import java.io.InputStreamReader;import java.sql.Connection;import java.sql.DriverManager;import java.sql.PreparedStatement;import java.sql.ResultSet;import java.sql.SQLException;import java.sql.Statement;import java.util.LinkedList;import java.util.List;import javax.swing.JOptionPane;import org.apache.http.HttpEntity;import org.apache.http.HttpResponse;import org.apache.http.client.ClientProtocolException;import org.apache.http.client.methods.HttpGet;import org.apache.http.impl.client.DefaultHttpClient;import org.apache.http.util.EntityUtils;import org.jsoup.Jsoup;import org.jsoup.nodes.Document;import org.jsoup.nodes.Element;import org.jsoup.select.Elements;public class getDataFromWeb { private static DefaultHttpClient httpClient=new DefaultHttpClient(); private static List<Integer> numList; public static List<Integer> getNumList() { return numList; } public static void setNumList(List<Integer> numList) { getDataFromWeb.numList = numList; } /** * @param args * @throws SQLException * @throws ClassNotFoundException */ public static void main(String[] args) throws ClassNotFoundException, SQLException { // TODO Auto-generated method stub setNumList(new LinkedList<Integer>()); getOne("http://baidu.lehecai.com/lottery/draw/list/50"); setDatabase(); readDatabase(); } public static void getOne(String url) throws ClassNotFoundException, SQLException{ HttpGet get=new HttpGet(url); HttpResponse response; try { response = httpClient.execute(get); HttpEntity entity = response.getEntity(); InputStream in = entity.getContent(); parseHtml(in,"td.td2 > a,span.ball_1,span.ball_2"); } catch (ClientProtocolException e) { // TODO Auto-generated catch block e.printStackTrace(); JOptionPane.showMessageDialog(null, "连接错误,请稍后重试!!"); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); JOptionPane.showMessageDialog(null, "连接错误,请稍后重试!!"); } //关闭连接 get.releaseConnection(); } public static void parseHtml(InputStream in,String aTags) throws ClassNotFoundException, SQLException{ Document document=null; try { document = Jsoup.parse(in, "GBK","");// System.out.println(document.html()); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); JOptionPane.showMessageDialog(null, "从临时文件,读取,转换错误!!"); } Elements elements=document.select(aTags); int i=0; for (Element e : elements) { numList.add(Integer.parseInt(e.html())); } elements=document.select("a.next"); for (Element e : elements) { getOne(e.absUrl("abs:href")); } } public static Connection getConn() throws ClassNotFoundException, SQLException{ Connection connTemp = DriverManager.getConnection("jdbc:sqlite:caipiao.db"); return connTemp; } public static void setDatabase() throws ClassNotFoundException, SQLException{ Class.forName("org.sqlite.JDBC"); Connection conn =getConn(); Statement stat = conn.createStatement(); stat.executeUpdate("drop table if exists caipiao;"); stat.executeUpdate("create table caipiao (issue integer primary key, red1 integer,red2 integer,red3 integer,red4 integer,red5 integer,red6 integer,blue integer);"); PreparedStatement prep = conn.prepareStatement( "insert into caipiao values (?,?,?,?,?,?,?,?);"); for(int i=0;i<numList.size();i++){ int j=i%8+1; prep.setInt(j, numList.get(i)); if(j==8) prep.addBatch(); } conn.setAutoCommit(false); prep.executeBatch(); conn.setAutoCommit(true); conn.close(); }public static void readDatabase() throws ClassNotFoundException, SQLException{ Connection conn =getConn(); Statement stat = conn.createStatement(); ResultSet rs = stat.executeQuery("select * from caipiao;"); while (rs.next()) { System.out.println("期数 = " + rs.getInt(1)+",红球:"+rs.getInt(2)+","+rs.getInt(3)+","+rs.getInt(4)+","+rs.getInt(5)+","+rs.getInt(6)+",篮球:"+rs.getInt(7)); } rs = stat.executeQuery("select count(*) from caipiao;"); System.out.println("总记录数"+rs.getInt(1)); stat.close(); rs.close(); conn.close();}}//该片段来自于http://byrx.net
用户点评