General methods for saving records

Source: Internet
Author: User

In JadePool, to facilitate user input, ProcessVO provides two general methods for saving records. 1. public int save (String tableName, Map <String, Object> mapRecord) throws SQLException 2. public Object saveOne (String tableName, Map <String, Object> mapRecord) the throws SQLException save method returns the return value of the standard JDBC insert or update operation, that is, 1 is returned for successful insertion or update, 0 is returned for failure, and the saveOne method is based on actual needs, returns the primary key value of the inserted or updated record. Both common methods call the underlying _ preparedStatementInsert and _ preparedStatementUpdate core methods. The basic idea is: if the record exists, update it. If the record does not exist, insert it. If the value of the first primary key is null | "", a new primary key value is automatically inserted. This method is not suitable for inserting tables with multiple primary keys, however, updating tables with multiple primary keys is not affected. The source code of the save method is as follows: [java]/*** save a record. * If the record exists, update it. If the record does not exist, insert it. If the value of the first primary key is null | "", a new primary key value is automatically inserted. This method is not suitable for inserting tables with multiple primary keys, however, updating tables with multiple primary keys does not affect ** @ param tableName is the table name * @ param mapRecord is the data of a record to be inserted into the table. The key name is the same as the field name, order independence; * @ return returns the status value returned by the Standard update Method * @ throws SQLException */@ Override public int save (String tableName, Map <String, Object> mapRecord) throws SQLException {int num = 0; String _ w = ""; String [] keys = db. getKeys (tableName); if (keys. length = 0) {return this. in Sert (tableName, mapRecord); // a table without a primary key can be directly inserted.} else {Object kv = mapRecord. get (keys [0]. toString (); if (kv = null | "". equals (kv) {num = insert (tableName, mapRecord, true); // The table has a primary key, but the record does not include the primary key record | the primary key record value is null | if the primary key value is "", the record is inserted and the primary key is automatically inserted.} else {Object [] recordFields = mapRecord. keySet (). toArray (); Map _ key_m = new LinkedHashMap (); for (int I = 0; I <recordFields. length; I ++) {if (tool. isInFields (keys, record Fields [I]. toString () {_ key_m.put (recordFields [I]. toString (), mapRecord. get (recordFields [I]. toString (); // extract the primary key field} if (! _ Key_m.isEmpty () {Object [] _ k = _ key_m.keySet (). toArray (); if (_ k. length! = Keys. length) {return num;} else {Field f = db. getField (tableName, _ k [0]. toString (); if (f. getTypeClassName (). equals ("java. lang. string ") {_ w =" where "+ _ k [0] +" like '"+ mapRecord. get (_ k [0]. toString () + "'";} else {_ w = "where" + _ k [0] + "=" + mapRecord. get (_ k [0]. toString ();} if (_ k. length> 1) {for (int I = 1; I <_ k. length; I ++) {f = db. getField (tableName, _ k [I]. toString ()); If (f. getTypeClassName (). equals ("java. lang. string ") {_ w = _ w +" and "+ _ k [I] +" like '"+ mapRecord. get (_ k [I]. toString () + "'";} else {_ w = _ w + "and" + _ k [I] + "=" + mapRecord. get (_ k [I]. toString () ;}} Map rm = this. queryOne ("select" + _ k [0] + "from" + tableName + _ w); if (rm. isEmpty () {num = this. insert (tableName, mapRecord); // insert original data} else {num = this. update (tableName, m ApRecord, _ w); // original local update }}} return num;} the source code of the saveOne method is as follows: [java] view plaincopyprint? /*** Save a record. * If the record exists, update it. If the record does not exist, insert it. If the value of the first primary key is null | "", a new primary key value is automatically inserted. This method is not suitable for inserting tables with multiple primary keys, however, updating tables with multiple primary keys does not affect ** @ param tableName is the table name * @ param mapRecord is the data of a record to be inserted into the table. The key name is the same as the field name, order independence; * @ return returns the first primary key value. [Note: The original return value of int type update is changed to the first primary key value, which will be more convenient for application] * @ throws SQLException */@ Override public Object saveOne (String tableName, map <String, Object> mapRecord) throws SQLException {Object firstKeyValue = null; int n = 0; String _ w = ""; String [] keys = db. getKeys (tableName); String [] fields = db. getFields (tableName); if (keys. length = 0) {n = this. insert (tableName, mapRecord); // a table without a primary key can be inserted directly if (n> 0) {firstKe YValue = mapRecord. get (fields [0]);} // if no primary key exists, the first field value} else {firstKeyValue = mapRecord is returned. get (keys [0]. toString (); if (firstKeyValue! = Null &! "". Equals (firstKeyValue) {Object [] recordFields = mapRecord. keySet (). toArray (); Map _ key_m = new LinkedHashMap (); for (int I = 0; I <recordFields. length; I ++) {if (tool. isInFields (keys, recordFields [I]. toString () {_ key_m.put (recordFields [I]. toString (), mapRecord. get (recordFields [I]. toString (); // extract the primary key field} if (! _ Key_m.isEmpty () {Object [] _ k = _ key_m.keySet (). toArray (); if (_ k. length! = Keys. length) {return null; // If there are multiple primary keys and the record lacks multiple primary keys, do not save} else {Field f = db. getField (tableName, _ k [0]. toString (); if (f. getTypeClassName (). equals ("java. lang. string ") {_ w =" where "+ _ k [0] +" like '"+ mapRecord. get (_ k [0]. toString () + "'";} else {_ w = "where" + _ k [0] + "=" + mapRecord. get (_ k [0]. toString ();} if (_ k. length> 1) {for (int I = 1; I <_ k. length; I ++) {f = db. getField (ta BleName, _ k [I]. toString (); if (f. getTypeClassName (). equals ("java. lang. string ") {_ w = _ w +" and "+ _ k [I] +" like '"+ mapRecord. get (_ k [I]. toString () + "'";} else {_ w = _ w + "and" + _ k [I] + "=" + mapRecord. get (_ k [I]. toString () ;}} Map rm = this. queryOne ("select" + _ k [0] + "from" + tableName + _ w); if (rm. isEmpty () {n = this. insert (tableName, mapRecord); // insert original data} else {n = th Is. update (tableName, mapRecord, _ w); // original local update }}} else {// if (firstKeyValue = null | "". equals (firstKeyValue) Field f = db. getField (tableName, keys [0]); String className = f. getTypeClassName (); if (className. equals ("java. lang. long ") {firstKeyValue = insertKey (tableName); mapRecord. put (keys [0], firstKeyValue); n = update (tableName, mapRecord); // The table has a primary key, but the record does not include the primary key record | the primary key record value is null | if the primary key value is "", the record is inserted and automatically inserted. Input primary key} if (className. equals ("java. lang. integer ") | className. equals ("java. lang. short ") | className. equals ("java. lang. float ") | className. equals ("java. lang. double ") | className. equals ("java. lang. string ") {firstKeyValue = insertAutoKey (tableName); mapRecord. put (keys [0], firstKeyValue); n = update (tableName, mapRecord); // The table has a primary key, but the record does not include the primary key record | the primary key record value is null | if the primary key value is "", the record is inserted and the primary key is automatically inserted.} return firstKeyValue;} Application: [java] Map m = ...; // one record, Jade j = new Jade (); Object keyValue = j. saveOne (tableName, m); // or int num = j. save (tableName, m); j. commit (); when saving multiple records, you can follow these methods: [java] List <Map> list = ...; // a set of records Jade j = new Jade (); int num = 0; for (Map m: list) {num = num + j. save (tableName, m);} j. commit (); Note: Currently there is no way to save multiple records. Using this method to save multiple records is less efficient. In the future, we will provide an efficient method to save multiple records.

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.