Administrator
2021-01-19 fbed4c7738db69c0d6a30c10f6944268ba28ea9e
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
196
197
198
199
package com.backup;
 
import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Date;
 
import com.base.Com;
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 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){
            //System.err.println(dbpath);
            File file = new File(dbpath);
            if(file.exists()){
                System.out.println("数据库的安装目录:"+dbpath);
            }else{
                //根据主程序在注册表中的参数获取mysql的安装目录
                dbpath = getMySQLPathFromReg();
                System.out.println(dbpath);
                if(dbpath != null && dbpath.length()>0){
                    file = new File(dbpath);
                    if(file.exists()){
                        System.out.println("数据库的安装目录:"+dbpath);
                    }else {
                        System.err.println("未获取到数据库的安装目录     at  " + Com.getDateTimeFormat(new Date(), Com.DTF_YMDhms));
                    }
                }
            }
        }
    }
 
    /**
     *    备份数据库表到指定的文件目录
     * @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"                 //根据数据库中的mysqldump备份数据库
                + 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";
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally{
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return path;
    }
    
    
    
    /**
     *     根据主程序在注册表中的目录获取mysql的安装目录
     * @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;
    }
    
    /**
     *     根据在注册表中的指定的key值和注册表中的路径值获取指定的参数
     * @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(),"gbk");  
            String line;  
            BufferedReader ir = new BufferedReader(in);
            int index = 0;
            while ((line = ir.readLine()) != null) {  
                //System.out.println(line);
                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) throws UnsupportedEncodingException {
        MysqlConnPool pool = new MysqlConnPool("127.0.0.1",3360,5);
        
        System.out.println(new MyRunTime(pool));        
    }
}