蓄电池监控管理平台数据库初始化程序
DELL
2024-12-12 690f171908a15508049251d7489f8ce519b54a8f
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
package com.sql;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
 
import com.base.Com;
 
/**
 *     ´´½¨Êý¾Ý¿âÒÔ¼°±í¸ñ
 * @author ¾ü
 *
 */
public class Sql_Mysql 
{
    /**********************************************************************/
    public static final String DB_ALARM     = "`db_alarm`";
    public static final String DB_BATT         = "`db_batt`";
    public static final String DB_DIS_BATT     = "db_dis_batt";
    public static final String DB_POWER_RT     = "`db_power_rt`";
    public static final String DB_REAL_BATT = "`db_real_batt`";
    public static final String DB_PARAM     = "`db_param`";
    public static final String DB_POWER_ALARM     = "`db_power_alarm`";
    
    
    /**********************************************************************/
    public static final String Mon_Inf_Table = DB_BATT + ".mon_inf";
    public static final String Mon_Plan_Table = DB_BATT + ".mon_plan";
    public static final String Power_Inf_Table = DB_BATT + ".power_inf";
    public static final String Sinf_Pinf_Table = DB_BATT + ".sinf_pinf";
    public static final String Station_Inf_Table = DB_BATT + ".station_inf";
    
    
    /****************************** db_power_rt ****************************************/
    public static final String Power_Real_Rt1_Table = DB_POWER_RT + ".`tb_power_real_rt1`";        
    public static final String Power_Real_Rt2_Table = DB_POWER_RT + ".`tb_power_real_rt2`";
    public static final String Power_Real_Rt3_Table = DB_POWER_RT + ".`tb_power_real_rt3`";
    
    /******************************* db_dis_batt ***************************************/
    public static final String Batt_Test_Inf_Table = DB_DIS_BATT + ".batt_test_inf";                    //Ðîµç³ØÀúÊ·Êý¾Ý
    
    
    /******************************* db_real_batt ***************************************/
    public static final String Rt_Data_Table = DB_REAL_BATT + ".`rt_data`";                                //Ðîµç³Øµ¥Ìåʵʱ
    public static final String Rt_State_Table = DB_REAL_BATT + ".`rt_state`";                            //Ðîµç³Øµ¥Ìåʵʱ
 
    
    /******************************* db_param ***************************************/
    public static final String BattAlarm_Param_Table = DB_PARAM + ".battalarm_param";                    //µç³Ø¸æ¾¯²ÎÊý
    public static final String PowerAlarm_param_Table = DB_PARAM + ".poweralarm_param";                //µçÔ´¸æ¾¯²ÎÊý
 
    /*******************************  db_power_alarm ***************************************/
    public static final String Power_Alarm_Table = DB_POWER_ALARM + ".power_alarm";                            //µçԴʵʱ¸æ¾¯
    public static final String Power_Alarm_Cfg_Table = DB_POWER_ALARM + ".power_alarm_cfg";                    //µçÔ´¸æ¾¯ÅäÖñí
    
    /**********************************************************************/
    public static final String Batt_Alarm_Table = DB_ALARM + ".batt_alarm";                        //µçԴʵʱ¸æ¾¯·Ö±í
    
    /**********************************************************************/
    //--------------------------------------------------------------------------------------------//
    public Connection mysql_con;
    
    public Sql_Mysql(Connection conn)
    {
        mysql_con = conn;
    }
    
    /*
    public static Connection getConnection() throws SQLException, java.lang.ClassNotFoundException{
        String url = "jdbc:mysql://192.168.48.128:3306/studentinfo";
        Class.forName("com.mysql.jdbc.Driver");
        String userName = "root";
        String password = "lmx8688139";
        Connection con = DriverManager.getConnection(url,userName,password);
        return con;
    }
    */
    
