ViewController.java 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. package com.zskk.control;
  2. import java.io.IOException;
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6. import java.util.HashMap;
  7. import java.util.List;
  8. import java.util.Map;
  9. import com.alibaba.fastjson.JSON;
  10. import com.jfinal.core.Controller;
  11. import com.jfinal.kit.PropKit;
  12. import com.jfinal.plugin.activerecord.ActiveRecordPlugin;
  13. import com.jfinal.plugin.activerecord.Db;
  14. import com.jfinal.plugin.activerecord.Record;
  15. import com.jfinal.plugin.activerecord.dialect.SqlServerDialect;
  16. import com.jfinal.plugin.druid.DruidPlugin;
  17. import okhttp3.FormBody;
  18. import okhttp3.MediaType;
  19. import okhttp3.OkHttpClient;
  20. import okhttp3.Request;
  21. import okhttp3.RequestBody;
  22. import okhttp3.Response;
  23. public class ViewController extends Controller {
  24. private static final MediaType JSON_CODE = MediaType.get("application/json; charset=utf-8");
  25. private static final OkHttpClient OKHTTP_CLIENT = new OkHttpClient();
  26. /**
  27. * 在被连接数据库执行sql语句
  28. */
  29. public void executeSql() {
  30. try {
  31. List<Record> d = Db.use("connected").find(this.getPara("sqlstr"));
  32. this.renderJson(d);
  33. } catch (Exception e) {
  34. // TODO: handle exception
  35. this.renderText(e.toString());
  36. }
  37. }
  38. /**
  39. * 在被连接数据库执行sql语句
  40. */
  41. public void executeSqlUpdate() {
  42. try {
  43. Integer d = Db.use("connected").update(this.getPara("sqlstr"));
  44. this.renderText(d.toString());
  45. } catch (Exception e) {
  46. // TODO: handle exception
  47. this.renderText(e.toString());
  48. }
  49. }
  50. public void testConn() {
  51. try {
  52. DruidPlugin druidPluginConnected = createConnectedDruidPlugin();
  53. druidPluginConnected.start();
  54. // 配置ActiveRecord插件
  55. ActiveRecordPlugin arpConnected = new ActiveRecordPlugin("connected", druidPluginConnected);
  56. arpConnected.setDialect(new SqlServerDialect());
  57. arpConnected.start();
  58. } catch (Exception e) {
  59. // TODO: handle exception
  60. this.renderText(e.toString());
  61. }
  62. }
  63. public void st() {
  64. this.renderText(new Date().toString());
  65. }
  66. public static DruidPlugin createConnectedDruidPlugin() {
  67. return new DruidPlugin(PropKit.get("jdbcUrl_connected"), PropKit.get("user_connected"),PropKit.get("password_connected").trim());
  68. }
  69. /**
  70. * post请求
  71. * @param url-请求地址
  72. * @param map-参数集合
  73. * @return
  74. */
  75. private static String doPost(String url, Map<String, String> map) {
  76. FormBody.Builder builder = new FormBody.Builder();
  77. for (String key : map.keySet()) {
  78. builder.add(key, map.get(key));
  79. }
  80. RequestBody formBody = builder.build();
  81. Request request = new Request.Builder().url(url).post(formBody).build();
  82. try (Response response = OKHTTP_CLIENT.newCall(request).execute()) {
  83. if (!response.isSuccessful()) throw new IOException("Unexpected code " + response);
  84. String content = response.body().string();
  85. return content;
  86. } catch (IOException e) {
  87. // TODO Auto-generated catch block
  88. e.printStackTrace();
  89. return null;
  90. }
  91. }
  92. /**
  93. * 日期字符串格式转换
  94. * @param dateStr
  95. * @return
  96. */
  97. private Date parseStringToDate(String dateStr) {
  98. if (dateStr == null) {
  99. return new Date();
  100. }
  101. SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
  102. Date date = null;
  103. try {
  104. date = sdf.parse(dateStr);
  105. } catch (ParseException e) {
  106. // TODO Auto-generated catch block
  107. e.printStackTrace();
  108. }
  109. return date;
  110. }
  111. public static void main(String[] args) {
  112. Map<String,String> paramsMap=new HashMap<String,String>();
  113. paramsMap.put("institution_id", "44100001");
  114. String contentString = doPost("https://risserver3.pacsonline.cn/butt/getExam/butt/getExam", paramsMap);
  115. System.out.println(contentString);
  116. }
  117. }