一区二区三区在线-一区二区三区亚洲视频-一区二区三区亚洲-一区二区三区午夜-一区二区三区四区在线视频-一区二区三区四区在线免费观看

服務器之家:專注于服務器技術及軟件下載分享
分類導航

PHP教程|ASP.NET教程|JAVA教程|ASP教程|

服務器之家 - 編程語言 - JAVA教程 - Java實現Dbhelper支持大數據增刪改

Java實現Dbhelper支持大數據增刪改

2020-03-25 13:28邱慕夏 JAVA教程

這篇文章主要介紹了Java實現Dbhelper支持大數據增刪改功能的實現過程,感興趣的小伙伴們可以參考一下

在做項目的時候,技術選型很重要,在底層的方法直接影響了我們對大數據訪問以及修改的速度,在Java中有很多優秀的ORM框架,比如說:JPA,Hibernate 等等,正如我們所說的,框架有框架的好處,當然也存在一些可以改進的地方,這個時候,就需要我們針對于不同的業務不同的需求,不同的訪問量,對底層的架構重新封裝,來支持大數據增刪改。

代碼:

?
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
319
320
321
322
import java.io.*;
import java.sql.*;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
 
import javax.servlet.jsp.jstl.sql.*;
 
/**
 * DbHelper
 * @author qmx
 *
 */
public class Dbhelper {
   
private String sql;  //要傳入的sql語句  
public void setSql(String sql) {
  this.sql = sql;
}
  
private List sqlValues; //sql語句的參數
public void setSqlValues(List sqlValues) {
  this.sqlValues = sqlValues;
}
 
 
private List<List> sqlValue; //sql語句的參數
public void setSqlValue(List<List> sqlValues) {
  this.sqlValue = sqlValues;
}
 
private Connection con; //連接對象
  public void setCon(Connection con) {
  this.con = con;
}
 
  public Dbhelper(){
    this.con=getConnection(); //給Connection的對象賦初值
  }
   