    public void close_con()
    {
        try {
            if(null != mysql_con) {                
                mysql_con.close();
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    /**
     * ³õʼ»¯´´½¨Êý¾Ý¿â
     * @throws SQLException
     */
    public void checkAndCreateDB() throws SQLException
    {
        sqlMysqlExecute("CREATE SCHEMA IF NOT EXISTS " + DB_ALARM + " AUTHORIZATION sysdba");
        sqlMysqlExecute("CREATE SCHEMA IF NOT EXISTS " + DB_BATT + " AUTHORIZATION sysdba");
        sqlMysqlExecute("CREATE SCHEMA IF NOT EXISTS " + DB_DIS_BATT + " AUTHORIZATION sysdba");
        sqlMysqlExecute("CREATE SCHEMA IF NOT EXISTS " + DB_POWER_RT + " AUTHORIZATION sysdba");
        sqlMysqlExecute("CREATE SCHEMA IF NOT EXISTS " + DB_REAL_BATT + " AUTHORIZATION sysdba");
        sqlMysqlExecute("CREATE SCHEMA IF NOT EXISTS " + DB_PARAM + " AUTHORIZATION sysdba");
        sqlMysqlExecute("CREATE SCHEMA IF NOT EXISTS " + DB_POWER_ALARM + " AUTHORIZATION sysdba");
    }
    
    
    //ʹÓÃdbÊý¾Ý¿â
    public void sqlMysqlUseDB(String db) throws SQLException
    {
        sqlMysqlExecute("use " + db);
    }
    
    
    /**
     * ¼ì²étb±íÊÇ·ñ´æÔÚ
     * @param tb
     * @return
     * @throws SQLException
     */
    public boolean sqlMysqlCheckIfTableExist(String tb) throws SQLException
    {
        String sql_str = "SHOW TABLES LIKE '" + tb + "'";
        ResultSet res = sqlMysqlQuery(sql_str);
        boolean exist = false;
        while(res.next())
        {
            exist = true;
            break;
        }
        
        return exist;
    }
    
    /**
     *     ¼ì²étb±íÊÇ·ñ´æÔÚ
     * @param tb ±íÃû²»´øÊý¾Ý¿âÃû
     * @return true:´æÔÚ   false:²»´æÔÚ
     * @throws SQLException
     */
    public boolean sqlCheckIfTableExist(String tb)
    {
        String sql_str = " SELECT COUNT(*) as count FROM information_schema.TABLES " + 
                         " WHERE table_name ='" + tb + "';";
        ResultSet res = sqlMysqlQuery(sql_str);
        boolean exist = false;
        try {
            if (null != res && res.next()) {
                int count = res.getInt("count");
                if(count > 0) {
                    exist = true;
                }
            } 
        } catch (Exception e) {
            
        } finally{
            if(null != res) {
                try {
                    res.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }        
        return exist;
    }
    
 
    
    //Ö´ÐÐsqlÓï¾ä
    public void sqlMysqlExecute(String sql_str) throws SQLException
    {
        Statement sql = mysql_con.createStatement();
        sql.setQueryTimeout(5*60);
        sql.execute(sql_str);
    }
    //Ö´ÐÐsqlÓï¾ä
    public void sqlMysqlTotalExecute(String sql_str) throws SQLException
    {
        Statement sql = mysql_con.createStatement();
        sql.setQueryTimeout(5*60);
        sql.execute(sql_str);
    }
    
    
    //ÔÚÊÂÎïÖÐÖ´ÐжàÌõsqlÓï¾ä
    public boolean makeManualCommit(ArrayList<String> al_sql_strs) 
    {
        boolean exe_res = true;
        try {
            mysql_con.setAutoCommit(false);
            
            for(int n=0; n<al_sql_strs.size(); n++) {
                sqlMysqlExecute(al_sql_strs.get(n));
            }
            
            if(true == exe_res) {
                mysql_con.commit();
            }
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
            exe_res = false;
        } finally {
            try {
                if(false == exe_res) {
                    mysql_con.rollback();
                }
                mysql_con.setAutoCommit(true);
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        }        
        return exe_res;
    }
    
    //¸ù¾ÝsqlÓï¾äÖ´ÐÐsql²éѯÓï¾ä
    public ResultSet sqlMysqlQuery(String sql_str)
    {
        ResultSet res = null;
        try
        {
            Statement sql = mysql_con.createStatement();
            sql.setQueryTimeout(30);
            String query = sql_str;
            res =  sql.executeQuery(query);
        }
        catch(SQLException ex)
        {
            System.err.println("SQLException:" + ex.getMessage());
        }
        
        return res;
    }
    
    //¸ù¾ÝsqlÓï¾äÖ´ÐдóÅúÁ¿sql²éѯÓï¾ä
    public ResultSet sqlMysqlTotalQuery(String sql_str)
    {
        ResultSet res = null;
        try
        {
            Statement sql = mysql_con.createStatement();
            sql.setQueryTimeout(5*60);
            String query = sql_str;
            res =  sql.executeQuery(query);
        }
        catch(SQLException ex)
        {
            System.err.println("SQLException:" + ex.getMessage());
        }
        
        return res;
    }
    
    //¸ù¾ÝsqlÓï¾äÖ´ÐÐsql¸üÐÂÓï¾ä
    public void sqlMysqlUpdate(String sql_str)
    {
        try
        {
            Statement sql = mysql_con.createStatement();
            sql.setQueryTimeout(30);
            String query = sql_str;
            sql.execute(query);
        }
        catch(SQLException ex)
        {
            System.out.println("SQLException:" + ex.getMessage());
        }
    }
    
    
    
    
    //»ñȡд´½¨µÄ¼Ç¼µÄtestrecordcountµÄÖµ
    public int getBattTestRecordCountNew(int bg_id, String table)
    {
        int count = 0;
        boolean res_exe = true;
        String sql_str0 = "SELECT test_record_count_ex FROM " + table 
                            + " WHERE BattGroupId=" + bg_id  + " FOR UPDATE";
        String sql_str1 = "UPDATE " + table + " SET test_record_count_ex=test_record_count+1"
                            + " WHERE BattGroupId=" + bg_id;
        String sql_str2 = "SELECT MAX(test_record_count_ex) FROM " + table 
                            + " WHERE BattGroupId=" + bg_id;
        String sql_str3 = "INSERT INTO " + table + " (BattGroupId,test_record_count, test_record_count_ex) "
                            + " VALUES (" + bg_id + "," + 1 + "," + 1 + ")";
        try {
            mysql_con.setAutoCommit(false);
            ResultSet res = sqlMysqlQuery(sql_str0);
            if(res.next()) {
                sqlMysqlExecute(sql_str1);
                res = sqlMysqlQuery(sql_str2);
                if(res.next()) {
                    count = res.getInt(1);
                }
            } else {
                count = 1;
                sqlMysqlExecute(sql_str3);
            }
            
            mysql_con.commit();
        } catch (SQLException e) {
            e.printStackTrace();
            res_exe = false;
        } finally {
            if(false == res_exe) {
                try {
                    mysql_con.rollback();
                    mysql_con.setAutoCommit(true);
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
        }
        
        return count;
    }
}