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

服務(wù)器之家:專注于服務(wù)器技術(shù)及軟件下載分享
分類導(dǎo)航

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

服務(wù)器之家 - 編程語言 - JAVA教程 - Java實(shí)現(xiàn)從數(shù)據(jù)庫導(dǎo)出大量數(shù)據(jù)記錄并保存到文件的方法

Java實(shí)現(xiàn)從數(shù)據(jù)庫導(dǎo)出大量數(shù)據(jù)記錄并保存到文件的方法

2020-01-15 14:125iasp JAVA教程

這篇文章主要介紹了Java實(shí)現(xiàn)從數(shù)據(jù)庫導(dǎo)出大量數(shù)據(jù)記錄并保存到文件的方法,涉及Java針對數(shù)據(jù)庫的讀取及文件寫入等操作技巧,具有一定參考借鑒價值,需要的朋友可以參考下

本文實(shí)例講述了Java實(shí)現(xiàn)從數(shù)據(jù)庫導(dǎo)出大量數(shù)據(jù)記錄并保存到文件的方法。分享給大家供大家參考,具體如下:

數(shù)據(jù)庫腳本:

?
1
2
3
4
5
6
7
-- Table "t_test" DDL
CREATE TABLE `t_test` (
 `id` int(11) NOT NULL AUTO_INCREMENT,
 `title` varchar(255) DEFAULT NULL,
 `createTime` bigint(20) DEFAULT NULL,
 PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;

代碼:

?
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
package com.yanek.test;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class TestDB {
 public static void main(String[] args) {
  Test(); // 生成測試數(shù)據(jù)
  //Exp();
  //Exp(0);
  //System.out.println(readText("/opt/id.txt"));
 }
 /**
  * 導(dǎo)出數(shù)據(jù)
  */
  public static void Exp() {
   Connection Conn=null;
   try {
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    String jdbcUrl = "jdbc:mysql://127.0.0.1:3306/testcms?characterEncoding=GBK";
    String jdbcUsername = "root";
    String jdbcPassword = "root";
    Conn = DriverManager.getConnection(jdbcUrl, jdbcUsername, jdbcPassword);
    System.out.println("conn"+Conn);
    Exp(Conn);
   } catch (SQLException e) {
    e.printStackTrace();
   }
   catch (InstantiationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IllegalAccessException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   finally
   {
    try {
     Conn.close();
    } catch (SQLException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }
  public static void Exp(int startid) {
   Connection Conn=null;
   try {
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    String jdbcUrl = "jdbc:mysql://127.0.0.1:3306/testcms?characterEncoding=GBK";
    String jdbcUsername = "root";
    String jdbcPassword = "root";
    Conn = DriverManager.getConnection(jdbcUrl, jdbcUsername, jdbcPassword);
    System.out.println("conn"+Conn);
    Exp(Conn,startid);
   } catch (SQLException e) {
    e.printStackTrace();
   }
   catch (InstantiationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IllegalAccessException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   finally
   {
    try {
     Conn.close();
    } catch (SQLException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }
  /**
  * 導(dǎo)出從startid開始的數(shù)據(jù)
  * @param conn
  * @param start_id
  */
  public static void Exp(Connection conn,int start_id) {
   int counter = 0;
   int startid=start_id;
   boolean flag = true;
   while (flag) {
    flag = false;
    String Sql = "SELECT * FROM t_test WHERE id>"
      + startid + " order by id asc LIMIT 50";
    System.out.println("sql===" + Sql);
    try {
     Statement stmt = conn.createStatement();
     ResultSet rs = stmt.executeQuery(Sql);
      while (rs.next()) {
       flag = true;
       int id = rs.getInt("id");
       String title = rs.getString("title");
       startid = id ;
       counter++;
       writeContent(counter+"--id--"+id+"--title-"+title+"\r\n", "/opt/","log.txt",true);
       System.out.println("i="+counter+"--id--"+id+"--title-"+title);
      }
     rs.close();
     stmt.close();
    } catch (SQLException e) {
     e.printStackTrace();
    }
   }
   writeContent(""+startid, "/opt/","id.txt",false);
  }
  /**
  * 導(dǎo)出一小時內(nèi)的數(shù)據(jù)
  * @param conn
  */
  public static void Exp(Connection conn) {
   int counter = 0;
   //一小時內(nèi)的數(shù)據(jù)
   Long timestamp = System.currentTimeMillis() - (60 * 60 * 1000);
   boolean flag = true;
   while (flag) {
    flag = false;
    String Sql = "SELECT * FROM t_test WHERE createTime>"
      + timestamp + " LIMIT 50";
    System.out.println("sql===" + Sql);
    try {
     Statement stmt = conn.createStatement();
     ResultSet rs = stmt.executeQuery(Sql);
     while (rs.next()) {
      flag = true;
      int id = rs.getInt("id");
      String title = rs.getString("title");
      Long lastmodifytime = rs.getLong("createTime");
      timestamp = lastmodifytime;
      counter++;
      System.out.println("i="+counter+"--id--"+id+"--title-"+title);
     }
     rs.close();
     stmt.close();
    } catch (SQLException e) {
     e.printStackTrace();
    }
   }
  }
  public static void Test() {
   Connection Conn=null;
   try {
    Class.forName("com.mysql.jdbc.Driver").newInstance();
    String jdbcUrl = "jdbc:mysql://127.0.0.1:3306/testcms?characterEncoding=GBK";
    String jdbcUsername = "root";
    String jdbcPassword = "root";
    Conn = DriverManager.getConnection(jdbcUrl, jdbcUsername, jdbcPassword);
    System.out.println("conn"+Conn);
    for(int i=1;i<=10000;i++)
    {
     add(Conn,"testTitle"+i+"-"+System.currentTimeMillis());
    }
   } catch (SQLException e) {
    e.printStackTrace();
   }
   catch (InstantiationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (IllegalAccessException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   } catch (ClassNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   finally
   {
    try {
     Conn.close();
    } catch (SQLException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    }
   }
  }
  public static void add(Connection conn,String title)
  {
   PreparedStatement pstmt = null;
   String insert_sql = "insert into t_test(title,createTime) values (?,?)";
   System.out.println("sql="+insert_sql);
   try {
    pstmt = conn.prepareStatement(insert_sql);
    pstmt.setString(1,title);
    pstmt.setLong(2,System.currentTimeMillis());
    int ret = pstmt.executeUpdate();
   } catch (SQLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
   finally{
    try {
     pstmt.close();
    } catch (SQLException e) {
     // TODO Auto-generated catch block
     e.printStackTrace();
    
   }
  }
  /**
   * 寫入內(nèi)容到文件
   *
   * @param number
   * @param filename
   * @return
   */
  public static boolean writeContent(String c, String dirname,String filename,boolean isAppend) {
   File f=new File(dirname);
   if (!f.exists())
   {
     f.mkdirs();
   }
   try {
    FileOutputStream fos = new FileOutputStream( dirname+File.separator+filename,isAppend);
    OutputStreamWriter writer = new OutputStreamWriter(fos);
    writer.write(c);
    writer.close();
    fos.close();
   } catch (IOException e) {
    e.printStackTrace();
    return false;
   }
   return true;
  }
  /**
   * 從文件讀取內(nèi)容
   *
   * @param filename
   * @return
   */
  public static String readText(String filename) {
   String content = "";
   try {
    File file = new File(filename);
    if (file.exists()) {
     FileReader fr = new FileReader(file);
     BufferedReader br = new BufferedReader(fr);
     String str = "";
     String newline = "";
     while ((str = br.readLine()) != null) {
      content += newline + str;
      newline = "\n";
     }
     br.close();
     fr.close();
    }
   } catch (IOException e) {
    e.printStackTrace();
   }
   return content;
  }
}

基本思想: 就是通過記錄開始記錄id,執(zhí)行多次sql來處理. 由于大數(shù)據(jù)量所以不能使用一條sql語句來輸出.否則會內(nèi)存不足導(dǎo)致錯誤.

主要用途: 可以使用在做接口開發(fā)時,給第三方提供數(shù)據(jù)增量輸出的場景使用.

希望本文所述對大家Java程序設(shè)計有所幫助。

延伸 · 閱讀

精彩推薦
主站蜘蛛池模板: 国产自产自拍 | 日韩在线视精品在亚洲 | 久久青青草原精品国产软件 | 歪歪漫画a漫入口 | 欧美黑人成人免费全部 | 蜜桃久久久亚洲精品成人 | 毛片在线免费视频 | 男人躁女人过程 | 视频在线观看高清免费 | 暖暖的视频完整视频韩国免费 | 亚洲大尺码 | 国产精品网页 | 成年无限观看onlyfans | 国产一区精品视频 | 無码一区中文字幕少妇熟女H | 精品午夜中文字幕熟女人妻在线 | 日韩免费在线视频观看 | 日本人泡妞xxxxxx69 | 大肥臀风间由美 中文字幕 大东北chinesexxxx露脸 | 2022国产麻豆剧传媒剧情 | 91麻豆精品国产片在线观看 | 2019亚洲男人天堂 | 91yellow吧字幕网zmff7 | 91精品国产色综合久久不卡蜜 | 亚洲精品国产在线网站 | 好大好硬好深好爽想要小雪 | 希望影院高清免费观看视频 | 成人网免费视频 | 香蕉久久高清国产精品免费 | 色天天综合网色鬼综合 | 欧美日韩国产精品自在自线 | 爱情岛论坛亚洲品质自拍视频 | 俺去俺来也www色官网免费的 | spank日本网站脱裤子打屁股 | 亚洲天堂岛国片 | 女同全黄h全肉动漫 | 日本免费三区 | 视频一区二区三区在线 | 特黄特级毛片免费视 | 国产亚洲欧美在线中文bt天堂网 | 欧美成人禁片在线观看俄罗斯 |