Was forced by the boss to achieve the excle analysis algorithm of pivot table

Source: Internet
Author: User
Tags object object

At present, only two-dimensional double table can be implemented, the back function will continue to improve

Package Com.example.demo;

Import java.sql.SQLException;
Import java.util.ArrayList;
Import Java.util.HashMap;
Import Java.util.Iterator;
Import java.util.List;
Import Java.util.Map;

Import Org.junit.runner.RunWith;
Import org.springframework.test.context.ContextConfiguration;
Import Org.springframework.test.context.ContextHierarchy;
Import Org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
Import org.springframework.test.context.web.WebAppConfiguration;

Import Com.alibaba.fastjson.JSON;


@RunWith (Springjunit4classrunner.class)
@WebAppConfiguration
@ContextHierarchy ({@ContextConfiguration (locations = {"File:src/main/webapp/web-inf/applicationcontext.xml"}),
@ContextConfiguration (locations = {"File:src/main/webapp/web-inf/spring-mvc-servlet.xml"})})

public class Querytest {
public static void Main (string[] args) throws SQLException {
List L = new ArrayList ();

L.add (New Fielddefinitiondomain ("zz", "int", "money", "line"));
L.add (New Fielddefinitiondomain ("xx", "String", "date", "Row"));
L.add (New Fielddefinitiondomain ("zz", "String", "Company", "Row"));
String fieldjsonlist = json.tojsonstring (l);
Map FieldMap = new HashMap ();
list<fielddefinitiondomain> Parsearray = Json.parsearray (fieldjsonlist, Fielddefinitiondomain.class);
arraylist<string[]> strlist = new arraylist<> ();
list<string[]> intlist = new arraylist<> ();
String headfieldname = null;
String headtype = null;
String headtablename = null;
String headdirection = null;
for (Fielddefinitiondomain Fd:parsearray) {
String [] Strarray = new String[4];
Strarray[0] = Fd.gettablename ();
STRARRAY[1] = Fd.getfieldname ();
STRARRAY[2] = Fd.gettype ();
STRARRAY[3] = fd.getdirection ();
if (Fd.gettype (). Equals ("int") | | Fd.gettype (). Equals ("Long")) {
Intlist.add (Strarray);
}else {
Strlist.add (Strarray);
}
}
Headtablename = strlist.get (0) [0];
Headfieldname = strlist.get (0) [1];
Headtype = strlist.get (0) [2];
headdirection = strlist.get (0) [3];
For (string[] array:strlist) {
if (!array[1].equals (Headfieldname)) {
List arrayList = new ArrayList ();
Addlist (Arraylist,array);
Fieldmap.put (Array[0], arrayList);
}
}
list<integer> iilist = new ArrayList ();
For (string[] array:intlist) {
List List = (list) fieldmap.get (array[0]);
if (!notempty (list)) {
List arrayList = new ArrayList ();
Addlist (Arraylist,array);
Fieldmap.put (Array[0], arrayList);

}
(List) list.get (0)). Add (array[1]);//Field name
(List) List.get (1)). Add (array[2]);//field type
(List) List.get (2)). Add (array[3]);//Arrange (line or vertical)
Iilist.add ((List) List.get (2)). Size ());
}


List resultlist = new ArrayList ();
Tablesqlutils tablesqlutils = new Tablesqlutils ();
List List = Tablesqlutils.query (Headtablename, Headtype, headfieldname, NULL);
Map mm = new HashMap ();
List.foreach (Ll1->mm.put ((List) ll1). Get (0), New ArrayList ());
For (Object O2:fieldMap.keySet ()) {
List llist= (list) fieldmap.get (O2);
List fieldlist = (list) llist.get (0);
List typelist = (list) llist.get (1);
String fieldstr = headfieldname+ ",";
String typestr = headtype+ ",";
for (Object object:fieldlist) {
Fieldstr+=object+ ",";
}
for (Object object:typelist) {
Typestr+=object+ ",";
}
Fieldstr=fieldstr.substring (0,fieldstr.length ()-1);
Typestr=typestr.substring (0,typestr.length ()-1);
List query = Tablesqlutils.query ((String) O2, Typestr, Fieldstr,null);
Query.foreach (ll->{
List ll1 = (list) ll;
List ll2 = ((List) mm.get (ll1.get (0)));
if (ll2!=null)
Ll2.add (LL1);

});
Mm.put ("Numberindex", iilist);
System.out.println (mm);
}
}
private static void Addlist (List arrayList, string[] array) {
List arrayList2 = new ArrayList ();
List arrayList3 = new ArrayList ();
List arrayList4 = new ArrayList ();
Arraylist2.add (array[1]);
Arraylist3.add (array[2]);
Arraylist4.add (Array[3]);
Arraylist.add (ARRAYLIST2);
Arraylist.add (ARRAYLIST3);
Arraylist.add (ARRAYLIST4);
}
private static void sum (List <Long> list) {
Long sum = 0L;
if (Notempty (list)) {
for (int i = 0; i < list.size (); i++) {
Sum+=list.get (i);
}
List.add (sum);
}
}
public static Boolean Notempty (List list) {
if (List!=null&&list.size () >0) return true;
return false;
}
}

