whyclxw
2020-12-28 ccab4911b9e8cedb15cef8845a96d7431ada1df7
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
package com.sql;
import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
 
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
 
import com.base.Com;
 
/**
 *     ´´½¨Êý¾Ý¿âÒÔ¼°±í¸ñ
 * @author lxw
 *
 */
public class Sql_Mysql 
{
    //--------------------------------------------------------------------------------------------//
    final public static String DB_MW_Motor = "`db_3.5mw_motor`";                                        //3.5mwʵʱÊý¾Ý¿â
    final public static String DB_MW_Motor_History = "`db_3.5mw_motor_history`";                        //3.5mwÀúʷʵʱÊý¾Ý¿â
    final public static String DB_AppSys = "`db_app_sys`";
    //--------------------------------------------------------------------------------------------//
    public final static String AppSys_Table = DB_AppSys + ".`tb_app_sys`";
    public final static String Tb_MW_Motor_inf = DB_MW_Motor + ".`tb_3.5mw_motor_inf`";
    public final static String TB_RECTIFIER_POWER_RT = DB_MW_Motor + ".`tb_rectifier_power_rt`";
    public final static String TB_RECTIFIER_POWER_CONTROL = DB_MW_Motor + ".`tb_rectifier_power_control`";
    //--------------------------------------------------------------------------------------------//
    public final static String Tb_RECTIFIER_POWER_RealData = DB_MW_Motor_History + ".tb_rectifier_power_";                
    
    //--------------------------------------------------------------------------------------------//
    
    public Logger logger = null;
    public Connection mysql_con;
    
    public Sql_Mysql(Connection conn)
    {
        mysql_con = conn;
        logger = LogManager.getLogger(this.getClass());
    }
    
    /*
    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 {
            mysql_con.close();
        } catch (SQLException e) {
            logger.error(e.toString(),e);
        }
    }
    
    
    //ʹÓÃ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;
    }
    
    //Ö´ÐÐsqlÓï¾ä
    public void sqlMysqlExecute(String sql_str) throws SQLException
    {
        Statement sql = mysql_con.createStatement();
        sql.setQueryTimeout(30);
        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) {
                // TODO Auto-generated catch block
                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 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());
        }
    }
    
 
    
    
    
    
    
    
    
    
    
    
    
}