蓄电池监控管理平台数据库初始化程序
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
package com.database_util;
 
import java.sql.ResultSet;
import java.sql.SQLException;
 
import com.sql.MysqlConnPool;
import com.sql.Sql_Mysql;
 
public class DB_Ram_db {
    
    public static void init(MysqlConnPool pool, boolean recreate) {
        createFBS9100s_fod_state(pool,recreate);        //´´½¨³ýÁò״̬±ítb_fbs9100s_fod_state    
        
        createFBS9100SetDLParam_Table(pool,recreate);    //´´½¨³ýÁò²ÎÊý±í
    }
    
    
    /**
     *     ´´½¨     web_site Êý¾Ý¿â
     * @param pool
     */
    public static void createDb_Ram_Db(MysqlConnPool pool) {
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        try {
            sql.sqlMysqlExecute("CREATE DATABASE IF NOT EXISTS " + Sql_Mysql.DB_RamDB);
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            sql.close_con();
        }
    }
        
    /**
     * ´´½¨³ýÁò״̬±í
     * @param pool
     * @param recreate
     */
    public static void createFBS9100s_fod_state(MysqlConnPool pool, boolean recreate) {
        String sql_str1 = " DROP TABLE IF EXISTS " + Sql_Mysql.FBS9100s_fod_state;
        String sql_str2 = "CREATE TABLE IF NOT EXISTS "+ Sql_Mysql.FBS9100s_fod_state +" (" + 
                "  num int(11) NOT NULL AUTO_INCREMENT," + 
                "  dev_id int(11) NOT NULL DEFAULT '0' COMMENT 'É豸id'," + 
                "  BattGroupId int(11) NOT NULL DEFAULT '0' COMMENT 'µç³Ø×éid'," + 
                "  record_time datetime NOT NULL DEFAULT '1970-01-01 00:00:00' COMMENT '¼Ç¼ʱ¼ä'," + 
                "  RestTime_one int(11) NOT NULL DEFAULT '0' COMMENT 'Ê£ÓàÌìÊý(×é1)'," + 
                "  WorkState_one int(11) NOT NULL DEFAULT '0' COMMENT '¹¤×÷ģʽ(×é1)'," + 
                "  VGroupVol_one double NOT NULL DEFAULT '0' COMMENT '×é¶Ëµçѹ(×é1)'," + 
                "  VpeakVol_one double NOT NULL DEFAULT '0' COMMENT '·åÖµµçѹ(×é1)'," + 
                "  RestTime_two int(11) NOT NULL DEFAULT '0' COMMENT 'Ê£ÓàÌìÊý(×é2)'," + 
                "  WorkState_two int(11) NOT NULL DEFAULT '0' COMMENT '¹¤×÷ģʽ(×é2)'," + 
                "  VGroupVol_two double NOT NULL DEFAULT '0' COMMENT '×é¶Ëµçѹ(×é2)'," + 
                "  VpeakVol_two double NOT NULL DEFAULT '0' COMMENT '·åÖµµçѹ(×é2)'," + 
                "  RestTime_three int(11) NOT NULL DEFAULT '0' COMMENT 'Ê£ÓàÌìÊý(×é3)'," + 
                "  WorkState_three int(11) NOT NULL DEFAULT '0' COMMENT '¹¤×÷ģʽ(×é3)'," + 
                "  VGroupVol_three double NOT NULL DEFAULT '0' COMMENT '×é¶Ëµçѹ(×é3)'," + 
                "  VpeakVol_three double NOT NULL DEFAULT '0' COMMENT '·åÖµµçѹ(×é3)'," + 
                "  RestTime_four int(11) NOT NULL DEFAULT '0' COMMENT 'Ê£ÓàÌìÊý(×é4)'," + 
                "  WorkState_four int(11) NOT NULL DEFAULT '0' COMMENT '¹¤×÷ģʽ(×é4)'," + 
                "  VGroupVol_four double NOT NULL DEFAULT '0' COMMENT '×é¶Ëµçѹ(×é4)'," + 
                "  VpeakVol_four double NOT NULL DEFAULT '0' COMMENT '·åÖµµçѹ(×é4)'," + 
                "  RestTime_five int(11) NOT NULL DEFAULT '0' COMMENT 'Ê£Óàʱ¼ä(×é5)'," + 
                "  WorkState_five int(11) NOT NULL DEFAULT '0' COMMENT '³ýÁò5״̬(×é5)'," + 
                "  VGroupVol_five int(11) NOT NULL DEFAULT '0' COMMENT '×é¶Ëµçѹ(×é5)'," + 
                "  VpeakVol_five int(11) NOT NULL DEFAULT '0' COMMENT '·åÖµµçѹ(×é5)'," + 
                "  PRIMARY KEY (num)," + 
                "  UNIQUE KEY unique_battgroupid (BattGroupId) USING BTREE" + 
                ") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;";
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        ResultSet res = null;
        try {
            if(recreate) {
                sql.sqlMysqlExecute(sql_str1);
            }
            sql.sqlMysqlExecute(sql_str2);
            
            //ÒÆ³ý֮ǰµÄÒÔÉ豸id·Ö×éµÄÉ豸idΨһË÷Òý
            res = sql.sqlMysqlQuery("show INDEX FROM "+Sql_Mysql.FBS9100s_fod_state+" WHERE KEY_name = 'unique_dev_id'");
            if(res.next()) {
                sql.sqlMysqlExecute("alter table "+Sql_Mysql.FBS9100s_fod_state+" drop index unique_dev_id;");
            }
            
            //Ìí¼ÓÊ£Óàʱ¼ä(×é5)
            res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
                    + " WHERE table_schema='db_ram_db'"
                    + " AND table_name='tb_fbs9100s_fod_state'"
                    + " AND column_name='RestTime_five'");
            if(false == res.next()) {
                sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.FBS9100s_fod_state 
                                + " ADD COLUMN RestTime_five int(11) NOT NULL DEFAULT '0' COMMENT 'Ê£Óàʱ¼ä(×é5)'");
            }
            //Ìí¼Ó³ýÁò״̬(×é5)
            res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
                    + " WHERE table_schema='db_ram_db'"
                    + " AND table_name='tb_fbs9100s_fod_state'"
                    + " AND column_name='WorkState_five'");
            if(false == res.next()) {
                sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.FBS9100s_fod_state 
                                + " ADD COLUMN WorkState_five int(11) NOT NULL DEFAULT '0' COMMENT '³ýÁò5״̬(×é5)'");
            }
            //Ìí¼Ó×é¶Ëµçѹ(×é5)
            res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
                    + " WHERE table_schema='db_ram_db'"
                    + " AND table_name='tb_fbs9100s_fod_state'"
                    + " AND column_name='VGroupVol_five'");
            if(false == res.next()) {
                sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.FBS9100s_fod_state 
                                + " ADD COLUMN VGroupVol_five int(11) NOT NULL DEFAULT '0' COMMENT '×é¶Ëµçѹ(×é5)'");
            }
            //Ìí¼Ó·åÖµµçѹ(×é5)
            res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
                    + " WHERE table_schema='db_ram_db'"
                    + " AND table_name='tb_fbs9100s_fod_state'"
                    + " AND column_name='VpeakVol_five'");
            if(false == res.next()) {
                sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.FBS9100s_fod_state 
                                + " ADD COLUMN VpeakVol_five int(11) NOT NULL DEFAULT '0' COMMENT '·åÖµµçѹ(×é5)'");
            }
            //Ìí¼Óµç³Ø×éid×Ö¶Î
            res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
                    + " WHERE table_schema='db_ram_db'"
                    + " AND table_name='tb_fbs9100s_fod_state'"
                    + " AND column_name='BattGroupId'");
            if(false == res.next()) {
                sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.FBS9100s_fod_state 
                                + " ADD COLUMN BattGroupId int(11) NOT NULL DEFAULT '0' COMMENT 'µç³Ø×éid' AFTER dev_id");
            }
            //Ìí¼Óµç³Ø×éidΪΨһË÷Òý
            res = sql.sqlMysqlQuery("show INDEX FROM "+Sql_Mysql.FBS9100s_fod_state+" WHERE KEY_name = 'unique_BattGroupId'");
            if(!res.next()) {
                //Ìí¼ÓΨһË÷ÒýǰÏÈɾ³ý±íÖмǼ
                sql.sqlMysqlExecute("DELETE FROM "+Sql_Mysql.FBS9100s_fod_state+" WHERE num>0");            
                sql.sqlMysqlExecute("alter table "+Sql_Mysql.FBS9100s_fod_state+" add UNIQUE `unique_BattGroupId`(`BattGroupId`);");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if(null != res) {
                try {
                    res.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            sql.close_con();
        }
    }
    
    /**
     * ´´½¨³ýÁò״̬±í
     * @param pool
     * @param recreate
     */
    public static void createFBS9100SetDLParam_Table(MysqlConnPool pool, boolean recreate) {
        String sql_str1 = " DROP TABLE IF EXISTS " + Sql_Mysql.FBS9100SetDLParam_Table;
        String sql_str2 = "CREATE TABLE IF NOT EXISTS "+ Sql_Mysql.FBS9100SetDLParam_Table +" (" + 
                "  num int(11) NOT NULL AUTO_INCREMENT," + 
                "  dev_id int(11) NOT NULL DEFAULT '0' COMMENT 'É豸id'," + 
                "  op_cmd int(11) NOT NULL DEFAULT '0'," + 
                "  test_cmd int(11) NOT NULL DEFAULT '0'," + 
                "  GroupNum int(11) NOT NULL DEFAULT '0' COMMENT 'Ä£¿é±àºÅ'," + 
                "  BattGroupNum int(11) NOT NULL DEFAULT '0' COMMENT 'µç³Ø×éºÅ'," + 
                "  STDCap_one int(11) NOT NULL DEFAULT '0' COMMENT '×é1±ê³ÆÈÝÁ¿'," + 
                "  FloatVol_one double NOT NULL DEFAULT '0' COMMENT '×é1¸¡³äµçѹ'," + 
                "  Strength_one double NOT NULL DEFAULT '0' COMMENT '×é1³ýÁòÇ¿¶È'," + 
                "  YHStrength_one double NOT NULL DEFAULT '0' COMMENT '×é1Ñø»¤Ç¿¶È'," + 
                "  TimeLong_one int(11) NOT NULL DEFAULT '0' COMMENT '×é1³ýÁòÌìÊý'," + 
                "  STDCap_two int(11) NOT NULL DEFAULT '0' COMMENT '×é2±ê³ÆÈÝÁ¿'," + 
                "  FloatVol_two double NOT NULL DEFAULT '0' COMMENT '×é2¸¡³äµçѹ'," + 
                "  Strength_two double NOT NULL DEFAULT '0' COMMENT '×é2³ýÁòÇ¿¶È'," + 
                "  YHStrength_two double NOT NULL DEFAULT '0' COMMENT '×é2Ñø»¤Ç¿¶È'," + 
                "  TimeLong_two int(11) NOT NULL DEFAULT '0' COMMENT '×é2³ýÁòÌìÊý'," + 
                "  STDCap_three int(11) NOT NULL DEFAULT '0' COMMENT '×é3±ê³ÆÈÝÁ¿'," + 
                "  FloatVol_three double NOT NULL DEFAULT '0' COMMENT '×é3¸¡³äµçѹ'," + 
                "  Strength_three double NOT NULL DEFAULT '0' COMMENT '×é3³ýÁòÇ¿¶È'," + 
                "  YHStrength_three double NOT NULL DEFAULT '0' COMMENT '×é3Ñø»¤Ç¿¶È'," + 
                "  TimeLong_three int(11) NOT NULL DEFAULT '0' COMMENT '×é3³ýÁòÌìÊý'," + 
                "  STDCap_four int(11) NOT NULL DEFAULT '0' COMMENT '×é4±ê³ÆÈÝÁ¿'," + 
                "  FloatVol_four double NOT NULL DEFAULT '0' COMMENT '×é4¸¡³äµçѹ'," + 
                "  Strength_four double NOT NULL DEFAULT '0' COMMENT '×é4³ýÁòÇ¿¶È'," + 
                "  YHStrength_four double NOT NULL DEFAULT '0' COMMENT '×é4Ñø»¤Ç¿¶È'," + 
                "  TimeLong_four int(11) NOT NULL DEFAULT '0' COMMENT '×é4³ýÁòÌìÊý'," + 
                "  PRIMARY KEY (num)," + 
                "  UNIQUE KEY unique_dev_id (dev_id)" + 
                ") ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8;";
        Sql_Mysql sql = new Sql_Mysql(pool.getConn());
        ResultSet res = null;
        try {
            if(recreate) {
                sql.sqlMysqlExecute(sql_str1);
            }
            sql.sqlMysqlExecute(sql_str2);            
            //Ìí¼Óµç³Ø×é±àºÅ×Ö¶Î
            res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
                    + " WHERE table_schema='db_ram_db'"
                    + " AND table_name='tb_fbs9100s_fod_param'"
                    + " AND column_name='BattGroupNum'");
            if(false == res.next()) {
                sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.FBS9100SetDLParam_Table 
                                + " ADD COLUMN BattGroupNum int(11) NOT NULL DEFAULT '0' COMMENT 'µç³Ø×é×éºÅ'");
            }
            //Ìí¼Ó±ê³ÆÈÝÁ¿(×é5)
            res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
                    + " WHERE table_schema='db_ram_db'"
                    + " AND table_name='tb_fbs9100s_fod_param'"
                    + " AND column_name='STDCap_five'");
            if(false == res.next()) {
                sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.FBS9100SetDLParam_Table 
                                + " ADD COLUMN STDCap_five int(11) NOT NULL DEFAULT '0' COMMENT '×é5±ê³ÆÈÝÁ¿'");
            }
            //Ìí¼Ó¸¡³äµçѹ(×é5)
            res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
                    + " WHERE table_schema='db_ram_db'"
                    + " AND table_name='tb_fbs9100s_fod_param'"
                    + " AND column_name='FloatVol_five'");
            if(false == res.next()) {
                sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.FBS9100SetDLParam_Table 
                                + " ADD COLUMN FloatVol_five double NOT NULL DEFAULT '0' COMMENT '×é5¸¡³äµçѹ'");
            }
            //Ìí¼Ó³ýÁòÇ¿¶È(×é5)
            res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
                    + " WHERE table_schema='db_ram_db'"
                    + " AND table_name='tb_fbs9100s_fod_param'"
                    + " AND column_name='Strength_five'");
            if(false == res.next()) {
                sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.FBS9100SetDLParam_Table 
                                + " ADD COLUMN Strength_five double NOT NULL DEFAULT '0' COMMENT '×é5³ýÁòÇ¿¶È'");
            }
            //Ìí¼ÓÑø»¤Ç¿¶È(×é5)
            res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
                    + " WHERE table_schema='db_ram_db'"
                    + " AND table_name='tb_fbs9100s_fod_param'"
                    + " AND column_name='YHStrength_five'");
            if(false == res.next()) {
                sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.FBS9100SetDLParam_Table 
                                + " ADD COLUMN YHStrength_five double NOT NULL DEFAULT '0' COMMENT '×é5Ñø»¤Ç¿¶È'");
            }
            //Ìí¼ÓÊ£Óàʱ¼ä(×é5)
            res = sql.sqlMysqlQuery("SELECT * FROM information_schema.columns"
                    + " WHERE table_schema='db_ram_db'"
                    + " AND table_name='tb_fbs9100s_fod_param'"
                    + " AND column_name='TimeLong_five'");
            if(false == res.next()) {
                sql.sqlMysqlExecute("ALTER TABLE " + Sql_Mysql.FBS9100SetDLParam_Table 
                                + " ADD COLUMN TimeLong_five int(11) NOT NULL DEFAULT '0' COMMENT '×é5³ýÁòÌìÊý'");
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            if(null != res) {
                try {
                    res.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            sql.close_con();
        }
    }
}