Package Com.example.demo;

Import java.sql.Connection;
Import Java.sql.DriverManager;
Import java.sql.PreparedStatement;
Import Java.sql.ResultSet;
Import java.sql.SQLException;
Import java.util.ArrayList;
Import Java.util.Date;
Import Java.util.HashMap;
Import java.util.List;
Import Java.util.Map;


public class Tablesqlutils {
Private String driver= "Com.mysql.jdbc.Driver";
Private String username= "Finance_uatuser";
Private String password= "E2guthk2u6y_g2o0ltqjgnetuwooqp/wy";
Private String url= "Jdbc:mysql://121.43.155.144:3306/finance_uat?useunicode=true&amp;characterencoding=utf-8 &amp;zerodatetimebehavior=converttonull ";
Public List query (String table,string type,string fieldname,string prefs) throws SQLException {
Connection Connection = null;
PreparedStatement preparestatement =null;
ResultSet rs = null;
Map m = new HashMap ();
Map map = new HashMap ();
string[] Fieldsplit = Fieldname.split (",");
List resultlist = new ArrayList ();
string[] types = Type.split (",");


if (fieldsplit.length>1) {
for (int i = 1; i < fieldsplit.length; i++) {
Map.put (Fieldsplit[i],new ArrayList ());
//}
}if (fieldsplit.length==1) {
for (int i = 0; i < fieldsplit.length; i++) {
Map.put (Fieldsplit[i],new ArrayList ());
//}
//}



try {
1. Get connection
Connection = getconnection ();
3. Prepare SQL
String sql = "Select";
for (String sf:fieldsplit) {
Sql+=sf+ ",";
}
Sql=sql.substring (0, Sql.length ()-1);
sql+= "from" +table;
if (fieldsplit.length>1)
sql+= "where" +fieldsplit[0]+ "=?";

preparestatement = connection.preparestatement (sql);
if (fieldsplit.length>1)
Preparestatement.setstring (1,prefs);
4. Execute the query and get resultset
rs = Preparestatement.executequery ();
5. Handling ResultSet
while (Rs.next ()) {
rs.get+ the corresponding type + in the database (the corresponding column alias in the database)
Switchtype (types,fieldsplit,rs,resultlist);
}
return resultlist;
} catch (Exception e) {
E.printstacktrace ();
return null;
}finally{
Rs.close ();
Preparestatement.close ();
Connection.close ();
}
}
private void Switchtype (string[] types, string[] field, ResultSet RS, List resultlist) throws SQLException {
List arr = new ArrayList ();
for (int i = 0; i < field.length; i++) {
Listadd (Rs,types[i],field[i], arr);
}
Resultlist.add (arr);

}

private void Listadd (ResultSet rs,string type, String field, List arr) throws SQLException {
Object o = null;
if (type.equals ("int")) {
o = (long) rs.getint (field);
}
if (Type.equals ("String")) {
o = rs.getstring (field);
}
if (Type.equals ("Long")) {
o = rs.getlong (field);
}
Arr.add (o);
}
Private Connection getconnection () throws ClassNotFoundException, SQLException {
Class.forName (driver);
Connection Connection = drivermanager.getconnection (URL, username, password);
return connection;

}
public static void Main (string[] args) throws SQLException {
Tablesqlutils tablesqlutils = new Tablesqlutils ();
List query = Tablesqlutils.query ("zz", "String,int", "Date,money", null);
Query.foreach (Str->system.out.println (str));
}
}

Was forced by the boss to achieve the excle analysis algorithm of pivot table

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.