首页 > 开发 > Java > 正文

Java实现从数据库导出大量数据记录并保存到文件的方法

2024-07-13 09:55:56
字体:
来源:转载
供稿:网友

这篇文章主要介绍了Java实现从数据库导出大量数据记录并保存到文件的方法,涉及Java针对数据库的读取及文件写入等操作技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了Java实现从数据库导出大量数据记录并保存到文件的方法。分享给大家供大家参考,具体如下:

数据库脚本:

 

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

代码:

 

 
  1. package com.yanek.test;  
  2. import java.io.BufferedReader;  
  3. import java.io.File;  
  4. import java.io.FileOutputStream;  
  5. import java.io.FileReader;  
  6. import java.io.IOException;  
  7. import java.io.OutputStreamWriter;  
  8. import java.sql.Connection;  
  9. import java.sql.DriverManager;  
  10. import java.sql.PreparedStatement;  
  11. import java.sql.ResultSet;  
  12. import java.sql.SQLException;  
  13. import java.sql.Statement;  
  14. public class TestDB {  
  15. public static void main(String[] args) {  
  16. Test(); // 生成测试数据  
  17. //Exp();  
  18. //Exp(0);  
  19. //System.out.println(readText("/opt/id.txt"));  
  20. }  
  21. /**  
  22. * 导出数据  
  23. */ 
  24. public static void Exp() {  
  25. Connection Conn=null;  
  26. try {  
  27. Class.forName("com.mysql.jdbc.Driver").newInstance();  
  28. String jdbcUrl = "jdbc:mysql://127.0.0.1:3306/testcms?characterEncoding=GBK";  
  29. String jdbcUsername = "root";  
  30. String jdbcPassword = "root";  
  31. Conn = DriverManager.getConnection(jdbcUrl, jdbcUsername, jdbcPassword);  
  32. System.out.println("conn"+Conn);  
  33. Exp(Conn);  
  34. catch (SQLException e) {  
  35. e.printStackTrace();  
  36. }  
  37. catch (InstantiationException e) {  
  38. // TODO Auto-generated catch block  
  39. e.printStackTrace();  
  40. catch (IllegalAccessException e) {  
  41. // TODO Auto-generated catch block  
  42. e.printStackTrace();  
  43. catch (ClassNotFoundException e) {  
  44. // TODO Auto-generated catch block  
  45. e.printStackTrace();  
  46. }  
  47. finally 
  48. {  
  49. try {  
  50. Conn.close();  
  51. catch (SQLException e) {  
  52. // TODO Auto-generated catch block  
  53. e.printStackTrace();  
  54. }  
  55. }  
  56. }  
  57. public static void Exp(int startid) {  
  58. Connection Conn=null;  
  59. try {  
  60. Class.forName("com.mysql.jdbc.Driver").newInstance();  
  61. String jdbcUrl = "jdbc:mysql://127.0.0.1:3306/testcms?characterEncoding=GBK";  
  62. String jdbcUsername = "root";  
  63. String jdbcPassword = "root";  
  64. Conn = DriverManager.getConnection(jdbcUrl, jdbcUsername, jdbcPassword);  
  65. System.out.println("conn"+Conn);  
  66. Exp(Conn,startid);  
  67. catch (SQLException e) {  
  68. e.printStackTrace();  
  69. }  
  70. catch (InstantiationException e) {  
  71. // TODO Auto-generated catch block  
  72. e.printStackTrace();  
  73. catch (IllegalAccessException e) {  
  74. // TODO Auto-generated catch block  
  75. e.printStackTrace();  
  76. catch (ClassNotFoundException e) {  
  77. // TODO Auto-generated catch block  
  78. e.printStackTrace();  
  79. }  
  80. finally 
  81. {  
  82. try {  
  83. Conn.close();  
  84. catch (SQLException e) {  
  85. // TODO Auto-generated catch block  
  86. e.printStackTrace();  
  87. }  
  88. }  
  89. }  
  90. /**  
  91. * 导出从startid开始的数据  
  92. * @param conn  
  93. * @param start_id  
  94. */ 
  95. public static void Exp(Connection conn,int start_id) {  
  96. int counter = 0;  
  97. int startid=start_id;  
  98. boolean flag = true;  
  99. while (flag) {  
  100. flag = false;  
  101. String Sql = "SELECT * FROM t_test WHERE id>" 
  102. + startid + " order by id asc LIMIT 50";  
  103. System.out.println("sql===" + Sql);  
  104. try {  
  105. Statement stmt = conn.createStatement();  
  106. ResultSet rs = stmt.executeQuery(Sql);  
  107. while (rs.next()) {  
  108. flag = true;  
  109. int id = rs.getInt("id");  
  110. String title = rs.getString("title");  
  111. startid = id ;  
  112. counter++;  
  113. writeContent(counter+"--id--"+id+"--title-"+title+"/r/n""/opt/","log.txt",true);  
  114. System.out.println("i="+counter+"--id--"+id+"--title-"+title);  
  115. }  
  116. rs.close();  
  117. stmt.close();  
  118. catch (SQLException e) {  
  119. e.printStackTrace();  
  120. }  
  121. }  
  122. writeContent(""+startid, "/opt/","id.txt",false);  
  123. }  
  124. /**  
  125. * 导出一小时内的数据  
  126. * @param conn  
  127. */ 
  128. public static void Exp(Connection conn) {  
  129. int counter = 0;  
  130. //一小时内的数据  
  131. Long timestamp = System.currentTimeMillis() - (60 * 60 * 1000);  
  132. boolean flag = true;  
  133. while (flag) {  
  134. flag = false;  
  135. String Sql = "SELECT * FROM t_test WHERE createTime>" 
  136. + timestamp + " LIMIT 50";  
  137. System.out.println("sql===" + Sql);  
  138. try {  
  139. Statement stmt = conn.createStatement();  
  140. ResultSet rs = stmt.executeQuery(Sql);  
  141. while (rs.next()) {  
  142. flag = true;  
  143. int id = rs.getInt("id");  
  144. String title = rs.getString("title");  
  145. Long lastmodifytime = rs.getLong("createTime");  
  146. timestamp = lastmodifytime;  
  147. counter++;  
  148. System.out.println("i="+counter+"--id--"+id+"--title-"+title);  
  149. }  
  150. rs.close();  
  151. stmt.close();  
  152. catch (SQLException e) {  
  153. e.printStackTrace();  
  154. }  
  155. }  
  156. }  
  157. public static void Test() {  
  158. Connection Conn=null;  
  159. try {  
  160. Class.forName("com.mysql.jdbc.Driver").newInstance();  
  161. String jdbcUrl = "jdbc:mysql://127.0.0.1:3306/testcms?characterEncoding=GBK";  
  162. String jdbcUsername = "root";  
  163. String jdbcPassword = "root";  
  164. Conn = DriverManager.getConnection(jdbcUrl, jdbcUsername, jdbcPassword);  
  165. System.out.println("conn"+Conn);  
  166. for(int i=1;i<=10000;i++)  
  167. {  
  168. add(Conn,"testTitle"+i+"-"+System.currentTimeMillis());  
  169. }  
  170. catch (SQLException e) {  
  171. e.printStackTrace();  
  172. }  
  173. catch (InstantiationException e) {  
  174. // TODO Auto-generated catch block  
  175. e.printStackTrace();  
  176. catch (IllegalAccessException e) {  
  177. // TODO Auto-generated catch block  
  178. e.printStackTrace();  
  179. catch (ClassNotFoundException e) {  
  180. // TODO Auto-generated catch block  
  181. e.printStackTrace();  
  182. }  
  183. finally 
  184. {  
  185. try {  
  186. Conn.close();  
  187. catch (SQLException e) {  
  188. // TODO Auto-generated catch block  
  189. e.printStackTrace();  
  190. }  
  191. }  
  192. }  
  193. public static void add(Connection conn,String title)  
  194. {  
  195. PreparedStatement pstmt = null;  
  196. String insert_sql = "insert into t_test(title,createTime) values (?,?)";  
  197. System.out.println("sql="+insert_sql);  
  198. try {  
  199. pstmt = conn.prepareStatement(insert_sql);  
  200. pstmt.setString(1,title);  
  201. pstmt.setLong(2,System.currentTimeMillis());  
  202. int ret = pstmt.executeUpdate();  
  203. catch (SQLException e) {  
  204. // TODO Auto-generated catch block  
  205. e.printStackTrace();  
  206. }  
  207. finally{  
  208. try {  
  209. pstmt.close();  
  210. catch (SQLException e) {  
  211. // TODO Auto-generated catch block  
  212. e.printStackTrace();  
  213. }  
  214. }  
  215. }  
  216. /**  
  217. * 写入内容到文件  
  218.  
  219. * @param number  
  220. * @param filename  
  221. * @return  
  222. */ 
  223. public static boolean writeContent(String c, String dirname,String filename,boolean isAppend) {  
  224. File f=new File(dirname);  
  225. if (!f.exists())  
  226. {  
  227. f.mkdirs();  
  228. }  
  229. try {  
  230. FileOutputStream fos = new FileOutputStream( dirname+File.separator+filename,isAppend);  
  231. OutputStreamWriter writer = new OutputStreamWriter(fos);  
  232. writer.write(c);  
  233. writer.close();  
  234. fos.close();  
  235. catch (IOException e) {  
  236. e.printStackTrace();  
  237. return false;  
  238. }  
  239. return true;  
  240. }  
  241. /**  
  242. * 从文件读取内容  
  243.  
  244. * @param filename  
  245. * @return  
  246. */ 
  247. public static String readText(String filename) {  
  248. String content = "";  
  249. try {  
  250. File file = new File(filename);  
  251. if (file.exists()) {  
  252. FileReader fr = new FileReader(file);  
  253. BufferedReader br = new BufferedReader(fr);  
  254. String str = "";  
  255. String newline = "";  
  256. while ((str = br.readLine()) != null) {  
  257. content += newline + str;  
  258. newline = "/n";  
  259. }  
  260. br.close();  
  261. fr.close();  
  262. }  
  263. catch (IOException e) {  
  264. e.printStackTrace();  
  265. }  
  266. return content;  
  267. }  
  268. }  

基本思想: 就是通过记录开始记录id,执行多次sql来处理. 由于大数据量所以不能使用一条sql语句来输出.否则会内存不足导致错误.

主要用途: 可以使用在做接口开发时,给第三方提供数据增量输出的场景使用.

希望本文所述对大家Java程序设计有所帮助。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表