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

作者:5iasp 时间:2024-01-16 09:47:37 

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

数据库脚本:


-- 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;

代码:


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(); // 生成测试数据
 //Exp();
 //Exp(0);
 //System.out.println(readText("/opt/id.txt"));
}
/**
 * 导出数据
 */
 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();
   }
  }
 }
 /**
 * 导出从startid开始的数据
 * @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);
 }
 /**
 * 导出一小时内的数据
 * @param conn
 */
 public static void Exp(Connection conn) {
  int counter = 0;
  //一小时内的数据
  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();
   }  
  }
 }
 /**
  * 写入内容到文件
  *
  * @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;
 }
 /**
  * 从文件读取内容
  *
  * @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,执行多次sql来处理. 由于大数据量所以不能使用一条sql语句来输出.否则会内存不足导致错误.

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

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

标签:Java,数据库,导出,保存
0
投稿

猜你喜欢

  • Python通过zookeeper实现分布式服务代码解析

    2021-09-11 11:09:48
  • 网站通过W3C标准注意点

    2010-01-14 20:15:00
  • 在Python 2.7即将停止支持时,我们为你带来了一份python 3.x迁移指南

    2022-03-04 21:11:24
  • Go语言中的函数详解

    2024-04-25 15:04:35
  • Python的PIL库中getpixel方法的使用

    2022-01-06 09:08:51
  • python logging模块的使用总结

    2021-11-15 06:46:53
  • python 给图像添加透明度(alpha通道)

    2021-05-04 04:57:22
  • Golang 统计字符串中数字字母数量的实现方法

    2024-04-23 09:43:04
  • 基于vue实现微博三方登录流程解析

    2024-05-03 15:08:42
  • ASP代理采集的核心函数代码

    2010-01-02 20:43:00
  • keras 获取某层的输入/输出 tensor 尺寸操作

    2021-11-14 09:55:06
  • 基于Python3.6中的OpenCV实现图片色彩空间的转换

    2022-05-20 14:03:13
  • .NET 6中为record类型自定义Equals方法

    2023-07-15 21:29:29
  • JS实现获取毫秒值及转换成年月日时分秒的方法

    2024-04-18 09:42:54
  • 微信小程序访问mysql数据库流程详解

    2024-01-23 10:34:43
  • Go gRPC服务客户端流式RPC教程

    2023-07-16 06:08:55
  • 利用python爬取散文网的文章实例教程

    2023-06-17 10:40:18
  • 用server.transfer隐藏网页真实地址

    2007-12-04 13:02:00
  • pycharm下pyqt4安装及环境配置的教程

    2021-11-01 16:03:57
  • 基于Python编写一个爆炸信息窗口脚本

    2023-04-01 02:07:05
  • asp之家 网络编程 m.aspxhome.com