Dynamic SQL splicing, select, insert the wording

 1 import java.util.Iterator;
 2 import java.util.Map;
 3 import java.util.Set;
 4 
 5 public class SQLUtil {
 6     public static String sqlSelect = "";
 7 
 8     public static String selectQuerySQL(Map<String, Object> map, String tableName) {
 9         if (map.isEmpty() || map.size() == 0) {
10             sqlSelect = "select*from" + tableName;
11         } else {
12             sqlSelect = "select*from" + tableName + "where";
13             Set set = map.keySet();
14             Iterator iterator = set.iterator();
15             int index = 0;
16             while (iterator.hasNext()) {
17                 String key = (String) iterator.next();
18                 Object value = map.get(key);
19                 if (value instanceof String) {
20                     value = "'" + value + "'";
21                 }
22                 index++;
23                 if ((index == 1 && map.size() == 1) || map.size() == index) {
24                     sqlSelect += key + "=" + value + " ";
25                 } else {
26                     sqlSelect += key + "=" + value + " " + "and" + " ";
27                 }
28             }
29         }
30         System.out.println("待执行的select SQL:"+sqlSelect);
31         return sqlSelect;
32     }
33     public static String insertSQL(Map<String,Object>map){
34         Iterator<String> iterator = map.keySet().iterator();
35         StringBuilder sqlTable=new StringBuilder("insert into");
36         StringBuilder sqlKey=new StringBuilder("(");
37         StringBuilder sqlVaule= new StringBuilder("value(");
38         while (iterator.hasNext()){
39             String key=iterator.next();
40             if(map.get(key).toString()==null||map.get(key).toString().length()==0){
41                 iterator.remove();
42             }else {
43                 if (key.equals("tableName")){
44                     sqlTable.append(map.get(key));
45                 }else {
46                     sqlKey.append(key+",");
47                     sqlVaule.append(map.get(key)+",");
48                 }
49             }
50         }
51         sqlTable.append (sqlKey.toString (). substring ( 0, sqlKey.toString (). lastIndexOf ( ",")) + ")"). append (sqlVaule.toString (). substring (0, sqlVaule.lastIndexOf ( " , ")) +") " );
 52 is          String SQL = sqlTable.toString ();
 53 is          System.out.println (" INSERT the SQL to be executed: "+ SQL);
 54 is          return SQL;
 55      }
 56 is      / * this is use of the above method, can be ignored * / 
57 is      / * public static void main (String [] args) {
 58          // call to select the method
 59          the Map <String, Object> = new new Map the HashMap <> ();
 60          map.put ( "field name", "specific value");
61         String tableName="表名";
62 is          System.out.println (selectQuerySQL (Map, tableName) .toString ());
 63 is          // call to the insert method
 64          the Map <String, Object> = new new Map the HashMap <> ();
 65          map.put ( "field name "" particular value ");
 66          map.put (" tableName "," add here the last table name ");
 67          String SQL = INSERT SQL (the Map);
 68          PreparedStatement Statment = Connection.prepareStatement (SQL);
 69          Statment. execute (sql); // this is a SQL statement is executed by the need to give JDBCUtil in Connection Connection
 70      } * / 
71 is }

 

Guess you like

Origin www.cnblogs.com/wangquanyi/p/11329035.html