  /**
   * 獲取數據庫連接
   * @return
   */
  private Connection getConnection(){ 
  
    String driver_class=null;
    String driver_url=null;
    String database_user=null;
    String database_password=null;
    try {
      InputStream fis=this.getClass().getResourceAsStream("/db.properties"); //加載數據庫配置文件到內存中
      Properties p=new Properties();
      p.load(fis);
       
      driver_class=p.getProperty("driver_class");   //獲取數據庫配置文件
      driver_url=p.getProperty("driver_url");
      database_user=p.getProperty("database_user");
      database_password=p.getProperty("database_password");
   
       
      Class.forName(driver_class);
      con=DriverManager.getConnection(driver_url,database_user,database_password);
       
       
    } catch (ClassNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }
    return con;
  }
   
  
   
 
  /**
   * 關閉數據庫
   * @param con
   * @param pst
   * @param rst
   */
  private void closeAll(Connection con,PreparedStatement pst,ResultSet rst){
    if(rst!=null){
      try {
        rst.close();
      } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
     
    if(pst!=null){
      try {
        pst.close();
      } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
     
    if(con!=null){
      try {
        con.close();
      } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
     
     
  }
   
 
  /**
   * 關閉數據庫
   * @param con
   * @param pst
   * @param rst
   */
  private void closeAll(Connection con,Statement pst,ResultSet rst){
    if(rst!=null){
      try {
        rst.close();
      } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
     
    if(pst!=null){
      try {
        pst.close();
      } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
     
    if(con!=null){
      try {
        con.close();
      } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
     
     
  }
   
  /**
   * 查找
   * @param sql
   * @param sqlValues
   * @return
   */
  public Result executeQuery(){
    Result result=null;
    ResultSet rst=null;
    PreparedStatement pst=null;
    try {
     
      pst=con.prepareStatement(sql);
      if(sqlValues!=null&&sqlValues.size()>0){ //當sql語句中存在占位符時
        setSqlValues(pst,sqlValues);
      }
    rst=pst.executeQuery();
    result=ResultSupport.toResult(rst); //一定要在關閉數據庫之前完成轉換
       
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }finally{
      this.closeAll(con, pst, rst);
    }
     
    return result;
  }
   
   
  /**
   * 增刪改
   * @return
   */
  public int executeUpdate(){
    int result=-1;
    PreparedStatement pst=null;
    try {
      pst=con.prepareStatement(sql);
      if(sqlValues!=null&&sqlValues.size()>0){ //當sql語句中存在占位符時
        setSqlValues(pst,sqlValues);
      }
    result=pst.executeUpdate();
    } catch (SQLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
    }finally{
      this.closeAll(con, pst, null);
    }      
    return result;
  }
   
   
  /**
   * 使用PreparedStatement加批量的方法
   * @return
   */
  public int[] executeUpdateMore(){  
    int[] result=null;   
    try{  
      PreparedStatement prest =con.prepareStatement(sql,ResultSet.TYPE_SCROLL_SENSITIVE,ResultSet.CONCUR_READ_ONLY);
       for(List sqlValueString : sqlValue){      
         for(int i=0;i<sqlValueString.size();i++){
          try {
            prest.setObject(i+1,sqlValueString.get(i));
          } catch (SQLException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
          }                    
        }
        prest.addBatch();
       }
       prest.executeBatch(); 
     /*  con.commit();*/  
       this.closeAll(con, prest, null);
    } catch (SQLException ex){  
     Logger.getLogger(Dbhelper.class.getName()).log(Level.SEVERE, null,ex);  
    
    return result;
     
  
   
  /**
   * 使用PreparedStatement加批量的方法,strvalue:
   * "INSERT INTOadlogs(ip,website,yyyymmdd,hour,object_id) VALUES('192.168.1.3','localhost','20081009',8,'23123')"
   * @return
   * @throws SQLException
   */
  public int[] executeUpdateMoreNotAuto() throws SQLException{   
    int[] result =null;
    con.setAutoCommit(false);  
    Statement stmt =con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,  
                      ResultSet.CONCUR_READ_ONLY);  
    String[] SqlString= null;
    for(String strvalue : SqlString){        
      stmt.execute(strvalue);  
    }  
    con.commit(); 
    return result;
  }
   
   
   
  /**
   * 使用PreparedStatement加批量的方法,strvalue:
   * "INSERT INTOadlogs(ip,website,yyyymmdd,hour,object_id) VALUES('192.168.1.3','localhost','20081009',8,'23123')"
   * @return
   * @throws SQLException
   */
  public int[] executeMoreNotAuto() throws SQLException{   
    //保存當前自動提交模式
    Boolean booleanautoCommit=false;
    String[] SqlString= null;
    int[] result= null;
     try
     {
      booleanautoCommit=con.getAutoCommit();
       //關閉自動提交
      con.setAutoCommit(false);
      Statement stmt =con.createStatement(ResultSet.TYPE_SCROLL_SENSITIVE,  
          ResultSet.CONCUR_READ_ONLY); 
      //使用Statement同時收集多條sql語句
      /*stmt.addBatch(insert_sql1);
      stmt.addBatch(insert_sql2);
      stmt.addBatch(update_sql3);*/
      for(String strvalue : SqlString){        
        stmt.addBatch(strvalue);  
      
       
       //同時提交所有的sql語句
       stmt.executeBatch();
       //提交修改
       con.commit();
       con.setAutoCommit(booleanautoCommit);
       this.closeAll(con, stmt, null);
     }
     catch(Exception e)
     {
      e.printStackTrace();
      con.rollback();  //設定setAutoCommit(false)沒有在catch中進行Connection的rollBack操作,操作的表就會被鎖住,造成數據庫死鎖
     }
     return result;
  }
   
   
   
  /**
   * 給sql語句中的占位符賦值
   * @param pst
   * @param sqlValues
   */
  private void setSqlValues(PreparedStatement pst,List sqlValues){
    for(int i=0;i<sqlValues.size();i++){
      try {
        pst.setObject(i+1,sqlValues.get(i));
      } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
      }
    }
  }
     
}

我們的在db.properties中寫入訪問數據庫的信息:

?
1
2
3
4
driver_class=com.mysql.jdbc.Driver
driver_url=jdbc:mysql://192.168.22.246:3306/importexceltest
database_user=basic
database_password=basic

測試:

?
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
import java.util.*;
 
public class ImportExcelTest {
  public static void main(String[] args){
   
    /*Dbhelper db = new Dbhelper();
    String sql = "insert into tb_coursetype(id,courseTypeName) values('2012003','qmx3')";
    db.setSql(sql);
    db.executeUpdate();*/
     
    /*Dbhelper db1 = new Dbhelper();
    String sql1 = "insert into tb_coursetype(id,courseTypeName) values(?,?)";
    List sqlValues = new ArrayList();
    sqlValues.add("2012004");
    sqlValues.add("qmx4");
    db1.setSqlValues(sqlValues);
    db1.setSql(sql1);
    db1.executeUpdate();*/
     
     
    Dbhelper db = new Dbhelper();
    String sql = "insert into tb_coursetype(id,courseTypeName) values(?,?)";
    List<List> sqlValues = new ArrayList();
    List sqlValueString =new ArrayList();
    sqlValueString.add("2012010");
    sqlValueString.add("qmx10");
    sqlValues.add(sqlValueString);
    List sqlValueString1 =new ArrayList();
    sqlValueString1.add("2012011");
    sqlValueString1.add("qmx11");
    sqlValues.add(sqlValueString1);
    List sqlValueString2 =new ArrayList();
    sqlValueString2.add("2012012");
    sqlValueString2.add("qmx12");
    sqlValues.add(sqlValueString2);
    List sqlValueString3 =new ArrayList();
    sqlValueString3.add("2012013");
    sqlValueString3.add("qmx13");
    sqlValues.add(sqlValueString3);
    db.setSqlValue(sqlValues);
    db.setSql(sql);
    db.executeUpdateMore();
     
  }
}

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 日本一区二区三区视频在线观看 | 国产欧美久久久精品影院 | 日本在线一区二区 | 亚洲 欧美 日韩 综合 | 亚洲精品国产综合久久一线 | 私人影院免费观看 | h肉动漫在线视频无修无遮挡 | 免费看男人使劲躁女人小说 | 免费亚洲视频在线观看 | 亚洲码和乱人伦中文一区 | 美女脱一光二净的视频 | 热久久亚洲 | 偷偷狠狠的日日高清完整视频 | 调教扩张宫颈女人惨叫 | 成人快手破解版 | 激情三级做爰在线观看激情 | 欧美日韩国产精品自在自线 | 午夜精品在线 | 天天舔天天操天天干 | 美女视频久久 | 精品国产无限资源免费观看 | 91赵邦贺 | 日本不卡视频免费的 | 国产自在自线午夜精品之la | 午夜国产精品视频在线 | 国产亚洲精品精品国产亚洲综合 | 欧美日韩中文字幕久久伊人 | 免费看日产一区二区三区 | 好大好爽好硬我要喷水了 | 国产精品久久久久一区二区三区 | 4tube高清性欧美 | 四虎免费影院在线播放 | a色在线| 国产精品免费视频能看 | 欧美一区二区三区不卡视频 | 亚洲国产精品嫩草影院久久 | 日韩成人一区ftp在线播放 | 亚洲精品第五页中文字幕 | 不卡一区二区三区卡 | 免费看一级大片 | 黑人艹逼 |