whyclxw
2024-04-18 0782678b7344ec48706c5a7e9de63f558e3356a2
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.table_sql;
 
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;
 
import com.mchange.v2.c3p0.ComboPooledDataSource;
 
public class MySqlPool {
    private ComboPooledDataSource mysql_ds = new ComboPooledDataSource();//Êý¾ÝÔ´±»¹Ø±Õºó²»ÄÜÔÙµ÷ÓÃgetconn£¨£©·½·¨
    private int mSqlPort = 3360;
    //private String mSqlIp="118.89.139.230";
    //private String mSqlIp="127.0.0.1";
    private String mSqlIp="192.168.10.79";
    private String user="root";
    private String upassword="lmx8688139";
    private int conncount_max=200;
    private int conncount_min=2;
    private int MaxStatements=50;
    private int MaxIdleTime=60;
    private int InitialPoolSize=2;
    
    public ComboPooledDataSource getMysql_ds() {
        return mysql_ds;
    }
    public MySqlPool() {
        //System.out.println("mSqlIp:"+mSqlIp);
        
        try {
            //Thread.sleep(1000*60);
            mysql_ds.setDriverClass("com.mysql.jdbc.Driver");
            mysql_ds.setJdbcUrl("jdbc:mysql://" + mSqlIp + ":" + mSqlPort);
            mysql_ds.setUser(user);
            mysql_ds.setPassword(upassword);
            mysql_ds.setMaxPoolSize(conncount_max);
            mysql_ds.setMinPoolSize(conncount_min);
            mysql_ds.setIdleConnectionTestPeriod(60);
            // ÉèÖÃÁ¬½Ó³ØÖеÄ×î´óStatementsÊýÁ¿£¡  
            mysql_ds.setMaxStatements(MaxStatements);  
            // ÉèÖÃÁ¬½Ó³ØµÄ×î´ó¿ÕÏÐʱ¼ä£¡  
            mysql_ds.setMaxIdleTime(MaxIdleTime); 
             // ÉèÖóõʼÁ¬½Ó³ØµÄ´óС£¡  
            mysql_ds.setInitialPoolSize(InitialPoolSize);
            //System.out.println("11111111111111111111111111");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    public Connection getConn(){
        Connection con = null;
        try {
            con=mysql_ds.getConnection();
        } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return con;
    }
    
    public static boolean makeManualCommit(Connection mysql_con,ArrayList<String> al_sql_strs) 
    {
        boolean exe_res = true;
        try {
            mysql_con.setAutoCommit(false);
            
            for(int n=0; n<al_sql_strs.size(); n++) {
                if(true == exe_res) {
                    exe_res = executeCreateTableNoclose(mysql_con,al_sql_strs.get(n));
                } else {
                    break;
                }
            }
            
            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();
            }finally {
                if(mysql_con!=null)
                    try {
                        mysql_con.close();
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
            }
        }
        
        return exe_res;
    }
    //Ö´ÐÐsqlÓï¾äwith no colse
      public static boolean executeCreateTableNoclose(Connection mysql_con,String sql_str)
      {  
          PreparedStatement ps=null;
      
          boolean rest = true;
          try
          {
              ps = mysql_con.prepareStatement(sql_str);
              ps.setQueryTimeout(600);
              ps.execute();
          }
          catch(SQLException ex)
          {
              ex.printStackTrace();
              rest = false;
              
          }finally {
              try {
                ps.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
          return rest;
      }
 
    //¸ù¾ÝsqlÓï¾äÖ´ÐÐsql²éѯÓï¾ä
    public ResultSet sqlMysqlQuery(Connection mysql_con,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 boolean sqlMysqlUpdate(Connection mysql_con,String sql_str)
    {   boolean bl=true;
        Statement sql=null;
        try
        {
            sql= mysql_con.createStatement();
            sql.setQueryTimeout(30);
            String query = sql_str;
            bl=sql.execute(query);
        }
        catch(SQLException ex)
        {   
            bl=false;
            System.out.println("SQLException:" + ex.getMessage());
        }finally {
            try {
                if(sql!=null)sql.close();
                if(mysql_con!=null)mysql_con.close();
            } catch (SQLException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
        return bl;
    }
    //¹Ø±ÕÁ¬½Ó
    public void close_con(Connection mysql_con)
    {
        try {
            mysql_con.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
    
    public static void main(String[] args) {
        MySqlPool mPool=new MySqlPool();
        Connection conn=mPool.getConn();
        System.out.println("conn:"+conn);
        String sql="show databases";
        //boolean bl=mPool.executeUpdate(sql, conn);
        
        
    }
}