Java uses JDBC Connection database to do JSON encapsulation and parsing examples

Source: Internet
Author: User
Tags id3


Java uses JDBC Connection database to do JSON encapsulation and parsing examples


This example is an example of using SOLServer2007 to connect a database with JDBC and to query the data format encapsulated into JSON.
Directly on the code--
Main function
Package Com.kuatang.jsondatas;


Import Java.io.File;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import java.util.LinkedList;
Import Java.util.regex.Matcher;
Import Java.util.regex.Pattern;


Import Net.sf.json.JSONObject;


Import com.kuatang.id3.ID3;


public class Main {



public static final String jsonshellbefor = "{";
public static final String jsonshellafter = "}";
public static final String Jsonlink = ":";
public static final String jsoninterval = ",";
public static final String Jsonstringsymbol = "\";



public static void Main (String args[]) {
/**
* JSON data encapsulation output, completed connection database query data
*/
JSON JSON = new JSON ();
Json. JSONOJECTSTR ()//test passed

Getjsondata ()//The JSON wrapper test with no database query passed

Getjsoncontent (1281, "66.3gp");/test Passed
/**
* Test the correctness of IP, through
*/
Isrightip ("192.168.1.101");
SYSTEM.OUT.PRINTLN ("-----------------*-----------------");
Isrightip ("11.168.1.101");
SYSTEM.OUT.PRINTLN ("-----------------* *-----------------");
Isrightip ("101");
SYSTEM.OUT.PRINTLN ("-----------------* * * *-----------------");
/**
* Test IP + port test pass
*/
Isrightipandport ("192.168.1.101");
System.out.println ("*************************************");
Isrightipandport ("192.168.1.101.6666");
System.out.println ("*************************************");
Isrightipandport ("192.168.1.101:7887");
System.out.println ("*************************************");

/**
* ID3 algorithm, decision tree test through
*/
ID3 Inst = new ID3 ();
Inst.readarff (New File ("E:\\myeclipse 2014\\id3_arff.txt"));
Inst.setdec ("Play");//string with play, because the combination of text, determine the calculation of the entropy of the positive and negative examples Yes and no
Linkedlist<integer> ll=new linkedlist<integer> ();
//
for (int i=0;i<inst.attribute.size (); i++) {
if (I!=inst.decatt)
Ll.add (i);
//        }
Arraylist<integer> al=new arraylist<integer> ();
for (int i=0;i<inst.data.size (); i++) {
Al.add (i);
//        }
INST.BUILDDT ("DecisionTree", "null", AL, LL);
Inst.writexml ("E:\\dtid3.xml");
}


public static Boolean Isrightip (String arg0) {
Regular expression: IP address
String Ipregex = "\\b (?! \\d\\d\\d) \\d+|1\\d\\d|2[0-4]\\d|25[0-5]) \. ((?! \\d\\d\\d) \\d+|1\\d\\d|2[0-4]\\d|25[0-5]) \. ((?! \\d\\d\\d) \\d+|1\\d\\d|2[0-4]\\d|25[0-5]) \. ((?! \\d\\d\\d) \\d+|1\\d\\d|2[0-4]\\d|25[0-5]) \\b ";
Pattern pattern = pattern.compile (Ipregex);
String s = "255.255.255.255";
Matcher Matcher = Pattern.matcher (arg0);
if (Matcher.matches ()) {
System.out.println (arg0 + "is the correct IP address.) ");
return true;
}
else{
System.out.println (arg0 + "is not the correct IP address.) ");
return false;
}
}
/*----------------------------------------------------------------------*/

public static Boolean Isrightipandport (String arg0) {


Regular expression: IP address + port number
String Ipandportregex = "\\b (?! \\d\\d\\d) \\d+|1\\d\\d|2[0-4]\\d|25[0-5]) \. ((?! \\d\\d\\d) \\d+|1\\d\\d|2[0-4]\\d|25[0-5]) \. ((?! \\d\\d\\d) \\d+|1\\d\\d|2[0-4]\\d|25[0-5]) \. ((?! \\d\\d\\d) \\d+|1\\d\\d|2[0-4]\\d|25[0-5]) \\:\\d{1,5}\\b ";
Pattern pattern = pattern.compile (Ipandportregex);
Matcher Matcher = Pattern.matcher (arg0);
if (Matcher.matches ()) {
System.out.println (arg0 +) is the correct ip+ port number address. ");
return true;
}
else{
System.out.println (arg0 +) is not the correct ip+ port number address. ");
return false;
}
}

public static String Getjsondata () {
String buffer = new string ();
String strbuffer = new string ();

hashmap<string, object> map = new hashmap<> ();

Map.put ("param", "3030.jpg;7873.jpg");
Map.put ("token", 0);
Map.put ("msg_id", 111);

Jsonobject jsonObject0 = jsonobject.fromobject (map);
System.out.println ("----------------test a-----------" + jsonObject0);
/*------The output above-the order is indeterminate------> *
/*---------------------Test a-----------{"token": 0, "param": "3030.jpg;7873.jpg", "msg_id": 111}*/

Jsonobject jsonobject = jsonobject.fromobject (map);
if (!buffer.equals ("")) {
Buffer = buffer + "," +jsonobject.tostring ();
}else{
Buffer = jsonobject.tostring ();
}
hashmap<string, object> map1 = new hashmap<> ();
Map1.put ("Param", "success");
Jsonobject JsonObject1 = Jsonobject.fromobject (MAP1);
if (!buffer.equals ("")) {
Buffer = buffer + "," +jsonobject1.tostring ();
}else{
Buffer = jsonobject1.tostring ();
}

hashmap<string, object> map2 = new hashmap<> ();
Map2.put ("token", 0);
Jsonobject jsonObject2 = Jsonobject.fromobject (MAP2);
if (!buffer.equals ("")) {
Buffer = buffer + "," +jsonobject2.tostring ();
}else{
Buffer = jsonobject2.tostring ();
}

SYSTEM.OUT.PRINTLN ("---------------buffer-------------->" + buffer);/ Test Result:---------------buffer-------------->{"msg_id": 111},{"param": "Success"},{"token": 0}

/*---------------------------------------------------*/
Entity Entity = new Entity ();
entity.msg_id = 111;
Entity.param = "Success";
Entity.token = 0;
Entity.type = "";
return buffer;

}

Class Entity {
public String param;
public int msg_id;
public int token;
public String type;
}

/*-------------------------------------------------------------*/
public static string getjsoncontent (int imsg_id, string strfilename) {
if (Mtokennumber = = Valid_tokennumber) {
System.out.print ("Your not create sessions to Ambadevice, please set correct!");
return null;
// }

String strjsoncontent;
Strjsoncontent = Getkeyvalue ("token", 0)
+ Jsoninterval + getkeyvalue ("msg_id", imsg_id)
+ Jsoninterval + getkeyvalue ("file", strFileName);
SYSTEM.OUT.PRINTLN ("-------need be deleted file is----author:apriltang---201605------" + Addjsonshell (strjsoncontent ));
Return Addjsonshell (strjsoncontent);
}//End





public static string Getkeyvalue (string strkey, int ivalue) {
Return Addstringsymbol (strkey) + Jsonlink + ivalue;
}

public static string Getkeyvalue (String strkey, long Ivalue) {
Return Addstringsymbol (strkey) + Jsonlink + ivalue;
}



static string Getkeyvalue (String strkey, string strvalue) {
Return Addstringsymbol (strkey) + Jsonlink + addstringsymbol (strvalue);
}

static string Addstringsymbol (String strkeyname) {
Return Jsonstringsymbol + strkeyname + jsonstringsymbol;
}



static string Addjsonshell (String strjsoncontent) {
Return jsonshellbefor + strjsoncontent + jsonshellafter;
}

}
——————————————————————————————————————————————————————————————————————————————————————————————————————
JSON class
Package Com.kuatang.jsondatas;


Import Net.sf.json.JSONObject;


/**
*
* @author Kuatang
* @description JSON data encapsulation
*
*/
public class JSON {

Private Connectdatabase database = null;
Private String jsonstr = "";
Private String SQL = "SELECT top * from Person.Address";
Public String Jsonojectstr () {

Database = new Connectdatabase ();
Jsonobject jsonobject = Jsonobject.fromobject (Database.setconnectdatabase (SQL));
Jsonstr = Jsonobject.tostring ();
Jsonstr = Database.setconnectdatabase (SQL);
System.out.println (JSONSTR);
return jsonstr;
}




}
——————————————————————————————————————————————————————————————————————————————————————————
Database jdbc:
Package Com.kuatang.jsondatas;


Import java.sql.Connection;
Import Java.sql.DriverManager;
Import Java.sql.ResultSet;
Import Java.sql.ResultSetMetaData;
Import Java.util.HashMap;
Import Java.util.Map;


Import Net.sf.json.JSONObject;


/**
*
* @author Kuatang
* @description Connection Database
* @user JDBC SELECT ...
* @time 2015-12-17
*
*/
public class Connectdatabase {

private static final String URL = "jdbc:sqlserver://127.0.0.1:1433;databasename=adventureworks;user=sa;password= 123456 ";
private static final String qu_dong= "Com.microsoft.sqlserver.jdbc.SQLServerDriver";
Private list<map<string, string>> List = null;
Private map<string, string> Map = null;
PRIVATE Connection conn = null;
Private java.sql.Statement state = null;
Private ResultSet rs = null;
Private String buffer = "";

public string Setconnectdatabase (string SQL) {


try{

Class.forName (Qu_dong);
conn = Drivermanager.getconnection (URL);
State = Conn.createstatement ();
rs = State.executequery (SQL);
Getting metadata for a result set
ResultSetMetaData RSMD = Rs.getmetadata ();
int datasize = Rsmd.getcolumncount ();
while (Rs.next ()) {

Map = new hashmap<string, string> ();
for (int i=1;i<=datasize;i++) {
String columnName = Rsmd.getcolumnname (i);
System.out.println (Rsmd.getcolumntypename (i));
if (Rsmd.getcolumntypename (i). Equals ("int")) {
int columnint = Rs.getint (columnName);
Map.put (ColumnName, string.valueof (Columnint));
}else{
String columnstr = rs.getstring (columnName);
if (columnstr==null| | Columnstr.equals ("")) {
Map.put ("", columnstr);
}else{
Map.put (ColumnName, COLUMNSTR);
}
}
}
Jsonobject jsonobject = jsonobject.fromobject (map);
if (!buffer.equals ("")) {
Buffer = buffer+ "," +jsonobject.tostring ();
}else{
Buffer = jsonobject.tostring ();
}
}

}catch (Exception e) {
E.printstacktrace ();
System.out.println (E.getmessage ());
}finally{
if (Rs!= null)
try {
Rs.close ();
catch (Exception e) {
}
if (state!= null)
try {
State.close ();
catch (Exception e) {
}
IF (conn!= null)
try {
Conn.close ();
catch (Exception e) {
}
}

return buffer;
}



}
————————————————————————————————————————————————————————————————————————————————————————
The corresponding jar pack please go to my csdn resources to download.
————————————————————————————————————————————————————————————————————————————————————-

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: info-contact@alibabacloud.com 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.