一、Java操作MySQL
1.插入insert
1 import java.sql.*; 2 import java.util.UUID; 3 4 public class BigData { 5 public static void main(String[]args) { 6 try{ 7 Class.forName("com.mysql.cj.jdbc.Driver"); 8 System.out.println("驱动加载成功"); 9 String url="jdbc:mysql://10.10.10.10:12900/scene_micro_sit"; 10 String user="scene"; 11 String password="Scene"; 12 //3、获取连接 13 Connection conn = DriverManager.getConnection(url, user, password); 14 15 //----------------------对数据库进行操作------------------- 16 //记录开始时间 17 Long begin=System.currentTimeMillis(); 18 System.out.println("开始计时"); 19 //-----------插入数据---------- 20 //sql语句 21 String sqlPre="insert into scene_edge (id,mac,ip,uuid,device_type,app_version,status,online,last_online_time,create_time,modify_time) values (?,?,?,?,?,?,?,?,?,?,?)"; 22 //使用PrepareStatement 23 PreparedStatement pstate = conn.prepareStatement(sqlPre); 24 25 for(int a=0;a<600;a++){ 26 //生成随机uuid 27 String uuid= UUID.randomUUID().toString(); 28 pstate.setInt(1,a); 29 pstate.setString(2,"00:90:27:e4:84:1c"); 30 pstate.setString(3,"10.8.214.1"); 31 pstate.setString(4,uuid); 32 pstate.setInt(5,1); 33 pstate.setString(6,"1.0.0.200915"); 34 pstate.setInt(7,1); 35 pstate.setInt(8,0); 36 pstate.setString(9,"2020-09-21 10:57:11"); 37 pstate.setString(10,"2020-09-21 10:57:11"); 38 pstate.setString(11,"2020-09-21 10:57:11"); 39 pstate.executeUpdate(); 40 } 41 //循环完毕,关闭连接 42 pstate.close(); 43 conn.close(); 44 //结束时间 45 Long end = System.currentTimeMillis(); 46 System.out.println("600条数据,插入数据库耗时:"+(end-begin)+"ms"); 47 }catch (ClassNotFoundException e){ 48 e.printStackTrace(); 49 }catch (SQLException e){ 50 e.printStackTrace(); 51 } 52 } 53 }
2.更新update
import java.sql.*; import java.util.UUID; public class Bangding { public static void main(String[]args) { try{ Class.forName("com.mysql.cj.jdbc.Driver"); System.out.println("驱动加载成功"); String url="jdbc:mysql://10.10.10.10:12900/bvc_scece_api_sit"; String user="scece"; String password="scece"; //3、获取连接 Connection conn = DriverManager.getConnection(url, user, password); //----------------------对数据库进行操作------------------- //记录开始时间 Long begin=System.currentTimeMillis(); System.out.println("开始计时"); //-----------插入数据---------- //sql语句 String sqlPre="UPDATE scene_edge set scene_id=(SELECT id from scene_info WHERE name=?) WHERE id=?"; //使用PrepareStatement PreparedStatement pstate = conn.prepareStatement(sqlPre); for(int a=4965;a<5834;a+=2){ int i=0; String b=String.valueOf(a); pstate.setString(1,"最旺"+b+"案场"); pstate.setInt(2,i); pstate.executeUpdate(); i++; } //循环完毕,关闭连接 pstate.close(); conn.close(); //结束时间 Long end = System.currentTimeMillis(); System.out.println("绑定案场,操作数据库耗时:"+(end-begin)+"ms"); }catch (ClassNotFoundException e){ e.printStackTrace(); }catch (SQLException e){ e.printStackTrace(); } } }
3.查询select
1 import java.sql.*; 2 import java.util.UUID; 3 4 public class Test { 5 public static final String DRIVER = "com.mysql.cj.jdbc.Driver"; 6 public static final String URL = "jdbc:mysql://10.10.10.10:12900/bvc_scece_api_sit"; 7 public static final String USERNAME = "scece"; 8 public static final String PASSWORD = "scece"; 9 public static void main(String[] args) throws SQLException, ClassNotFoundException { 10 Connection conn = DriverManager.getConnection(URL,USERNAME,PASSWORD); //每一个Connection对象表示一个数据库连接对象 11 Class.forName(DRIVER);//加载驱动程序 12 for(int a=4965;a<5834;a+=2){ 13 String sql = "SELECT id from scene_info WHERE name=?";//查询语句 14 String sqlPre="UPDATE scene_edge set scene_id=? WHERE id=?"; 15 PreparedStatement pstate = conn.prepareStatement(sql); 16 String b=String.valueOf(a); 17 pstate.setString(1,"最旺"+b+"案场"); 18 pstate.executeQuery(); 19 Statement stat = conn.createStatement(); 20 ResultSet rs=stat.executeQuery(sql);//查询 21 while (rs.next()) { 22 String deptno=rs.getString("id"); 23 System.out.println("id:"+deptno); 24 } 25 } 26 } 27 }
4.调用接口
1 import org.json.JSONException; 2 import org.json.JSONObject; 3 4 import java.io.BufferedReader; 5 import java.io.DataOutputStream; 6 import java.io.IOException; 7 import java.io.InputStreamReader; 8 import java.net.HttpURLConnection; 9 import java.net.URL; 10 11 12 public class jiekouchannel { 13 public static final String address="https://111.111.com/env-101/por-22779/scenesit/v1/third/subscribe"; 14 15 public static void login() { 16 for (int i = 11067; i < 100000; i++) { 17 try { 18 URL url = new URL(address); 19 HttpURLConnection connection = (HttpURLConnection) url.openConnection(); 20 connection.setDoOutput(true); 21 connection.setDoInput(true); 22 connection.setRequestMethod("POST"); 23 connection.setUseCaches(false); 24 connection.setInstanceFollowRedirects(true); 25 26 //设置请求头 27 connection.setRequestProperty("Content-Type", "application/json"); 28 connection.setRequestProperty("apikey", "ZFTTI6kFB0oYzhhxRL31ko4OFSS"); 29 connection.connect(); 30 31 //POST请求,包装成json数据 32 DataOutputStream out = new DataOutputStream(connection.getOutputStream()); 33 JSONObject obj = new JSONObject(); 34 obj.put("room_no", "AHHN" + i); 35 obj.put("customer_id", "32"); 36 obj.put("system_app", "04"); 37 obj.put("system_step", "01"); 38 obj.put("project_code", "AHHN"); 39 obj.put("room_address", "2323"); 40 obj.put("customer_name", "wang0521"); 41 obj.put("customer_phone", "13790447113"); 42 obj.put("customer_id_card", "421182199202111157"); 43 obj.put("customer_sale_time", "2020-10-14 08:57:42"); 44 obj.put("time", "2020-10-20 11:25:24"); 45 obj.put("customer_face_image", "data:image/jpg;base64,iVBORw0KG"); 46 47 out.writeBytes(obj.toString()); 48 out.flush(); 49 out.close(); 50 51 //读取响应 52 BufferedReader reder = new BufferedReader(new InputStreamReader(connection.getInputStream())); 53 String lines; 54 StringBuffer sb = new StringBuffer(""); 55 while (((lines = reder.readLine()) != null)) { 56 lines = new String(lines.getBytes(), "utf-8"); 57 sb.append(lines); 58 } 59 System.out.println(sb); 60 61 reder.close(); 62 63 //断开连接 64 connection.disconnect(); 65 66 } catch (IOException e) { 67 e.printStackTrace(); 68 } catch (JSONException e) { 69 e.printStackTrace(); 70 } 71 72 } 73 } 74 public static void main(String[]args){ 75 login(); 76 77 } 78 }
1 import okhttp3.*; 2 import java.io.IOException; 3 4 public class Jiekou{ 5 public static void main(String[]args) throws IOException { 6 OkHttpClient client = new OkHttpClient().newBuilder() 7 .build(); 8 MediaType mediaType = MediaType.parse("application/json"); 9 RequestBody body = RequestBody.create(mediaType, "{ "pm": "1308602115271294976", "processCode": "YJSSCN2020092300007" }"); 10 Request request = new Request.Builder() 11 .url("https://111.111.com.cn/env-101/por-22779/scenesit/v1/third/processInitiate") 12 .method("POST", body) 13 .addHeader("apikey", "ZFTTI6kFB0oYzHQ3IjhhxRL31OFSS") 14 .addHeader("Content-Type", "application/json") 15 .build(); 16 Response response = client.newCall(request).execute(); 17 System.out.println(response.body().string()); 18 } 19 }