zhanghaijian
2018-07-03 06580708bdc661873cbc2dfd6de8b3155f57b8ae
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
package ay.db;
 
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.UUID;
import ay.util.SecurityEncryptUtil;
 
public class DbUtil_mysql {
    
    /**
     * 用sql转库
     * @param conn_ppas
     * @param conn_mysql
     * @param sql_insert
     * @param tableName
     * @param tfc_id
     * @param ay_id
     * @param out_field
     * @param del_ay_id
     * @param is_leave
     */
    
    public void insert(Connection conn_ppas,Connection conn_mysql,String tableName,String sql_select,String sql_insert,String sql_del) {
        
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        System.out.println("/*****************************************"+String.format("%-20s", tableName)+"**************************************************************/");
        System.out.println("begin:"+df.format(new Date()));
        
//        sql_select=sql_select+" and rownum<10 ";
        
        System.out.println("\n---------sql_select:  "+sql_select);
                        
        int ii=0;
 
        StringBuffer sb = new StringBuffer();
        String s_name = "", s_type = "";
        try {    
            Statement st_mysql = conn_mysql.createStatement();
            Statement st_ppas = conn_ppas.createStatement();
            ResultSet rs = st_ppas.executeQuery(sql_select);
            if(sql_del!=null)st_mysql.executeUpdate(sql_del);
 
            ii=0;
            while (rs.next()) {
                ii=ii+1;
                sb = new StringBuffer();                
                sb.append(sql_insert+" values(");
                
                for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
//                    s_type = rs.getMetaData().getColumnTypeName(i).toUpperCase();
                    s_type = rs.getMetaData().getColumnTypeName(i).toLowerCase();
//                    System.out.println(s_type);
//                    System.out.println(rs.getObject(i));
                    if (rs.getObject(i) == null) {
                        sb.append("null,");
                    } else {
                        String columnNameString = rs.getMetaData().getColumnName(i);
//                        System.out.println(columnNameString);
                        if (s_type.equals("varchar")) {
                            sb.append("'" + ((String)rs.getObject(i)).replaceAll("\'", "’") + "',");
                        }
                        if (s_type.equals("datetime")) {
                            sb.append("'" + rs.getObject(i) + "',");
                        }
                        if (s_type.equals("numeric")|| s_type.equals("int")) {
                            sb.append(rs.getObject(i) + ",");
                        }
                        if (!s_type.equals("varchar")
                                && !s_type.equals("datetime")
                                && !s_type.equals("numeric")
                                && !s_type.equals("int")
                                ) {                                
                                sb.append("null,");
                        }
                        
                    }
                }
//                System.out.println("delete from "+tableName+" where id = '"+sb.substring(sb.indexOf("(", sb.indexOf("values"))+1, sb.indexOf(",", sb.indexOf("values"))).trim()+"'");
//                st_mysql.executeUpdate("delete from "+tableName+" where id = '"+sb.substring(sb.indexOf("(", sb.indexOf("values"))+1, sb.indexOf(",", sb.indexOf("values"))).trim()+"'");
                
//                System.out.println(sb.substring(0, sb.length() - 1) + ")");
//                st_mysql.executeUpdate(sb.substring(0, sb.length() - 1) + ")");
                    
                
                st_mysql.addBatch(sb.substring(0, sb.length() - 1) + ")");                    
                if(ii%1000==0){
                    st_mysql.executeBatch();
                    System.out.println(ii);
                }    
                    
                
            }            
            
            st_mysql.executeBatch();
            
            rs.close();
            st_ppas.close();
 
            st_mysql.close();            
            
            System.out.println("end:"+df.format(new Date())+"     "+ii );
 
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
    
    
    public ResultSet query(Connection con, String sql){
        Statement statement = null;
        ResultSet resultSet = null;
        try {
            statement = con.createStatement();
            resultSet = statement.executeQuery(sql);
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return resultSet;
    }
 
    
    public void update(Connection con, String sql){
        Statement statement = null;
        try {
//            System.out.println("\n=====update  sql: "+sql);
            statement = con.createStatement();
            statement.executeUpdate(sql);            
        } catch (SQLException e) {
//            Scanner reader=new Scanner(System.in);
//            System.out.println("输入任意字符继续回车继续");
//            String str = reader.next();
//            if(str.equals("Y")){
//                update(con, sql);
//            }
            e.printStackTrace();
        }
    }
}