mysql查询法定假日_从节假日api查询数据并插入到数据库代码实例

packagecn.com.ebidding;importnet.sf.json.JSONObject;import org.apache.commons.httpclient.*;importorg.apache.commons.httpclient.methods.GetMethod;importorg.apache.commons.httpclient.params.HttpMethodParams;importorg.apache.http.HttpEntity;importorg.apache.http.HttpResponse;importorg.apache.http.client.methods.HttpPost;importorg.apache.http.entity.StringEntity;importorg.apache.http.impl.client.DefaultHttpClient;importorg.apache.http.util.EntityUtils;importcom.mysql.jdbc.Connection;importcom.mysql.jdbc.PreparedStatement;importjava.io.IOException;importjava.sql.DriverManager;importjava.sql.SQLException;importjava.util.Calendar;/*** Created by liqun.chen on 2017/5/15.*/

public classHttpUtil {/*** json 字符串

*@paramurl

*@paramparam

*@return

*/

public staticString getSerchPersion(String url,String param){/*1 生成 HttpClinet 对象并设置参数*/HttpClient httpClient= newHttpClient();//设置 Http 连接超时为5秒

httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(5000);/*2 生成 GetMethod 对象并设置参数*/GetMethod getMethod= newGetMethod(url);//设置 get 请求超时为 5 秒

getMethod.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, 5000);//设置请求重试处理,用的是默认的重试处理:请求三次

getMethod.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, newDefaultHttpMethodRetryHandler());

String response= "";/*3 执行 HTTP GET 请求*/

try{int statusCode =httpClient.executeMethod(getMethod);/*4 判断访问的状态码*/

if (statusCode !=HttpStatus.SC_OK) {

System.err.println("请求出错: "+getMethod.getStatusLine());

}/*5 处理 HTTP 响应内容*/

//HTTP响应头部信息,这里简单打印

Header[] headers =getMethod.getResponseHeaders();for(Header h : headers)

System.out.println(h.getName()+ "------------ " +h.getValue());//读取 HTTP 响应内容,这里简单打印网页内容

byte[] responseBody = getMethod.getResponseBody();//读取为字节数组

response = newString(responseBody, param);

System.out.println("----------response:" +response);//读取为 InputStream,在网页内容数据量大时候推荐使用//InputStream response = getMethod.getResponseBodyAsStream();

} catch(HttpException e) {//发生致命的异常,可能是协议不对或者返回的内容有问题

System.out.println("请检查输入的URL!");

e.printStackTrace();

}catch(IOException e) {//发生网络异常

System.out.println("发生网络异常!");

e.printStackTrace();

}finally{/*6 .释放连接*/getMethod.releaseConnection();

}returnresponse;

}/*** post请求

*@paramurl

*@paramjson

*@return

*/

public staticJSONObject doPost(String url,JSONObject json){

DefaultHttpClient client= newDefaultHttpClient();

HttpPost post= newHttpPost(url);

JSONObject response= null;try{

StringEntity s= newStringEntity(json.toString());

s.setContentEncoding("UTF-8");

s.setContentType("application/json");//发送json数据需要设置contentType

post.setEntity(s);

HttpResponse res=client.execute(post);if(res.getStatusLine().getStatusCode() ==HttpStatus.SC_OK){

HttpEntity entity=res.getEntity();

String result= EntityUtils.toString(res.getEntity());//返回json格式:

try{

response=JSONObject.fromObject(result);

}catch(Exception e) {

response=new JSONObject().accumulate("key", result.toString());

}

}

}catch(Exception e) {throw newRuntimeException(e);

}returnresponse;

}private staticConnection getConn() {

String driver= "com.mysql.jdbc.Driver";

String url= "jdbc:mysql://localhost:3306/cec_dev";

String username= "root";

String password= "123456";

Connection conn= null;try{

Class.forName(driver);//classLoader,加载对应驱动

conn =(Connection) DriverManager.getConnection(url, username, password);

}catch(ClassNotFoundException e) {

e.printStackTrace();

}catch(SQLException e) {

e.printStackTrace();

}returnconn;

}private static intdeleted( ) {

Connection conn=getConn();int i = 0;

String sql= "DELETE FROM sys_holiday";

PreparedStatement pstmt;try{

pstmt=(PreparedStatement) conn.prepareStatement(sql);

i=pstmt.executeUpdate();

pstmt.close();

conn.close();

}catch(SQLException e) {

e.printStackTrace();

}returni;

}private static intinsert( String year,String m,String d,String holiday) {

Connection conn=getConn();int i = 0;

String sql= "INSERT INTO sys_holiday (YEAR,MONTH,DAY,holiday) VALUES(?,?,?,?)";

PreparedStatement pstmt;try{

pstmt=(PreparedStatement) conn.prepareStatement(sql);

pstmt.setString(1, year);

pstmt.setString(2, m);

pstmt.setString(3, d);

pstmt.setString(4, holiday);

i=pstmt.executeUpdate();

pstmt.close();

conn.close();

}catch(SQLException e) {

e.printStackTrace();

}returni;

}//调用

public static void main(String arg[]) throwsException {

deleted();

Calendar now=Calendar.getInstance();

now.set(now.get(now.YEAR),01, 01);for (int i = 0; i < 400; i++) {

String y=String.valueOf(now.get(now.YEAR));

String m=String.valueOf(now.get(now.MONTH)+1);

String d=String.valueOf(now.get(now.DATE));if (m.length()==1) {

m="0"+m;

}if (d.length()==1) {

d="0"+d;

}

String url= "http://tool.bitefu.net/jiari/?d="+y+m+d;

System.out.println(url);

JSONObject params= newJSONObject();//post 请求

JSONObject jsonObject =doPost(url,params);

System.out.println(jsonObject.toString());

System.out.println(jsonObject.get("key").toString());

insert(y, m, d,jsonObject.get("key").toString() );

now.add(now.DATE,1);

}

}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值