Incoming Java objects automatically create dynamic tables and input data __java learning

Source: Internet
Author: User
Tags dateformat stringbuffer

See hibernate you give an object, he can dynamically create the name of the table specified in the configuration file, and then enter the data into the database, the original feeling is very magical, but, as if hibernate can not dynamic table to create tables and input data

I write a common class here, you give an object, tell me to generate the table by year or month, and tell me that the field is not needed to create in the table, the class can be dynamic table to create the required table, and input data

Attention:

Because of business needs, the support for fields here is only int and integer,double and double

There is also string, and for string unification, the field created for varchar (100)

The table unifies the creation of an ID self-added primary key

  Java code    package com.xd.nms.util;   import java.lang.reflect.field;    import java.text.simpledateformat;   import java.util.date;   import  java.util.map;   import java.util.set;   import org.apache.commons.logging.log;    import org.apache.commons.logging.logfactory;   import  org.springframework.jdbc.core.jdbctemplate;  /**   * @ Description   Insert data dynamically and create tables    *   @author   Trisukiang    *  @version  1.0   *  @since    */   public class commontableinsert {       public static log  logger = logfactory.getlog (commontableinsert.class);       /**        *  Entry method        *  @param   tablename  Table prefix   &NBSP;&Nbsp;   *  @param  dateFormat  formatting methods        * @ param obj  saved objects        *  @return         */        @SuppressWarnings ("unchecked")         Public static int insertobject (String tablename,string dateformat,object obj,  map nocol) {           int re = 0;                     try {                   JdbcTemplate jt =  (JdbcTemplate)  springfactory.getobject ("JdbcTemplate");                simpledateformat format  = new simpledaTeformat (DateFormat);                             String tname =  tablename +  "_"  + format.format (New date ());                if (Commondbutil.getalltablename (jt,tname)) { //  If there is a table                    re  = saveobj (jt,tname,obj, nocol); //  save data                    }else{                    re = createtable (Jt,tname,obj, &NBSP;NOCOL); //  Dynamic CREATE table                    &Nbsp;if (re == 1) {                        re = saveobj (Jt,tname,obj, noCol);  / /  Save Data                     }               }                           } catch  (exception e)  {                   logger.error ("Public method inserts data entry method error",  e);            }                      return re;           }       /**         *  Save method, Note that the name of the actual table passed here          */           @SuppressWarnings ("unchecked")        private static int  Saveobj (Jdbctemplate jt,string tablename,object obj, map nocol) {               int re = 0;            stringbuffer sb = new stringbuffer ("");           try{                                sb.append (" insert into "  + tableName +  "  (");                    map<string,string> map =  objectutil.getproandvalmap (obj); //  properties and property values for objects                 map<string,string> maptype = objectutil.getprotypemap (obj); //  object's property type list                 set<string> set = map.keyset ();                   for (string key : set) {                    //  If the column is not   Non-processed columns                     if (Nocol.get (key)  == null) {                &nbSp;       sb.append (key +  ",");                    }                }                   sb.append (" tableName )  ");                                 sb.append (" values  ( )";                   for (String key  : set) {                    if (Nocol.get (key)  == null) {              &nbsP;         if (Maptype.get (key). Equals ("class  Java.lang.String ") {                            sb.append ("'"  +  map.get (Key)  +  "',"));                        }else{                            sb.append ( Map.get (Key)  +  ",");                        }                    }                }                  sb.append (" '  + tableName + 

Related Article

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.