因为中国外汇交易中心使用的数据格式为json,JAVA提供了专门解析json的lib,我们首先要引入该lib(解析json的依赖包有几个,csdn有提供所有lib的压缩包,我用的是json-lib-2.4-jdk15.rar,大家可以在csdn里搜索一下,解压出来即可)。以下是先获得中国外汇交易中心的json字符内容,然后解析json得到的汇率写入Tiptop oracle 数据库Table:azk_file,所以还需要jdbc驱动包(这个驱动包CSDN也很容易找到)。
如果是为TOP GP开发 ,那么你可以直接copy,然后打包成jar,在程序aooi070里调用这个jar即可。(aooi070可在(p_cron设置后台执行,因为外汇网站每天大概上午9:30更新,建议9:40左右执行), 附上的CSDN也是我的博客 先看JAVA源码: MAIN部分(传入参数args[0]为要取得汇率的日期): import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.text.ParseException; import net.sf.json.JSONArray; import net.sf.json.JSONObject; public class GetAooi070Data { public static void main(String[] args) throws ParseException { String date = args[0]; String url = "http://www.chinamoney.com.cn/r/cms/www/chinamoney/data/fx/ccpr.json?t=" + date; // url链接 String json = loadJson(url); // 获得json字符串 JSONObject object = JSONObject.fromObject(json); // 获得jason对象 JSONArray array = object.getJSONArray("records");// 丢掉头,只取records部分数据 new inputTiptop("数据库账号", "密码", "数据库IP地址", "数据库Schema", date).insertData(array); } public static String loadJson(String url) { StringBuilder json = new StringBuilder(); try { URL urlObject = new URL(url); URLConnection uc = urlObject.openConnection(); BufferedReader in = new BufferedReader(new InputStreamReader(uc.getInputStream(), "utf-8")); String inputLine = null; while ((inputLine = in.readLine()) != null) { json.append(inputLine); } in.close(); } catch (MalformedURLException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } return json.toString(); } } 再看CLASS部分(这里取了几个币别,其中日元对RMB汇率要注意换算,官方的是100日元对1人民币的汇率,结果要除以100的): import java.sql.Connection; import java.sql.DriverManager; import java.sql.PreparedStatement; import java.sql.SQLException; import net.sf.json.JSONArray; public class inputTiptop { // 定义数据库变量 private String url; private String ipaddress; private String passname; private String password; private String date; private String database; private String driver; private Connection con; public inputTiptop(String p_name, String p_word, String p_ip, String p_db, String p_date) // 构造指定帐号密码数据库链接 { passname = p_name; password = p_word; ipaddress = p_ip; database = p_db; driver = "oracle.jdbc.driver.OracleDriver"; date = p_date; try { Class.forName(driver); } catch (ClassNotFoundException e) { System.out.println(e); e.printStackTrace(); } url = "jdbc:oracle:thin:@" + ipaddress + ":1521" + ":" + database; } public void insertData(JSONArray array) { String sql = "Insert into azk_file (azk01,azk02,azk03,azk04,azk041,azk05,azk051,azk052, " + " azkuser,azkgrup,azkmodu,azkdate,azkoriu,azkorig,azktime) " + " Values (?, to_date(?, 'yyyy-mm-dd'), '', '', ? , '', '', '', '', '', '', '', '', '', '') "; try { con = DriverManager.getConnection(url, passname, password); con.setAutoCommit(false); PreparedStatement ps = con.prepareStatement(sql);// SQL预处理 int flag = 0; for (int i = 0; i < (array.size()); i++) { if (array.getJSONObject(i).getString("vrtEName").equals("USD/CNY")) { ps.setString(1, "USD"); ps.setString(2, date); ps.setFloat(3, Float.parseFloat(array.getJSONObject(i).getString("price"))); ps.addBatch(); flag = 1 + flag; } else if (array.getJSONObject(i).getString("vrtEName").equals("EUR/CNY")) { ps.setString(1, "EUR"); ps.setString(2, date); ps.setFloat(3, Float.parseFloat(array.getJSONObject(i).getString("price"))); ps.addBatch(); flag = 1 + flag; } else if (array.getJSONObject(i).getString("vrtEName").equals("100JPY/CNY")) { ps.setString(1, "JPY"); ps.setString(2, date); ps.setFloat(3, Float.parseFloat(array.getJSONObject(i).getString("price")) / 100); ps.addBatch(); flag = 1 + flag; }else { continue; }; } if (flag == 3){ int rst[] = ps.executeBatch();// 执行批处理,判断正确错误 if (rst.length > 0) { con.commit(); System.out.println(date.toString() + " insert " + passname + " success!"); } else { con.rollback(); System.out.println(date.toString() + " insert " + passname + " fail!"); } } ps.close(); con.close(); } catch (SQLException e) { System.out.println(e); e.printStackTrace(); } } } 以上打包的jar可执行文件为:ERPAooi070_fat.jar 然后在tiptop插入如下代码段: #NO.180305 ---ADD---S--- IF l_type=0 THEN RETURN END IF DELETE FROM azk_file WHERE azk02 = g_today AND azk01 IN('USD','EUR','JPY') #删除旧数据 LET l_cmd = "java -jar /usr/local/rate_prd/ERPAooi070_fat.jar " ,g_plant, " ",g_today USING "yyyymmdd", " >> /usr/local/rate_prd/ERPAooi070_fat_run.log" RUN l_cmd #运行java 解析网页json,直接写入数据库 LET l_cnt = 0 SELECT count(*) INTO l_cnt FROM azk_file WHERE azk02 = g_today AND azk01 IN('USD','EUR','JPY') IF l_cnt > 0 THEN ELSE LET g_flag1 = '3' END IF #將輸入的每日匯率做本月的加總 LET l_sql = " SELECT azk01, azk02 FROM azk_file ", " WHERE azk01 IN ('USD','EUR','JPY') ", " AND azk02 = ?" PREPARE i070_pre2 FROM l_sql DECLARE i070_cs2 CURSOR FOR i070_pre2 FOREACH i070_cs2 USING g_today INTO g_azk.azk01, g_azk.azk02 IF STATUS THEN CALL cl_err('FOREACH i070_cs2',STATUS,1) EXIT FOREACH END IF SELECT COUNT(1) INTO l_cnt FROM azi_file WHERE azi01 = g_azk.azk01 AND aziacti='Y' IF l_cnt < 1 THEN CONTINUE FOREACH END IF CALL i070_mth() END FOREACH #NO.180305 ---ADD---E--- 作者:居士爱吃泡面 来源:CSDN 原文:https://blog.csdn.net/ERP_TOP_DD/article/details/81502298 版权声明:本文为博主原创文章,转载请附上博文链接! |
|
|
沙发#
发布于:2019-04-19 21:56
学习学习
|
|
板凳#
发布于:2019-04-22 13:45
|
|