从mysql数据库获取的数据循环提交到接口
开发工具:idea 2017.2.5
数据库:mysql 5.5
1.第一步从mysql里面获取数据。
代码如下:
public class DBDate{
public List<Fbox> getDBDate() throws Exception{
List<Fbox> list = new ArrayList<Fbox>();
Connection conn=null;
try {
//加载驱动类
Class.forName("com.mysql.jdbc.Driver");
long start=System.currentTimeMillis(); //开始时间
//建立连接
conn= DriverManager.getConnection("jdbc:mysql://localhost:3306/test","root","123456"); //数据库地址,用户名,密码
long end = System.currentTimeMillis();
System.out.println(conn); //结束时间
System.out.println("连接耗时:"+(end - start)+"ms"); //结束时间-开始时间=耗时时间(ms)
//创建Statement对象
Statement stmt = conn.createStatement();
ResultSet rs = stmt.executeQuery("select * from zfbox"); //查询数据库的表的sql语句
System.out.println("namess\tgroupname\tvaluess"); //输出数据库的表结构参数
while (rs.next()) {
System.out.println(rs.getString(2)+"\t"+rs.getString(3)
+"\t"+rs.getString(4)); //输出数据库里面所有的查询的3个参数数据
//自定义对象
Fbox fbox = new Fbox(); //new了一个对象
fbox.setName(rs.getString(2)); // 参数放到对象里面
fbox.setGroupname(rs.getString(3));
fbox.setValue(rs.getString(4));
fbox.setType(rs.getString(5));
list.add(fbox); //赋好值的数据放到数组里面
}
}catch (SQLException e) {
e.printStackTrace();
}finally {
try {
if (conn != null) {
conn.close(); //关闭连接
}
}
catch (SQLException e) {
e.printStackTrace();
}
}
return list;
}
}
private String name; //监控点名称
private String groupname; //分组名称
private String value; //数值
private String type;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getGroupname() {
return groupname;
}
public void setGroupname(String groupname) {
this.groupname = groupname;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@Override
public String toString() {
return "Fbox{" +
"name='" + name + '\'' +
", groupname='" + groupname + '\'' +
", value='" + value + '\'' +
", type='" + type + '\'' +
'}';
}
这两个类就是连接数据库获取数据库表里面表的数据,再进行封装。
第二步就是封装数据进行循环提交。
public class Test {
private static String SEND_TP_URL = "http://fbox.flexem/v2/dmon/value"; //填写你要测试的接口地址
public static String send(String url, Object o) {
String json = new Gson().toJson( o );
String result = null;
try {
result = HttpRequest.post( url )
.timeout( 30000 )
.body( json, ContentType.JSON.toString() ).header("Authorization", "Bearer eyJhbGkwOTg2MywiaXNzIjoiaHR0cDovL2lkcy")//请求头这个参数接口有带没有就去掉
.header("Content-Type", "application/json")
.execute()
.body();
} catch (HttpException e) {
e.printStackTrace();
}
return result;
}
public static void sendfbox1() throws Exception {
DBDate dbDate = new DBDate();
List<Fbox> data = dbDate.getDBDate();
for (int i = 0; i < data.size(); i++) {
}
JSONArray jsonArray = JSONArray.fromObject(data); //list数组转成 JSONArray [{},{}]格式
System.out.println(jsonArray.toString());
JSONObject jsonObject = null;
for (int i = 0; i < jsonArray.size(); i++) { //遍历JSONArray大小
jsonObject = jsonArray.getJSONObject(i);
System.out.println(jsonObject);
}
for (int i = 0; i < jsonArray.size(); i++) {
long start = System.currentTimeMillis();
String ret = send(SEND_TP_URL, jsonArray.getJSONObject(i));
System.out.println(SEND_TP_URL);
System.out.println(jsonArray.getJSONObject(i));
System.out.println(ret);
long end = System.currentTimeMillis();
long time=end - start;
System.out.println("耗时:" +time+"ms");
SimpleDateFormat df = new SimpleDateFormat(" yyyy-MM-dd HH:mm:ss:SSS");//设置日期格式
System.out.println(df.format(new Date()));// new Date()为获取当前系统时间
}
}
public static void main(String[] args) {
for(int i = 0; i < 1; i++){
try {
sendfbox1();
}catch (Exception e){
e.printStackTrace();
}
}
}
}
接口根据数据库的数据循环提交参数,代码里面写每个接口提交的耗时时间。
demo我已经上传到我的资源里啦,想要下载的小伙伴可以去下载哟。
下载链接:https://download.csdn.net/download/zjj1213/12732997