whyclj
2019-02-19 76ad368d8977602e6d8de689ddc7c3d595fb173b
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
package com.backup;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import com.sql.MysqlConnPool;
 
public class MyRunTime {
    private String dbpath = "";
    private String serverip = "127.0.0.1";
    private String sqlport = "3360";
    private String uname = "root";
    private String upass = "lmx8688139";
    //private String backFilePath = "D:/temp/a.sql";
    private MysqlConnPool pool;
    
    private String regedit_code = "HKEY_LOCAL_MACHINE\\SYSTEM\\CurrentControlSet\\Services\\BMS_FBSDEV\\Parameters";            //Ö÷³ÌÐòÔÚ×¢²á±íÖеÄÏà¶ÔλÖÃ
    private final static String TYPE = "REG_SZ";                            //×¢²á±íÖвÎÊýµÄÀàÐÍ
    public MyRunTime(MysqlConnPool conn_pool){
        this.pool = conn_pool;                
        initParam();
    }
    
    /**
     * »ñÈ¡Êý¾Ý¿âµÄ°²×°Ä¿Â¼
     */
    private void initParam() {
        //´ÓÊý¾Ý¿âÖлñÈ¡mysqlµÄ°²×°Ä¿Â¼
        dbpath = getMysqlPath();
        if(dbpath != null && dbpath.length() > 0){
            File file = new File(dbpath);
            if(file.exists()){
                System.out.println(file.getAbsolutePath());
            }else{
                //´Ó×¢²á±íÖлñÈ¡Êý¾Ý¿âµÄ°²×°Ä¿Â¼
                dbpath = getMySQLPathFromReg();
                if(dbpath != null && dbpath.length()>0){
                    file = new File(dbpath);
                    if(file.exists()){
                        System.out.println(file.getAbsolutePath());
                    }
                }
            }
        }
    }
 
    /**
     * Ö´ÐÐÖ¸¶¨µÄCode
     * @param code
     * @return
     */
    public boolean exec(String dbname,String tbname,String backFilePath){
        boolean flag = true;
        Runtime runt = Runtime.getRuntime();    
        String code = dbpath+"/" + "mysqldump -h "+serverip   
                + " -P" + sqlport + " -u" + uname + " -p"                 //µÚ¶þ¸ö-pºóÃæ²»ÄÜÓпոñ£¬·ñÔò½«±»ÈÏΪÊÇÊý¾Ý¿âµÄÃû³Æ  
                + upass + " --result-file=" + backFilePath+"/"+tbname+".sql" 
                + " --opt -Q -R --skip-lock-tables --default-character-set=utf8 " + dbname +" "+tbname ;
        try {
            Process proc = runt.exec(code);    
            int tag = proc.waitFor();      // µÈ´ý½ø³ÌÖÕÖ¹     
            flag = flag && (tag == 0);
        } catch (IOException e) {
            e.printStackTrace();
            flag = false;
        } catch (InterruptedException e) {
            flag = false;
            e.printStackTrace();
        }        
        return flag;
    }
    
    
    /**
     * ²éѯÊý¾Ý¿âÃû°²×°Â·¾¶(¶ÔÓÚ°²×°Ä¿Â¼Öаüº¬ÖÐÎĵÄÇé¿öÏÂʧ°Ü)
     * @param conn
     * @return
     */
    public String getMysqlPath(){
        String sql="select @@basedir as basePath from dual";
        PreparedStatement ps = null;
        ResultSet rs = null;
        String path="";
        Connection conn = null;
        try {
            conn = this.pool.getConn();
            ps = conn.prepareStatement(sql);
            rs=ps.executeQuery();
            if(rs.next()){
                path=rs.getString("basePath");
            }
            if(path.endsWith("\\") || path.endsWith("/")){
                path += "bin";
            }else{
                path += "/bin";
            }
            System.out.println(path);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally{
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return path;
    }
    
    
    
    /**
     * ´Ó×¢²á±íÖлñȡƴ½ÓÊý¾Ý¿âµÄ°²×°Ä¿Â¼
     * @return
     */
    public String getMySQLPathFromReg(){
        String result = "";
        String path = getRegeditParam(regedit_code,"AppDirectory");
        if(path != null && path.length() > 0){
            File f = new File(path);
            File ParentFile = f.getParentFile();
            File[] files = ParentFile.listFiles();
            for(int i=0;i<files.length;i++){
                if(files[i].isDirectory()){
                    String fileString = files[i].getName().toUpperCase();
                    if(fileString.indexOf("MYSQL") >= 0){
                        result = files[i].getAbsolutePath();    
                        if(result.endsWith("\\") || result.endsWith("/")){
                            result += "bin";
                        }else{
                            result += "/bin";
                        }
                        return result;
                    }
                }
            }
        }
        return result;
    }
    
    /**
     * »ñȡָ¶¨×¢²á±íϵIJÎÊýÖµ
     * @param path
     * @param key
     * @return
     */
    public static String getRegeditParam(String path,String key){
        String commond = " reg query " + path;
        String result = "";
        InputStreamReader in = null;
        try {
            Process ps = null;  
            ps = Runtime.getRuntime().exec(commond);
            ps.getOutputStream().close();  
            in = new InputStreamReader(ps.getInputStream());  
            String line;  
            BufferedReader ir = new BufferedReader(in);
            int index = 0;
            while ((line = ir.readLine()) != null) {  
                if(line.indexOf(key) >= 0 && (index = line.indexOf(TYPE)) >= 0){
                    result = line.substring(index + TYPE.length()).trim();
                }  
            }  
            return result;
        } catch (IOException e) { 
            e.printStackTrace();  
        } finally{
            if(in != null){
                try {
                    in.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }     
    
    
    public static void main(String[] args) {
        //MysqlConnPool pool = new MysqlConnPool("127.0.0.1",3360,5);
        //MyRunTime my = new MyRunTime(pool);
        //my.exec("db_alarm", "tb_devalarm_data");
        
        //System.out.println(new MyRunTime(null).getMySQLPathFromReg());
        
    }
}