The interface is implemented via HTTP to implement the operation of inserting remote database data into local data.

Source: Internet
Author: User

Today, my colleague gave me an interface that allows me to get information from remote data to be plugged into a local database in the form of HTTP.

(1) A simple method is to pass parameters through Ajax and then request data in the background.

(2) Get the data through an HTTP request and insert it into a local database.

Implementation ideas (the first is also possible but if you use the first, you need to interact with the page, and finally choose the second method)

(1) Get the value by writing HTTP.

(2) parsing data in JSON format

(3) then connect the database to implement the data insert operation.

The business code is as follows:

Package com.message.transfer;

Import Com.message.transfer.po.Order;
Import Com.message.transfer.util.Jdbcdemo;

Import Net.sf.json.JSONArray;
Import Net.sf.json.JSONObject;

public class Tranfermain {
* Calling the other interface method
* Path provided by @param path or third party
* @param data sent to each other or third parties, in most cases send JSON data to the other side to resolve
public static void Interfaceutil (String path,string data) {
try {
URL url = new URL (path);
Opening and linking between URLs
HttpURLConnection conn = (httpurlconnection) url.openconnection ();
PrintWriter out = null;
Request method
Conn.setrequestmethod ("POST");
String encoding = "UTF-8";
Conn.setrequestproperty ("Content-type", "Application/json; charset= "+ encoding);
To set common request properties
Conn.setrequestproperty ("Accept", "*/*");
Conn.setrequestproperty ("Connection", "keep-alive");
Conn.setrequestproperty ("User-agent", "mozilla/4.0" (compatible; MSIE 6.0; Windows NT 5.1; SV1) ");
Sets whether to output to httpurlconnection, sets whether to read from HttpURLConnection, and also sends the POST request to set both
The most common HTTP requests are just get and post,get requests to get static pages, or you can put parameters behind URL strings and pass them on to the servlet.
The difference between post and get is that the post parameter is not placed inside the URL string, but is placed inside the body of the HTTP request.
Conn.setdooutput (TRUE);
Conn.setdoinput (TRUE);
Gets the output stream corresponding to the URLConnection object
out = new PrintWriter (Conn.getoutputstream ());
Out.write (data);
Send request parameters i.e. data
Out.print (data);
Buffered data
Out.flush ();
Gets the input stream corresponding to the URLConnection object
InputStream is = Conn.getinputstream ();
Constructs a character stream cache
BufferedReader br = new BufferedReader (new InputStreamReader (IS));
The buffer stream is set here, and performance is degraded if string is used.
StringBuffer bf = new StringBuffer ();
String line = null;
while (line = Br.readline ()) = null) {
/* This gets a string of strings, parses the JSON, and builds an entity class. Then set it in.
* Then use JDBC to insert into the database.
* */
Bf.append (line);
Get all the JSON data.
String str = bf.tostring ();
Converting string data to JSON objects
Jsonobject jsonobject = Jsonobject.fromobject (str);
String datastr= jsonobject.get ("Data"). ToString ();
Jsonobject datajsonobject = Jsonobject.fromobject (DATASTR);
String strlist = Datajsonobject.get ("Billlist"). ToString ();
This converts the string into an array of JSON objects.
Jsonarray Jsonarray=jsonarray.fromobject (strlist);
for (int i=0;i<jsonarray.size (); i++) {
Iterate through an array of objects, and then take out the objects in the array whose properties are billdtllist.
Jsonobject job = Jsonarray.getjsonobject (i);
String billdtlstr = Job.get ("Billdtllist"). ToString ();
Billdtllist is an array of objects that convert a string into an array of objects.
Jsonarray Jsonarraybilldtlstr=jsonarray.fromobject (BILLDTLSTR);
for (int j=0;j<jsonarraybilldtlstr.size (); j + +) {
Jsonobject dtljob = Jsonarraybilldtlstr.getjsonobject (j);
Place the value of the entity class in the corresponding property of the object.
Order stu= (Order) Jsonobject.tobean (Dtljob, Order.class);
Inserts an object into the database.
Jdbcdemo.insert (Stu);
Close the stream
Is.close ();
Disconnect, preferably written on, disconnect is cut off when the underlying TCP socket link is idle. It is not cut off if it is being used by another thread.
Fixed multi-threaded words, if not disconnect, the link will increase, until the message is not sent out. Write the disconnect after the normal some.
Conn.disconnect ();
SYSTEM.OUT.PRINTLN ("complete End");
} catch (Exception e) {
E.printstacktrace ();

public static void Main (string[] args) {
String senddata = "{\" code\ ": \" 101475\ ", \" billcode\ ": \" \ ", \" starttime\ ": \" 2018-8-1 24:00:00\ ", \" endtime\ ": \" 2018-8-2 24:00:00\ "}";
Interfaceutil ("URL address", senddata);


The interface is implemented via HTTP to implement the operation of inserting remote database data into local data.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.